diff --git a/pyproject.toml b/pyproject.toml index 3ad4b05..773e87b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,9 @@ Repository = "https://github.com/milla-jovovich/mempalace" [project.scripts] mempalace = "mempalace:main" +[project.optional-dependencies] +dev = ["pytest>=7.0", "ruff>=0.4.0"] + [dependency-groups] dev = ["pytest>=7.0", "ruff>=0.4.0"] diff --git a/tests/conftest.py b/tests/conftest.py index acb9170..757ca51 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,12 +3,30 @@ conftest.py — Shared fixtures for MemPalace tests. Provides isolated palace and knowledge graph instances so tests never touch the user's real data or leak temp files on failure. + +HOME is redirected to a temp directory at module load time — before any +mempalace imports — so that module-level initialisations (e.g. +``_kg = KnowledgeGraph()`` in mcp_server) write to a throwaway location +instead of the real user profile. """ import os import shutil import tempfile +# ── Isolate HOME before any mempalace imports ────────────────────────── +_original_env = {} +_session_tmp = tempfile.mkdtemp(prefix="mempalace_session_") + +for _var in ("HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH"): + _original_env[_var] = os.environ.get(_var) + +os.environ["HOME"] = _session_tmp +os.environ["USERPROFILE"] = _session_tmp +os.environ["HOMEDRIVE"] = os.path.splitdrive(_session_tmp)[0] or "C:" +os.environ["HOMEPATH"] = os.path.splitdrive(_session_tmp)[1] or _session_tmp + +# Now it is safe to import mempalace modules that trigger initialisation. import chromadb import pytest @@ -16,6 +34,23 @@ from mempalace.config import MempalaceConfig from mempalace.knowledge_graph import KnowledgeGraph +@pytest.fixture(scope="session", autouse=True) +def _isolate_home(tmp_path_factory): + """Ensure HOME points to a temp dir for the entire test session. + + The env vars were already set at module level (above) so that + module-level initialisations are captured. This fixture simply + restores the originals on teardown and cleans up the temp dir. + """ + yield + for var, orig in _original_env.items(): + if orig is None: + os.environ.pop(var, None) + else: + os.environ[var] = orig + shutil.rmtree(_session_tmp, ignore_errors=True) + + @pytest.fixture def tmp_dir(): """Create and auto-cleanup a temporary directory.""" diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index af72410..c6d8a3a 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -8,13 +8,10 @@ via monkeypatch to avoid touching real data. import json -import pytest - def _patch_mcp_server(monkeypatch, config, palace_path, kg): """Patch the mcp_server module globals to use test fixtures.""" from mempalace import mcp_server - import chromadb monkeypatch.setattr(mcp_server, "_config", config) monkeypatch.setattr(mcp_server, "_kg", kg) diff --git a/tests/test_searcher.py b/tests/test_searcher.py index 06dcb5c..6750162 100644 --- a/tests/test_searcher.py +++ b/tests/test_searcher.py @@ -4,8 +4,6 @@ test_searcher.py — Tests for the programmatic search_memories API. Tests the library-facing search interface (not the CLI print variant). """ -import chromadb - from mempalace.searcher import search_memories