test(hooks): isolate test_hooks_cli from test_cli side effect (#1510)

Nine save/log/precompact tests in test_hooks_cli.py passed only because
test_cli.py (alphabetically earlier) created ~/.mempalace in the session
tmp HOME as a side effect, satisfying the _palace_root_exists()
kill-switch. Run in isolation they short-circuited and failed (9 failed,
80 passed, 1 skipped).

Add a module autouse fixture that points PALACE_ROOT/STATE_DIR at a
per-test palace root that exists, so every test is robust standalone and
future tests don't inherit the trap. Kill-switch tests that need the
absent path call _redirect_palace_root after the fixture; monkeypatch
last-write-wins keeps their absent/file root and teardown restores the
real module value.

Isolation: pytest tests/test_hooks_cli.py -> 100 passed, 1 skipped.
Ordering preserved: test_cli + test_hooks_cli -> 165 passed.

Closes #1510
This commit is contained in:
Igor Lins e Silva 2026-05-18 17:28:50 -03:00
parent 34d8dde969
commit 426d164df4
1 changed files with 25 additions and 0 deletions

View File

@ -31,6 +31,31 @@ from mempalace.hooks_cli import (
)
@pytest.fixture(autouse=True)
def _isolated_existing_palace_root(monkeypatch, tmp_path):
"""Give every test an isolated, *existing* PALACE_ROOT/STATE_DIR.
Regression for #1510: nine save / log / precompact tests assumed
``~/.mempalace`` existed and only passed in the full suite because an
earlier test file (``test_cli.py``) created it as a side effect, so
the ``_palace_root_exists()`` kill-switch was satisfied. Run in
isolation they short-circuited and failed.
Defaulting every test to a per-test palace root that exists makes
them robust on their own and protects future tests from the same
trap. Tests that exercise the absent-root kill-switch path call
``_redirect_palace_root`` (or set their own PALACE_ROOT) *after* this
fixture; ``monkeypatch``'s last-write-wins means they keep their
absent/file root and teardown still restores the real module value.
"""
root = tmp_path / ".mempalace"
root.mkdir(exist_ok=True)
monkeypatch.setattr(hooks_cli_mod, "PALACE_ROOT", root)
monkeypatch.setattr(hooks_cli_mod, "STATE_DIR", root / "hook_state")
monkeypatch.setattr(hooks_cli_mod, "_state_dir_initialized", False)
return root
# --- _mempalace_python ---