From 79ccbb950d04754028f67cd62dbad83a1bfbec7b Mon Sep 17 00:00:00 2001 From: Pavel Minaev Date: Sun, 29 Sep 2019 23:58:49 -0700 Subject: [PATCH] Fix encoding issues when writing test logs. --- tests/pytest_fixtures.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/pytest_fixtures.py b/tests/pytest_fixtures.py index b206347f..14608764 100644 --- a/tests/pytest_fixtures.py +++ b/tests/pytest_fixtures.py @@ -5,6 +5,7 @@ from __future__ import absolute_import, print_function, unicode_literals import inspect +import io import os import platform import py.path @@ -51,7 +52,9 @@ def test_wrapper(request, long_tmpdir): original_log_dir = None try: - if options.log_dir is not None: + if options.log_dir is None: + write_log = lambda filename, data: None + else: original_log_dir = options.log_dir log_subdir = request.node.name for ch in r"\/:?*|<>": @@ -62,6 +65,11 @@ def test_wrapper(request, long_tmpdir): except Exception: pass + def write_log(filename, data): + filename = os.path.join(options.log_dir, filename) + with io.open(filename, "w", encoding="utf-8") as f: + f.write(data) + print("\n") # make sure on-screen logs start on a new line with log.to_file(prefix="tests"): log.info("Test {0} started.", request.node.name) @@ -84,17 +92,12 @@ def test_wrapper(request, long_tmpdir): report.outcome, ) - if options.log_dir is not None: - with open(os.path.join(options.log_dir, report_attr + ".log"), "w") as f: - f.write(report.longreprtext) - with open(os.path.join(options.log_dir, report_attr + ".stdout.log"), "w") as f: - f.write(report.capstdout) - with open(os.path.join(options.log_dir, report_attr + ".stderr.log"), "w") as f: - f.write(report.capstderr) + write_log(report_attr + ".log", report.longreprtext) + write_log(report_attr + ".stdout.log", report.capstdout) + write_log(report_attr + ".stderr.log", report.capstderr) - if failed and options.log_dir is not None: - with open(os.path.join(options.log_dir, "FAILED.log"), "w"): - pass + if failed: + write_log("FAILED.log", "") finally: if original_log_dir is not None: options.log_dir = original_log_dir