From 4e69570481f11f0f289567347b59fdb724f7ec66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 27 Oct 2026 10:28:18 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20test:=20keep=20inherited=20PYTHO?= =?UTF-8?q?NPATH=20in=20native=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The unset scenarios cleared os.environ before copying PYTHONPATH and PYTHONDONTWRITEBYTECODE, so the subprocess lost both. Downstream builds that find pytest_env only through PYTHONPATH failed 12 cases, as reported in https://github.com/pytest-dev/pytest-env/pull/275#issuecomment-6436801643. Pass the caller's values through and add the marker only in the set scenarios. --- tests/test_env.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_env.py b/tests/test_env.py index eb883b5..b906ead 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -806,13 +806,16 @@ def test_native_env_files_override_ini( (pytester.path / config_name).write_text(native, encoding="utf-8") pass_python_env: Final[bool] = scenario[1] pythonpath_marker: Final[str] = str(pytester.path / "pythonpath_marker") + inherited_env: Final[dict[str, str]] = { + key: os.environ[key] for key in ("PYTHONPATH", "PYTHONDONTWRITEBYTECODE") if key in os.environ + } python_env: Final[dict[str, str]] = ( { - "PYTHONPATH": os.pathsep.join(filter(None, (os.environ.get("PYTHONPATH"), pythonpath_marker))), + "PYTHONPATH": os.pathsep.join(filter(None, (inherited_env.get("PYTHONPATH"), pythonpath_marker))), "PYTHONDONTWRITEBYTECODE": "1", } if pass_python_env - else {} + else inherited_env ) extend: Final[bool] = scenario[0] expected_env: Final[tuple[str | None, str | None, bool, str | None]] = ( @@ -835,12 +838,11 @@ def test_env() -> None: """), ) system_env: Final[dict[str, str]] = {key: os.environ[key] for key in ("SYSTEMROOT", "WINDIR") if key in os.environ} - mocker.patch.dict(os.environ, python_env, clear=True) mocker.patch.dict( os.environ, { **system_env, - **{key: os.environ[key] for key in ("PYTHONPATH", "PYTHONDONTWRITEBYTECODE") if key in os.environ}, + **python_env, "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin", },