| 1 |
# -*- coding: UTF-8 |
| 2 |
""" |
| 3 |
Test issue #336: Unicode problem w/ exception traceback on Windows (python2.7) |
| 4 |
Default encoding (unicode-escape) of text() causes problems w/ |
| 5 |
exception tracebacks. |
| 6 |
|
| 7 |
STATUS: BASICS SOLVED (default encoding changed) |
| 8 |
|
| 9 |
ALTERNATIVE SOLUTIONS: |
| 10 |
* Use traceback2: Returns unicode-string when calling traceback. |
| 11 |
* Use text(traceback.format_exc(), sys.getfilesystemencoding(), "replace") |
| 12 |
where the text-conversion of a traceback is used. |
| 13 |
MAYBE traceback_to_text(traceback.format_exc()) |
| 14 |
""" |
| 15 |
|
| 16 |
from __future__ import print_function |
| 17 |
from behave.textutil import text |
| 18 |
import pytest |
| 19 |
import six |
| 20 |
|
| 21 |
# require_python2 = pytest.mark.skipif(not six.PY2, reason="REQUIRE: Python2") |
| 22 |
# require_python3 = pytest.mark.skipif(six.PY2, reason="REQUIRE: Python3 (or newer)") |
| 23 |
|
| 24 |
|
| 25 |
class TestIssue(object): |
| 26 |
# -- USE SAVED TRACEBACK: No need to require Windows platform. |
| 27 |
traceback_bytes = br"""\ |
| 28 |
Traceback (most recent call last): |
| 29 |
File "C:\Users\alice\xxx\behave\model.py", line 1456, in run |
| 30 |
match.run(runner.context) |
| 31 |
File "C:\Users\alice\xxx\behave\model.py", line 1903, in run |
| 32 |
self.func(context, args, *kwargs) |
| 33 |
File "features\steps\my_steps.py", line 210, in step_impl |
| 34 |
directories, task_names, reg_keys) |
| 35 |
AssertionError |
| 36 |
""" |
| 37 |
|
| 38 |
traceback_file_line_texts = [ |
| 39 |
# -- NOTE: Cannot use: ur'C:\Users ..." => \U is a unicode escape char. |
| 40 |
u'File "C:\\Users\\alice\\xxx\\behave\\model.py", line 1456, in run', |
| 41 |
u'File "C:\\Users\\alice\\xxx\\behave\\model.py", line 1903, in run', |
| 42 |
u'File "features\\steps\\my_steps.py", line 210, in step_impl', |
| 43 |
] |
| 44 |
|
| 45 |
# @require_python2 |
| 46 |
def test_issue__with_default_encoding(self): |
| 47 |
"""Test ensures that problem is fixed with default encoding""" |
| 48 |
text2 = text(self.traceback_bytes) |
| 49 |
assert isinstance(self.traceback_bytes, bytes) |
| 50 |
assert isinstance(text2, six.text_type) |
| 51 |
for file_line_text in self.traceback_file_line_texts: |
| 52 |
assert file_line_text in text2 |
| 53 |
|
| 54 |
# @require_python2 |
| 55 |
def test__problem_exists_with_problematic_encoding(self): |
| 56 |
"""Test ensures that problem exists with encoding=unicode-escape""" |
| 57 |
# -- NOTE: Explicit use of problematic encoding |
| 58 |
problematic_encoding = "unicode-escape" |
| 59 |
text2 = text(self.traceback_bytes, problematic_encoding) |
| 60 |
print("TEXT: "+ text2) |
| 61 |
assert isinstance(self.traceback_bytes, bytes) |
| 62 |
assert isinstance(text2, six.text_type) |
| 63 |
|
| 64 |
# -- VERIFY BAD-OUTCOME: With problematic encoding |
| 65 |
file_line_text = self.traceback_file_line_texts[0] |
| 66 |
assert file_line_text not in text2 |