Merge pull request #1547 from MemPalace/fix/1510-hooks-cli-test-isolation

test(hooks): isolate test_hooks_cli from test_cli side effect (#1510)
This commit is contained in:
Igor Lins e Silva
2026-05-18 17:46:52 -03:00
committed by GitHub
+33
View File
@@ -31,6 +31,39 @@ 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. ``_MINE_PID_DIR`` is patched too: it is derived from
``STATE_DIR`` *at module import* (hooks_cli.py:277), so patching
``STATE_DIR`` alone would leave mine-spawning tests writing PID files
under the import-time location instead of the per-test root. The
state dir is created so the docstring's "existing" promise holds.
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"
state_dir = root / "hook_state"
state_dir.mkdir(parents=True, exist_ok=True)
monkeypatch.setattr(hooks_cli_mod, "PALACE_ROOT", root)
monkeypatch.setattr(hooks_cli_mod, "STATE_DIR", state_dir)
monkeypatch.setattr(hooks_cli_mod, "_MINE_PID_DIR", state_dir / "mine_pids")
monkeypatch.setattr(hooks_cli_mod, "_state_dir_initialized", False)
return root
# --- _mempalace_python ---