Kaydet (Commit) 9b4ee12e authored tarafından Michael Foord's avatar Michael Foord

Cross platform unittest.TestResult newline handling when buffering stdout / stderr.

üst 1c7c11ef
...@@ -19,9 +19,8 @@ def failfast(method): ...@@ -19,9 +19,8 @@ def failfast(method):
return method(self, *args, **kw) return method(self, *args, **kw)
return inner return inner
NEWLINE = os.linesep STDOUT_LINE = '\nStdout:\n%s'
STDOUT_LINE = '%sStdout:%s%%s' % (NEWLINE, NEWLINE) STDERR_LINE = '\nStderr:\n%s'
STDERR_LINE = '%sStderr:%s%%s' % (NEWLINE, NEWLINE)
class TestResult(object): class TestResult(object):
...@@ -77,12 +76,12 @@ class TestResult(object): ...@@ -77,12 +76,12 @@ class TestResult(object):
output = sys.stdout.getvalue() output = sys.stdout.getvalue()
error = sys.stderr.getvalue() error = sys.stderr.getvalue()
if output: if output:
if not output.endswith(NEWLINE): if not output.endswith('\n'):
output += NEWLINE output += '\n'
self._original_stdout.write(STDOUT_LINE % output) self._original_stdout.write(STDOUT_LINE % output)
if error: if error:
if not error.endswith(NEWLINE): if not error.endswith('\n'):
error += NEWLINE error += '\n'
self._original_stderr.write(STDERR_LINE % error) self._original_stderr.write(STDERR_LINE % error)
sys.stdout = self._original_stdout sys.stdout = self._original_stdout
...@@ -158,12 +157,12 @@ class TestResult(object): ...@@ -158,12 +157,12 @@ class TestResult(object):
output = sys.stdout.getvalue() output = sys.stdout.getvalue()
error = sys.stderr.getvalue() error = sys.stderr.getvalue()
if output: if output:
if not output.endswith(NEWLINE): if not output.endswith('\n'):
output += NEWLINE output += '\n'
msgLines.append(STDOUT_LINE % output) msgLines.append(STDOUT_LINE % output)
if error: if error:
if not error.endswith(NEWLINE): if not error.endswith('\n'):
error += NEWLINE error += '\n'
msgLines.append(STDERR_LINE % error) msgLines.append(STDERR_LINE % error)
return ''.join(msgLines) return ''.join(msgLines)
......
import os
import sys import sys
import textwrap import textwrap
from cStringIO import StringIO, OutputType from cStringIO import StringIO, OutputType
...@@ -413,8 +414,8 @@ class TestOutputBuffering(unittest.TestCase): ...@@ -413,8 +414,8 @@ class TestOutputBuffering(unittest.TestCase):
print 'foo' print 'foo'
print >> sys.stderr, 'bar' print >> sys.stderr, 'bar'
self.assertEqual(out_stream.getvalue(), 'foo\n') self.assertEqual(out_stream.getvalue(), 'foo%s' % os.linesep)
self.assertEqual(err_stream.getvalue(), 'bar\n') self.assertEqual(err_stream.getvalue(), 'bar%s' % os.linesep)
self.assertEqual(result._original_stdout.getvalue(), '') self.assertEqual(result._original_stdout.getvalue(), '')
self.assertEqual(result._original_stderr.getvalue(), '') self.assertEqual(result._original_stderr.getvalue(), '')
...@@ -467,13 +468,13 @@ class TestOutputBuffering(unittest.TestCase): ...@@ -467,13 +468,13 @@ class TestOutputBuffering(unittest.TestCase):
expectedOutMessage = textwrap.dedent(""" expectedOutMessage = textwrap.dedent("""
Stdout: Stdout:
foo foo
""") """).replace('\n', os.linesep)
expectedErrMessage = '' expectedErrMessage = ''
if include_error: if include_error:
expectedErrMessage = textwrap.dedent(""" expectedErrMessage = textwrap.dedent("""
Stderr: Stderr:
bar bar
""") """).replace('\n', os.linesep)
expectedFullMessage = 'None\n%s%s' % (expectedOutMessage, expectedErrMessage) expectedFullMessage = 'None\n%s%s' % (expectedOutMessage, expectedErrMessage)
self.assertIs(test, self) self.assertIs(test, self)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment