Skip to content

Commit 643f73a

Browse files
authored
test: isolate integration test home (#3144)
* test: isolate integration test home Assisted-by: Codex (model: GPT-5, autonomous) * test: assert integration home isolation Assisted-by: Codex (model: GPT-5, autonomous) * test: extend integration home isolation to module-scoped setup Address Copilot review on #3144. Add a session-scoped autouse fixture so HOME/USERPROFILE/XDG are redirected for setup that runs outside a test function (e.g. the module-scoped status_* fixtures in test_integration_subcommand.py that run `specify init` before any per-test isolation applies). The function-scoped fixture still overrides HOME per test. Also assert Path.home() resolves to the isolated home, since most integrations (Hermes, catalog) read the home via that API rather than the env vars directly.
1 parent a7b4391 commit 643f73a

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

tests/integrations/conftest.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
from specify_cli.integrations.base import MarkdownIntegration
66

77

8-
@pytest.fixture(autouse=True)
9-
def _isolate_integration_home(monkeypatch: pytest.MonkeyPatch, tmp_path):
10-
"""Keep integration tests from reading or writing the real user home."""
11-
home = tmp_path / "home"
8+
def _redirect_home(monkeypatch: pytest.MonkeyPatch, home) -> None:
9+
"""Point HOME/USERPROFILE/XDG env vars at an isolated *home* directory."""
1210
for path in (home, home / ".cache", home / ".config", home / ".local" / "share"):
1311
path.mkdir(parents=True, exist_ok=True)
1412

@@ -19,6 +17,28 @@ def _isolate_integration_home(monkeypatch: pytest.MonkeyPatch, tmp_path):
1917
monkeypatch.setenv("XDG_DATA_HOME", str(home / ".local" / "share"))
2018

2119

20+
@pytest.fixture(scope="session", autouse=True)
21+
def _isolate_integration_home_session(tmp_path_factory):
22+
"""Isolate the user home for setup that runs outside a test function.
23+
24+
The per-test fixture below re-points HOME for each test, but function-scoped
25+
fixtures do not apply to module-/session-scoped fixtures. Some of those (e.g.
26+
the ``status_*_template`` fixtures in ``test_integration_subcommand.py``) run
27+
``specify init`` during setup, before any per-test isolation takes effect.
28+
A standalone ``MonkeyPatch`` gives them an isolated home too.
29+
"""
30+
monkeypatch = pytest.MonkeyPatch()
31+
_redirect_home(monkeypatch, tmp_path_factory.mktemp("session-home"))
32+
yield
33+
monkeypatch.undo()
34+
35+
36+
@pytest.fixture(autouse=True)
37+
def _isolate_integration_home(monkeypatch: pytest.MonkeyPatch, tmp_path):
38+
"""Keep integration tests from reading or writing the real user home."""
39+
_redirect_home(monkeypatch, tmp_path / "home")
40+
41+
2242
class StubIntegration(MarkdownIntegration):
2343
"""Minimal concrete integration for testing."""
2444

tests/integrations/test_home_isolation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def test_integration_tests_use_tmp_home(tmp_path: Path) -> None:
1515
assert Path(os.environ["XDG_CONFIG_HOME"]) == home / ".config"
1616
assert Path(os.environ["XDG_DATA_HOME"]) == home / ".local" / "share"
1717

18+
# Most integrations resolve the user home via Path.home() (e.g. Hermes,
19+
# catalog), so the isolation has to reach that API, not just the env vars.
20+
assert Path.home() == home
21+
1822
assert home.is_dir()
1923
assert (home / ".cache").is_dir()
2024
assert (home / ".config").is_dir()

0 commit comments

Comments
 (0)