Skip to content

Commit

Permalink
Merge pull request #3641 from Sup3rGeo/bugfix/logfile-unicode
Browse files Browse the repository at this point in the history
Fixes #3630
  • Loading branch information
nicoddemus authored Jun 30, 2018
2 parents a48c47b + dad3e77 commit 26e1784
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/3630.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Log messages with unicode characters would not appear in the output log file.
4 changes: 3 additions & 1 deletion src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ def __init__(self, config):
config, "log_file_date_format", "log_date_format"
)
# Each pytest runtests session will write to a clean logfile
self.log_file_handler = logging.FileHandler(log_file, mode="w")
self.log_file_handler = logging.FileHandler(
log_file, mode="w", encoding="UTF-8"
)
log_file_formatter = logging.Formatter(
log_file_format, datefmt=log_file_date_format
)
Expand Down
38 changes: 38 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import re
import os
from io import open

import six

Expand Down Expand Up @@ -827,6 +828,43 @@ def test_log_file(request):
assert "This log message won't be shown" not in contents


def test_log_file_unicode(testdir):
log_file = testdir.tmpdir.join("pytest.log").strpath

testdir.makeini(
"""
[pytest]
log_file={}
log_file_level = INFO
""".format(
log_file
)
)
testdir.makepyfile(
"""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
def test_log_file():
logging.getLogger('catchlog').info("Normal message")
logging.getLogger('catchlog').info("├")
logging.getLogger('catchlog').info("Another normal message")
"""
)

result = testdir.runpytest()

# make sure that that we get a '0' exit code for the testsuite
assert result.ret == 0
assert os.path.isfile(log_file)
with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read()
assert "Normal message" in contents
assert u"├" in contents
assert "Another normal message" in contents


@pytest.mark.parametrize("has_capture_manager", [True, False])
def test_live_logging_suspends_capture(has_capture_manager, request):
"""Test that capture manager is suspended when we emitting messages for live logging.
Expand Down

0 comments on commit 26e1784

Please sign in to comment.