Skip to content

Commit

Permalink
fix test pollution due to changing os.environ (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering authored Oct 13, 2024
1 parent 10c8a6f commit 6f3d55b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import sys

from pathlib import Path
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

import contextlib
import os

from typing import TYPE_CHECKING

from poetry.console.application import Application
Expand All @@ -9,6 +12,7 @@


if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -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)
25 changes: 25 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6f3d55b

Please sign in to comment.