Skip to content

Commit 5b57509

Browse files
fix(tests): isolate LocalFileStorage session dir from the real home dir (#16)
test_mcp_server.py constructs LocalFileStorage() with no storage_dir, which defaulted to the developer's real ~/.attune-help/sessions/ — so running the suite leaked session files there (it had accumulated 800+). attune-help's conftest had no isolation for it and the default path was hardcoded inline (not overridable). - storage.py: make the default dir overridable via ATTUNE_HELP_SESSIONS_DIR (new _default_storage_dir helper). - conftest.py: autouse fixture points it at a tmp dir for the whole suite. Verified: real sessions count is unchanged across a full 338-test run (previously grew every run). Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent c8305de commit 5b57509

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/attune_help/storage.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import json
1111
import logging
12+
import os
1213
import time
1314
from pathlib import Path
1415
from typing import Any, Protocol
@@ -17,6 +18,16 @@
1718

1819
_SESSION_TTL_SECONDS = 4 * 3600 # 4 hours
1920

21+
# Default on-disk location for session files. Overridable via the
22+
# ``ATTUNE_HELP_SESSIONS_DIR`` env var so tests/CI never touch the
23+
# developer's real ``~/.attune-help/sessions/``.
24+
_DEFAULT_SESSIONS_DIR = "~/.attune-help/sessions"
25+
26+
27+
def _default_storage_dir() -> Path:
28+
"""Resolve the default session directory (env-overridable)."""
29+
return Path(os.environ.get("ATTUNE_HELP_SESSIONS_DIR", _DEFAULT_SESSIONS_DIR)).expanduser()
30+
2031

2132
class SessionStorage(Protocol):
2233
"""Protocol for session state backends.
@@ -161,7 +172,8 @@ class LocalFileStorage:
161172
162173
Args:
163174
storage_dir: Directory for session files.
164-
Defaults to ~/.attune-help/sessions/.
175+
Defaults to ~/.attune-help/sessions/ (overridable via the
176+
``ATTUNE_HELP_SESSIONS_DIR`` env var).
165177
ttl_seconds: Session time-to-live in seconds.
166178
Defaults to 4 hours.
167179
"""
@@ -172,7 +184,7 @@ def __init__(
172184
ttl_seconds: int = _SESSION_TTL_SECONDS,
173185
) -> None:
174186
if storage_dir is None:
175-
self._dir = Path("~/.attune-help/sessions").expanduser()
187+
self._dir = _default_storage_dir()
176188
else:
177189
self._dir = Path(storage_dir)
178190
self._ttl = ttl_seconds

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
PYPROJECT = REPO_ROOT / "pyproject.toml"
1212

1313

14+
@pytest.fixture(autouse=True)
15+
def _isolated_sessions_dir(tmp_path, monkeypatch):
16+
"""Redirect LocalFileStorage's default session dir to a tmp path.
17+
18+
Without this, tests that construct ``LocalFileStorage()`` with no
19+
``storage_dir`` (e.g. test_mcp_server.py) read from / write to the
20+
developer's real ``~/.attune-help/sessions/``.
21+
"""
22+
monkeypatch.setenv("ATTUNE_HELP_SESSIONS_DIR", str(tmp_path / "sessions"))
23+
24+
1425
@pytest.fixture
1526
def plugin_root() -> Path:
1627
"""Absolute path to the plugin directory."""

0 commit comments

Comments
 (0)