From b61d5232857526850580df5fab1f47a81a846343 Mon Sep 17 00:00:00 2001 From: symonk Date: Wed, 8 Jul 2020 23:56:09 +0100 Subject: [PATCH] #7467 create subdirectories if they do not exist for --log-file and log_file --- src/_pytest/logging.py | 6 +++++ testing/logging/test_reporting.py | 44 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 52d75e66d9f..1c6bfb19e7a 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -531,11 +531,17 @@ def __init__(self, config: Config) -> None: # File logging. self.log_file_level = get_log_level_for_setting(config, "log_file_level") log_file = get_option_ini(config, "log_file") or os.devnull + if log_file != os.devnull: + directory = os.path.dirname(os.path.abspath(log_file)) + if not os.path.isdir(directory): + os.makedirs(directory) + self.log_file_handler = _FileHandler(log_file, mode="w", encoding="UTF-8") log_file_format = get_option_ini(config, "log_file_format", "log_format") log_file_date_format = get_option_ini( config, "log_file_date_format", "log_date_format" ) + log_file_formatter = logging.Formatter( log_file_format, datefmt=log_file_date_format ) diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index bbdf28b389a..dbad3c521cf 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -5,6 +5,7 @@ import pytest from _pytest.capture import CaptureManager +from _pytest.config import ExitCode from _pytest.pytester import Testdir from _pytest.terminal import TerminalReporter @@ -1152,3 +1153,46 @@ def test_bad_log(monkeypatch): ) result = testdir.runpytest() result.assert_outcomes(passed=1) + + +def test_log_file_cli_subdirectories_are_successfully_created(testdir): + path = testdir.makepyfile(""" def test_logger(): pass """) + expected = os.path.join(os.path.dirname(path), "foo", "bar") + result = testdir.runpytest("--log-file=foo/bar/logf.log") + assert "logf.log" in os.listdir(expected) + assert result.ret == ExitCode.OK + + +def test_log_file_cli_no_subdirectories_works_ok(testdir): + path = testdir.makepyfile(""" def test_logger(): pass """) + expected = os.path.join(os.path.dirname(path)) + result = testdir.runpytest("--log-file=logf.log") + assert "logf.log" in os.listdir(expected) + assert result.ret == ExitCode.OK + + +def test_log_file_marker_subdirectories_are_successfully_created(testdir): + path = testdir.makeini( + """ + [pytest] + log_file = sub/logf.log + """, + ) + expected = os.path.join(os.path.dirname(path), "sub") + result = testdir.runpytest() + assert "logf.log" in os.listdir(expected) + assert result.ret == ExitCode.OK + + +def test_log_file_marker_no_subdirectories_works_ok(testdir): + path = testdir.makeini( + """ + [pytest] + log_file = logf.log + """, + ) + testdir.makepyfile(""" def test_logger(): pass """) + expected = os.path.join(os.path.dirname(path)) + result = testdir.runpytest() + assert "logf.log" in os.listdir(expected) + assert result.ret == ExitCode.OK