fix: address Copilot review — remove unused imports, isolate HOME in tests, restore dev extra
This commit is contained in:
@@ -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."""
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user