From 04542db2ff83269180352af8c3e2c09249c1ad87 Mon Sep 17 00:00:00 2001 From: ziebam Date: Sat, 19 Feb 2022 22:04:09 +0100 Subject: [PATCH 1/4] Make log file relative to ini file if it exists --- src/_pytest/logging.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 0163554bae4..29a7d6a7317 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -560,6 +560,12 @@ def __init__(self, config: Config) -> None: if not os.path.isdir(directory): os.makedirs(directory) + # Log file should be relative to cwd when passed as a cmd argument and + # relative to the config file otherwise. + is_inifile_argument = "--log-file" not in config.invocation_params.args + if config.inipath is not None and is_inifile_argument: + log_file = os.path.join(config.inipath.parent, log_file) + 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( From 22682cb7e0962d9d16ab0c4f427ee5129f5cce64 Mon Sep 17 00:00:00 2001 From: ziebam Date: Sat, 19 Feb 2022 22:44:33 +0100 Subject: [PATCH 2/4] Add tests --- testing/logging/test_reporting.py | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 323ff7b2446..e55a64f8cf8 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1165,3 +1165,92 @@ def test_log_file_cli_subdirectories_are_successfully_created( result = pytester.runpytest("--log-file=foo/bar/logf.log") assert "logf.log" in os.listdir(expected) assert result.ret == ExitCode.OK + + +def test_log_file_in_root_dir_when_pytest_invoked_in_subdir(pytester, monkeypatch): + """Issue #7336 - log_file should be relative to the config file when it contains the log_file option.""" + pytester.makeini( + """ + [pytest] + log_file = pytest.log + """ + ) + + subdir = pytester.mkdir("subdir") + testfile = pytester.makepyfile( + """ + def test_this(): + pass + """ + ) + testfile.replace(subdir / testfile) + + monkeypatch.chdir(subdir) + pytester.runpytest() + pytester.chdir() # Restore the initial cwd. + + files_in_root_dir = set(os.listdir()) + assert {"pytest.log", "tox.ini"}.issubset(files_in_root_dir) + + +def test_log_file_in_subdir_when_specified_and_pytest_invoked_in_subdir( + pytester, monkeypatch +): + """Issue #7336 - log_file should be relative to the config file when it contains the log_file option.""" + pytester.makeini( + """ + [pytest] + log_file = subdir/pytest.log + """ + ) + + subdir = pytester.mkdir("subdir") + testfile = pytester.makepyfile( + """ + def test_this(): + pass + """ + ) + testfile.replace(subdir / testfile) + + monkeypatch.chdir(subdir) + pytester.runpytest() + + files_in_subdir = set(os.listdir()) + assert "pytest.log" in files_in_subdir + assert "tox.ini" not in files_in_subdir + + +def test_log_file_relative_to_invocation_dir_when_passed_as_cmd_argument( + pytester, monkeypatch +): + """PR #7350 comment related to issue #7350 (https://github.com/pytest-dev/pytest/pull/7350#pullrequestreview-429803796). + + Log_file should be relative to the invocation dir when passed as a commandline argument. + """ + pytester.makeini( + """ + [pytest] + """ + ) + + subdir = pytester.mkdir("subdir") + testfile = pytester.makepyfile( + """ + def test_this(): + pass + """ + ) + testfile.replace(subdir / testfile) + + monkeypatch.chdir(subdir) + pytester.runpytest("--log-file", "pytest.log") + + files_in_subdir = set(os.listdir()) + assert "pytest.log" in files_in_subdir + assert "tox.ini" not in files_in_subdir + + pytester.chdir() # Restore the initial cwd. + files_in_root_dir = set(os.listdir()) + assert "pytest.log" not in files_in_root_dir + assert "tox.ini" in files_in_root_dir From 0998c84eb2c62d2e58f6fcabf3b611d3b4435cb1 Mon Sep 17 00:00:00 2001 From: ziebam Date: Sat, 19 Feb 2022 22:44:57 +0100 Subject: [PATCH 3/4] Add changelog entry --- changelog/7336.breaking.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/7336.breaking.rst diff --git a/changelog/7336.breaking.rst b/changelog/7336.breaking.rst new file mode 100644 index 00000000000..dc48bab92e5 --- /dev/null +++ b/changelog/7336.breaking.rst @@ -0,0 +1 @@ +The pytest ``log_file`` ini marker is now relative to the configs ``inifile`` directory, as it was always the intention. It was originally introduced as relative to the current working directory unintentionally. From 4b5589e1bb73de8aa4226927f7dfe8cad14443b3 Mon Sep 17 00:00:00 2001 From: ziebam Date: Sat, 19 Feb 2022 22:53:05 +0100 Subject: [PATCH 4/4] Change wording from 'cmd' to 'cli' --- src/_pytest/logging.py | 4 ++-- testing/logging/test_reporting.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 29a7d6a7317..e26991c7669 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -560,8 +560,8 @@ def __init__(self, config: Config) -> None: if not os.path.isdir(directory): os.makedirs(directory) - # Log file should be relative to cwd when passed as a cmd argument and - # relative to the config file otherwise. + # Log file should be relative to invocation location when passed as a cli argument + # and relative to the config file otherwise. is_inifile_argument = "--log-file" not in config.invocation_params.args if config.inipath is not None and is_inifile_argument: log_file = os.path.join(config.inipath.parent, log_file) diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index e55a64f8cf8..736ec3a5bde 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1221,7 +1221,7 @@ def test_this(): assert "tox.ini" not in files_in_subdir -def test_log_file_relative_to_invocation_dir_when_passed_as_cmd_argument( +def test_log_file_relative_to_invocation_dir_when_passed_as_cli_argument( pytester, monkeypatch ): """PR #7350 comment related to issue #7350 (https://github.com/pytest-dev/pytest/pull/7350#pullrequestreview-429803796).