| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
Explore issue #495. |
| 4 |
HINT: Problem can be reproduced if you remove encoding-declaration from above. |
| 5 |
|
| 6 |
:: |
| 7 |
Traceback (most recent call last): |
| 8 |
File "/usr/local/bin/behave", line 11, in <module> |
| 9 |
sys.exit(main()) |
| 10 |
File "/usr/local/lib/python2.7/dist-packages/behave/__main__.py", line 109, in main |
| 11 |
failed = runner.run() |
| 12 |
... |
| 13 |
File "/usr/local/lib/python2.7/dist-packages/behave/model.py", line 919, in run |
| 14 |
runner.run_hook('after_scenario', runner.context, self) |
| 15 |
File "/usr/local/lib/python2.7/dist-packages/behave/runner.py", line 405, in run_hook |
| 16 |
self.hooks[name](context, *args) |
| 17 |
File "/mnt/work/test/lib/logcapture.py", line 22, in f |
| 18 |
v = h.getvalue() |
| 19 |
File "/usr/local/lib/python2.7/dist-packages/behave/log_capture.py", line 99, in getvalue |
| 20 |
return '\n'.join(self.formatter.format(r) for r in self.buffer) |
| 21 |
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 3807: ordinal not in range(128) |
| 22 |
""" |
| 23 |
|
| 24 |
from behave.log_capture import capture |
| 25 |
from behave.configuration import Configuration |
| 26 |
import logging |
| 27 |
import pytest |
| 28 |
|
| 29 |
# ----------------------------------------------------------------------------- |
| 30 |
# TEST SUPPORT |
| 31 |
# ----------------------------------------------------------------------------- |
| 32 |
# def ensure_logging_setup(): |
| 33 |
# logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
| 34 |
|
| 35 |
class SimpleContext(object): pass |
| 36 |
|
| 37 |
|
| 38 |
# ----------------------------------------------------------------------------- |
| 39 |
# TESTS: |
| 40 |
# ----------------------------------------------------------------------------- |
| 41 |
@pytest.mark.parametrize("log_message", [ |
| 42 |
u"Hello Alice", # case: unproblematic (GOOD CASE) |
| 43 |
u"Ärgernis ist überall", # case: unicode-string |
| 44 |
"Ärgernis", # case: byte-string (use encoding-declaration above) |
| 45 |
]) |
| 46 |
def test_issue(log_message): |
| 47 |
@capture(level=logging.INFO) |
| 48 |
def hook_after_scenario(context, message): |
| 49 |
logging.warn(message) |
| 50 |
raise RuntimeError() |
| 51 |
|
| 52 |
# -- PREPARE: |
| 53 |
# ensure_logging_setup() |
| 54 |
context = SimpleContext() |
| 55 |
context.config = Configuration("", load_config=False, |
| 56 |
log_capture=True, |
| 57 |
logging_format="%(levelname)s: %(message)s", |
| 58 |
logging_level=logging.INFO |
| 59 |
) |
| 60 |
context.config.setup_logging() |
| 61 |
|
| 62 |
# -- EXECUTE: |
| 63 |
with pytest.raises(RuntimeError): |
| 64 |
hook_after_scenario(context, log_message) |
| 65 |
# EXPECT: UnicodeError is not raised |