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 <bruno@soliv.dev>
This commit is contained in:
dj 2024-04-09 19:49:11 +05:30 committed by GitHub
parent d6d59fed41
commit 2f8e436213
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 2 deletions

View File

@ -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:
if old_pytest_version is None:
os.environ.pop("PYTEST_VERSION", None)
else:
os.environ["PYTEST_VERSION"] = old_pytest_version
def console_main() -> int:

View File

@ -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"