diff --git a/tests/integrations/conftest.py b/tests/integrations/conftest.py index 833e272b27..4671872356 100644 --- a/tests/integrations/conftest.py +++ b/tests/integrations/conftest.py @@ -1,8 +1,24 @@ """Shared test helpers for integration tests.""" +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" + for path in (home, home / ".cache", home / ".config", home / ".local" / "share"): + path.mkdir(parents=True, exist_ok=True) + + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("USERPROFILE", str(home)) + monkeypatch.setenv("XDG_CACHE_HOME", str(home / ".cache")) + monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config")) + monkeypatch.setenv("XDG_DATA_HOME", str(home / ".local" / "share")) + + class StubIntegration(MarkdownIntegration): """Minimal concrete integration for testing.""" diff --git a/tests/integrations/test_home_isolation.py b/tests/integrations/test_home_isolation.py new file mode 100644 index 0000000000..e79ff5dff6 --- /dev/null +++ b/tests/integrations/test_home_isolation.py @@ -0,0 +1,21 @@ +"""Regression tests for integration-test environment isolation.""" + +from __future__ import annotations + +import os +from pathlib import Path + + +def test_integration_tests_use_tmp_home(tmp_path: Path) -> None: + home = tmp_path / "home" + + assert Path(os.environ["HOME"]) == home + assert Path(os.environ["USERPROFILE"]) == home + assert Path(os.environ["XDG_CACHE_HOME"]) == home / ".cache" + assert Path(os.environ["XDG_CONFIG_HOME"]) == home / ".config" + assert Path(os.environ["XDG_DATA_HOME"]) == home / ".local" / "share" + + assert home.is_dir() + assert (home / ".cache").is_dir() + assert (home / ".config").is_dir() + assert (home / ".local" / "share").is_dir()