diff --git a/tests/integrations/conftest.py b/tests/integrations/conftest.py index 467187235..4b7806c4a 100644 --- a/tests/integrations/conftest.py +++ b/tests/integrations/conftest.py @@ -5,10 +5,8 @@ import pytest from specify_cli.integrations.base import MarkdownIntegration -@pytest.fixture(autouse=True) -def _isolate_integration_home(monkeypatch: pytest.MonkeyPatch, tmp_path): - """Keep integration tests from reading or writing the real user home.""" - home = tmp_path / "home" +def _redirect_home(monkeypatch: pytest.MonkeyPatch, home) -> None: + """Point HOME/USERPROFILE/XDG env vars at an isolated *home* directory.""" for path in (home, home / ".cache", home / ".config", home / ".local" / "share"): path.mkdir(parents=True, exist_ok=True) @@ -19,6 +17,28 @@ def _isolate_integration_home(monkeypatch: pytest.MonkeyPatch, tmp_path): monkeypatch.setenv("XDG_DATA_HOME", str(home / ".local" / "share")) +@pytest.fixture(scope="session", autouse=True) +def _isolate_integration_home_session(tmp_path_factory): + """Isolate the user home for setup that runs outside a test function. + + The per-test fixture below re-points HOME for each test, but function-scoped + fixtures do not apply to module-/session-scoped fixtures. Some of those (e.g. + the ``status_*_template`` fixtures in ``test_integration_subcommand.py``) run + ``specify init`` during setup, before any per-test isolation takes effect. + A standalone ``MonkeyPatch`` gives them an isolated home too. + """ + monkeypatch = pytest.MonkeyPatch() + _redirect_home(monkeypatch, tmp_path_factory.mktemp("session-home")) + yield + monkeypatch.undo() + + +@pytest.fixture(autouse=True) +def _isolate_integration_home(monkeypatch: pytest.MonkeyPatch, tmp_path): + """Keep integration tests from reading or writing the real user home.""" + _redirect_home(monkeypatch, tmp_path / "home") + + class StubIntegration(MarkdownIntegration): """Minimal concrete integration for testing.""" diff --git a/tests/integrations/test_home_isolation.py b/tests/integrations/test_home_isolation.py index e79ff5dff..c862b1686 100644 --- a/tests/integrations/test_home_isolation.py +++ b/tests/integrations/test_home_isolation.py @@ -15,6 +15,10 @@ def test_integration_tests_use_tmp_home(tmp_path: Path) -> None: assert Path(os.environ["XDG_CONFIG_HOME"]) == home / ".config" assert Path(os.environ["XDG_DATA_HOME"]) == home / ".local" / "share" + # Most integrations resolve the user home via Path.home() (e.g. Hermes, + # catalog), so the isolation has to reach that API, not just the env vars. + assert Path.home() == home + assert home.is_dir() assert (home / ".cache").is_dir() assert (home / ".config").is_dir()