Skip to content

Commit 33d2c38

Browse files
test: home-dir leak guard for ~/.attune-help/sessions/ (#17)
Session-scoped autouse alarm: fails the suite if any test wrote to the real ~/.attune-help/sessions/ instead of a tmp dir — the regression guard for the isolation fix in #16. Verified: full suite green (no false positive); a planted real-dir write trips it and names the file. specs/test-isolation-guard (layer 1 of 3). Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 5b57509 commit 33d2c38

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@
1010
PLUGIN_ROOT = REPO_ROOT / "plugin"
1111
PYPROJECT = REPO_ROOT / "pyproject.toml"
1212

13+
# Real home-dir state this layer's tests must never touch (they isolate to a
14+
# tmp dir instead). The guard below is a regression alarm — see
15+
# specs/test-isolation-guard/ and the workspace testing-conventions.md.
16+
_GUARDED_PATHS = [Path.home() / ".attune-help" / "sessions"]
17+
18+
19+
def _home_snapshot(p: Path):
20+
"""Comparable snapshot: dir → child names; file → mtime; absent → None."""
21+
if p.is_dir():
22+
return frozenset(c.name for c in p.iterdir())
23+
if p.exists():
24+
return ("file", p.stat().st_mtime)
25+
return None
26+
27+
28+
@pytest.fixture(scope="session", autouse=True)
29+
def _guard_real_home_state():
30+
"""Fail the run if any test wrote to real home-dir state (missing isolation)."""
31+
before = {p: _home_snapshot(p) for p in _GUARDED_PATHS}
32+
yield
33+
leaked = []
34+
for p in _GUARDED_PATHS:
35+
b, a = before[p], _home_snapshot(p)
36+
if isinstance(b, frozenset) or isinstance(a, frozenset):
37+
new = (a or frozenset()) - (b or frozenset())
38+
if new:
39+
leaked.append(f"{p}: new entries {sorted(new)[:5]}")
40+
elif b != a:
41+
leaked.append(f"{p}: created or modified")
42+
assert not leaked, (
43+
"Tests wrote to real home-dir state (missing isolation):\n "
44+
+ "\n ".join(leaked)
45+
+ "\nIsolate via the tmp-dir fixture — see testing-conventions.md."
46+
)
47+
1348

1449
@pytest.fixture(autouse=True)
1550
def _isolated_sessions_dir(tmp_path, monkeypatch):

0 commit comments

Comments
 (0)