From b61d5232857526850580df5fab1f47a81a846343 Mon Sep 17 00:00:00 2001 From: symonk Date: Wed, 8 Jul 2020 23:56:09 +0100 Subject: [PATCH 1/5] #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 From 2bd937bfa01dc8459753ed329391353611f09fd1 Mon Sep 17 00:00:00 2001 From: symonk Date: Thu, 9 Jul 2020 00:05:34 +0100 Subject: [PATCH 2/5] add dummy test to marker subdirs created --- testing/logging/test_reporting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index dbad3c521cf..b89e6794868 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1178,6 +1178,7 @@ def test_log_file_marker_subdirectories_are_successfully_created(testdir): log_file = sub/logf.log """, ) + testdir.makepyfile(""" def test_logger(): pass """) expected = os.path.join(os.path.dirname(path), "sub") result = testdir.runpytest() assert "logf.log" in os.listdir(expected) From 806ee073393dc17650c37e0da8d218088f5fdf3e Mon Sep 17 00:00:00 2001 From: symonk Date: Thu, 9 Jul 2020 00:09:57 +0100 Subject: [PATCH 3/5] add changelog improvement entry --- changelog/7467.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/7467.improvement.rst diff --git a/changelog/7467.improvement.rst b/changelog/7467.improvement.rst new file mode 100644 index 00000000000..1dbfd162547 --- /dev/null +++ b/changelog/7467.improvement.rst @@ -0,0 +1 @@ +``--log-file`` CLI option and ``log_file`` ini marker now creates subdirectories if specified. From a026af43b781afcb8ae708183f63b9491454bc99 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 11 Jul 2020 12:38:36 -0300 Subject: [PATCH 4/5] Remove redundant tests --- changelog/7467.improvement.rst | 2 +- testing/logging/test_reporting.py | 36 ------------------------------- 2 files changed, 1 insertion(+), 37 deletions(-) diff --git a/changelog/7467.improvement.rst b/changelog/7467.improvement.rst index 1dbfd162547..b7cf3b4d8aa 100644 --- a/changelog/7467.improvement.rst +++ b/changelog/7467.improvement.rst @@ -1 +1 @@ -``--log-file`` CLI option and ``log_file`` ini marker now creates subdirectories if specified. +``--log-file`` CLI option and ``log_file`` ini marker now create subdirectories if needed. diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index b89e6794868..1d38c5c81ae 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1161,39 +1161,3 @@ def test_log_file_cli_subdirectories_are_successfully_created(testdir): 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 - """, - ) - testdir.makepyfile(""" def test_logger(): pass """) - 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 From 0f1c6b799054b515e03c3c3ac7adbfc4bd6e6553 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 11 Jul 2020 12:41:37 -0300 Subject: [PATCH 5/5] Fix test in py35 --- testing/logging/test_reporting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 1d38c5c81ae..32224325884 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1157,7 +1157,7 @@ def test_bad_log(monkeypatch): 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") + expected = os.path.join(os.path.dirname(str(path)), "foo", "bar") result = testdir.runpytest("--log-file=foo/bar/logf.log") assert "logf.log" in os.listdir(expected) assert result.ret == ExitCode.OK