Skip to content

Commit

Permalink
Fix message about unicode/str mixing
Browse files Browse the repository at this point in the history
In nose-devs#288, the new warning message incorrectly stated that the
problem could arise from mixing non-ASCII 'unicode' and
non-ASCII 'str'. This is incorrect, as the 'unicode' does not
have to be non-ASCII.

This rewords the warning message to be correct, and also changes
the unit test to illustrate this.
  • Loading branch information
jmoldow committed Mar 23, 2016
1 parent b7185ad commit ede0f6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion nose2/plugins/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def outcomeDetail(self, event):
event.extraDetail.append(ln('>> end captured %s <<' % stream))
if stream_buffer_exc_info:
event.extraDetail.append('OUTPUT ERROR: Could not get captured %s output.' % stream)
event.extraDetail.append('The test may have output both non-ASCII Unicode strings and non-ASCII 8-bit strings.')
event.extraDetail.append("The test might've printed both 'unicode' strings and non-ASCII 8-bit 'str' strings.")
event.extraDetail.append(ln('>> begin captured %s exception traceback <<' % stream))
event.extraDetail.append(''.join(traceback.format_exception(*stream_buffer_exc_info)))
event.extraDetail.append(ln('>> end captured %s exception traceback <<' % stream))
Expand Down
23 changes: 13 additions & 10 deletions nose2/tests/unit/test_buffer_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def setUp(self):

class Test(TestCase):

unicode_string = util.safe_decode("test 日本")
printed_nonascii_str = util.safe_decode("test 日本").encode('utf-8')
printed_unicode = six.u("hello")

def test_out(self):
six.print_("hello")
Expand All @@ -29,11 +30,11 @@ def test_out(self):
def test_err(self):
six.print_("goodbye", file=sys.stderr)

def test_nonascii_mixed_unicode_and_str(self):
six.print_(self.unicode_string)
six.print_(self.unicode_string.encode('utf-8'))
six.print_(self.unicode_string, file=sys.stderr)
six.print_(self.unicode_string.encode('utf-8'), file=sys.stderr)
def test_mixed_unicode_and_nonascii_str(self):
six.print_(self.printed_nonascii_str)
six.print_(self.printed_unicode)
six.print_(self.printed_nonascii_str, file=sys.stderr)
six.print_(self.printed_unicode, file=sys.stderr)
raise {}["oops"]

self.case = Test
Expand Down Expand Up @@ -75,19 +76,21 @@ def test_captures_stderr_when_configured(self):
finally:
sys.stderr = err

def test_does_not_crash_with_nonascii_mixed_unicode_and_str(self):
def test_does_not_crash_with_mixed_unicode_and_nonascii_str(self):
self.plugin.captureStderr = True
test = self.case('test_nonascii_mixed_unicode_and_str')
test = self.case('test_mixed_unicode_and_nonascii_str')
test(self.result)
evt = events.OutcomeDetailEvent(self.watcher.events[0])
self.session.hooks.outcomeDetail(evt)
extraDetail = "".join(evt.extraDetail)
if six.PY2:
assert self.case.unicode_string not in extraDetail, "Output unexpectedly found in error message"
for string in [util.safe_decode(self.case.printed_nonascii_str), self.case.printed_unicode]:
assert string not in extraDetail, "Output unexpectedly found in error message"
assert "OUTPUT ERROR" in extraDetail
assert "UnicodeDecodeError" in extraDetail
else:
assert self.case.unicode_string in extraDetail, "Output not found in error message"
for string in [repr(self.case.printed_nonascii_str), self.case.printed_unicode]:
assert string in extraDetail, "Output not found in error message"

def test_decorates_outcome_detail(self):
test = self.case('test_out')
Expand Down

0 comments on commit ede0f6d

Please sign in to comment.