From 2f8e436213954a0f24839251b727bba65025a2c8 Mon Sep 17 00:00:00 2001 From: dj <112573278+dheerajck@users.noreply.github.com> Date: Tue, 9 Apr 2024 19:49:11 +0530 Subject: [PATCH] Cleaning up, if there was a value set for PYTEST_VERSION before pytest run by the calling process, we set it back after pytest run Co-authored-by: Bruno Oliveira --- src/_pytest/config/__init__.py | 6 +++++- testing/test_runner.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 3140e1c0f..6a8ac84ce 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -153,6 +153,7 @@ def main( :returns: An exit code. """ try: + old_pytest_version = os.environ.get("PYTEST_VERSION") os.environ["PYTEST_VERSION"] = __version__ try: config = _prepareconfig(args, plugins) @@ -189,7 +190,10 @@ def main( tw.line(f"ERROR: {msg}\n", red=True) return ExitCode.USAGE_ERROR finally: - os.environ.pop("PYTEST_VERSION", None) + if old_pytest_version is None: + os.environ.pop("PYTEST_VERSION", None) + else: + os.environ["PYTEST_VERSION"] = old_pytest_version def console_main() -> int: diff --git a/testing/test_runner.py b/testing/test_runner.py index c4fb85a7e..d3ddc5d9a 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -1097,6 +1097,7 @@ def test_outcome_exception_bad_msg() -> None: def test_pytest_version_env_var(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + os.environ["PYTEST_VERSION"] = "old version" pytester.makepyfile( """ import pytest @@ -1109,4 +1110,4 @@ def test_pytest_version_env_var(pytester: Pytester, monkeypatch: MonkeyPatch) -> ) result = pytester.runpytest_inprocess() assert result.ret == ExitCode.OK - assert "PYTEST_VERSION" not in os.environ + assert os.environ["PYTEST_VERSION"] == "old version"