From b9b160d358ea08387ac8043ea4a40bd893b55d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 13 Oct 2024 13:33:57 +0200 Subject: [PATCH] fix test pollution due to changing `os.environ` --- poetry.lock | 2 +- tests/conftest.py | 15 +++++++++++++++ tests/helpers.py | 22 ++++++++++++++++++++++ tests/test_helpers.py | 25 +++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/test_helpers.py diff --git a/poetry.lock b/poetry.lock index 6971d7e..ab873e7 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1003,7 +1003,7 @@ xattr = {version = "^1.0.0", markers = "sys_platform == \"darwin\""} type = "git" url = "https://github.com/Secrus/poetry.git" reference = "remove-shell" -resolved_reference = "59612d9501d2ad430020c54bb629cb6c73335f07" +resolved_reference = "d7694ed88f69aa3e5b93c6492f3cb3e25654efb8" [[package]] name = "poetry-core" diff --git a/tests/conftest.py b/tests/conftest.py index d203469..1c004cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import sys from pathlib import Path @@ -24,9 +25,12 @@ from tests.helpers import PoetryTestApplication from tests.helpers import TestExecutor from tests.helpers import TestLocker +from tests.helpers import isolated_environment if TYPE_CHECKING: + from collections.abc import Iterator + from poetry.installation.executor import Executor from poetry.poetry import Poetry from poetry.utils.env import Env @@ -102,6 +106,17 @@ def config( return c +@pytest.fixture(autouse=True) +def isolate_environ() -> Iterator[None]: + """Ensure the environment is isolated from user configuration.""" + with isolated_environment(): + for var in os.environ: + if var.startswith("POETRY_") or var in {"PYTHONPATH", "VIRTUAL_ENV"}: + del os.environ[var] + + yield + + @pytest.fixture def fixture_root() -> Path: return Path(__file__).parent / "fixtures" diff --git a/tests/helpers.py b/tests/helpers.py index 92a1e1f..e401b48 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,5 +1,8 @@ from __future__ import annotations +import contextlib +import os + from typing import TYPE_CHECKING from poetry.console.application import Application @@ -9,6 +12,7 @@ if TYPE_CHECKING: + from collections.abc import Iterator from pathlib import Path from typing import Any @@ -107,3 +111,21 @@ def _write_lock_data(self, data: TOMLDocument) -> None: return self._lock_data = data + + +@contextlib.contextmanager +def isolated_environment( + environ: dict[str, Any] | None = None, clear: bool = False +) -> Iterator[None]: + original_environ = dict(os.environ) + + if clear: + os.environ.clear() + + if environ: + os.environ.update(environ) + + yield + + os.environ.clear() + os.environ.update(original_environ) diff --git a/tests/test_helpers.py b/tests/test_helpers.py new file mode 100644 index 0000000..c554e55 --- /dev/null +++ b/tests/test_helpers.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +import os + +from tests.helpers import isolated_environment + + +def test_isolated_environment_restores_original_environ() -> None: + original_environ = dict(os.environ) + with isolated_environment(): + os.environ["TEST_VAR"] = "test" + assert os.environ == original_environ + + +def test_isolated_environment_clears_environ() -> None: + os.environ["TEST_VAR"] = "test" + with isolated_environment(clear=True): + assert "TEST_VAR" not in os.environ + assert "TEST_VAR" in os.environ + + +def test_isolated_environment_updates_environ() -> None: + with isolated_environment(environ={"NEW_VAR": "new_value"}): + assert os.environ["NEW_VAR"] == "new_value" + assert "NEW_VAR" not in os.environ