From b65006fa99673235b5b532590894c84d928931f9 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Beasley" Date: Sun, 18 Oct 2026 09:17:49 +0100 Subject: [PATCH 1/3] Propagate PYTHONPATH in test_native_env_files_override_ini This allows the test to work in environments where `PYTHONPATH` is needed to find the `pytest_env` plugin, such as downstream in Fedora. --- tests/test_env.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_env.py b/tests/test_env.py index 20423b6..0e8ed59 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -807,6 +807,13 @@ def test_env() -> None: assert (os.environ.get("INHERITED"), os.environ.get("FROM_CLI")) == {expected_env!r} """), ) - new_env: Final[dict[str, str]] = {"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin"} + env: dict[str, str] = {} + if "PYTHONPATH" in os.environ: + env["PYTHONPATH"] = os.environ["PYTHONPATH"] + new_env: Final[dict[str, str]] = { + **env, + "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", + "PYTEST_PLUGINS": "pytest_env.plugin", + } with mock.patch.dict(os.environ, new_env, clear=True): pytester.runpytest_subprocess(*(["--envfile", "+cli.env"] if extend else [])).assert_outcomes(passed=1) From 5aea2f97577f8fc7f31e72494493ac090429550b Mon Sep 17 00:00:00 2001 From: "Benjamin A. Beasley" Date: Sun, 18 Oct 2026 09:35:21 +0100 Subject: [PATCH 2/3] Propagate PYTHONDONTWRITEBYTECODE in test_native_env_files_override_ini This is useful for downstream testing where it may be desirable not to leave `.pyc` files lying around. --- tests/test_env.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_env.py b/tests/test_env.py index 0e8ed59..af3eb17 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -807,9 +807,8 @@ def test_env() -> None: assert (os.environ.get("INHERITED"), os.environ.get("FROM_CLI")) == {expected_env!r} """), ) - env: dict[str, str] = {} - if "PYTHONPATH" in os.environ: - env["PYTHONPATH"] = os.environ["PYTHONPATH"] + passthrough: Final[list[str]] = ["PYTHONPATH", "PYTHONDONTWRITEBYTECODE"] + env: Final[dict[str, str]] = {var: os.environ[var] for var in passthrough if var in os.environ} new_env: Final[dict[str, str]] = { **env, "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", From c2e119d92e45e72e760da4412b42d2030c3e9bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sun, 18 Oct 2026 08:40:56 -0700 Subject: [PATCH 3/3] test: verify subprocess env inheritance Assert that Python import and bytecode settings reach the child process. Keep Windows system paths so a clean environment can start Python. --- tests/test_env.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/tests/test_env.py b/tests/test_env.py index af3eb17..82455cf 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -782,8 +782,9 @@ def child(pytester: pytest.Pytester, mocker: MockerFixture) -> Path: "extend", [pytest.param(False, id="config-only"), pytest.param(True, id="cli-extend")], ) +@pytest.mark.parametrize("pass_python_env", [pytest.param(False, id="unset"), pytest.param(True, id="set")]) def test_native_env_files_override_ini( - pytester: pytest.Pytester, config_name: str, empty_env_files: bool, extend: bool + pytester: pytest.Pytester, config_name: str, empty_env_files: bool, extend: bool, pass_python_env: bool ) -> None: (pytester.path / "fallback.env").write_text("INHERITED=from_ini\n", encoding="utf-8") (pytester.path / "cli.env").write_text("FROM_CLI=from_cli\n", encoding="utf-8") @@ -799,20 +800,34 @@ def test_native_env_files_override_ini( None if empty_env_files else "from_ini", "from_cli" if extend else None, ) + pythonpath_marker: Final[str] = str(pytester.path / "pythonpath_marker") + python_env: Final[dict[str, str]] = ( + { + "PYTHONPATH": os.pathsep.join(filter(None, (os.environ.get("PYTHONPATH"), pythonpath_marker))), + "PYTHONDONTWRITEBYTECODE": "1", + } + if pass_python_env + else {} + ) pytester.makepyfile( test_env=dedent(f"""\ import os def test_env() -> None: assert (os.environ.get("INHERITED"), os.environ.get("FROM_CLI")) == {expected_env!r} + assert ({pythonpath_marker!r} in os.environ["PYTHONPATH"].split(os.pathsep), + os.environ.get("PYTHONDONTWRITEBYTECODE")) == ( + {pass_python_env!r}, {python_env.get("PYTHONDONTWRITEBYTECODE")!r} + ) """), ) - passthrough: Final[list[str]] = ["PYTHONPATH", "PYTHONDONTWRITEBYTECODE"] - env: Final[dict[str, str]] = {var: os.environ[var] for var in passthrough if var in os.environ} - new_env: Final[dict[str, str]] = { - **env, - "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", - "PYTEST_PLUGINS": "pytest_env.plugin", - } - with mock.patch.dict(os.environ, new_env, clear=True): - pytester.runpytest_subprocess(*(["--envfile", "+cli.env"] if extend else [])).assert_outcomes(passed=1) + system_env: Final[dict[str, str]] = {key: os.environ[key] for key in ("SYSTEMROOT", "WINDIR") if key in os.environ} + with mock.patch.dict(os.environ, python_env, clear=True): + new_env: Final[dict[str, str]] = { + **system_env, + **{key: os.environ[key] for key in ("PYTHONPATH", "PYTHONDONTWRITEBYTECODE") if key in os.environ}, + "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", + "PYTEST_PLUGINS": "pytest_env.plugin", + } + with mock.patch.dict(os.environ, new_env, clear=True): + pytester.runpytest_subprocess(*(["--envfile", "+cli.env"] if extend else [])).assert_outcomes(passed=1)