fix: address Copilot review — remove unused imports, isolate HOME in tests, restore dev extra

This commit is contained in:
Igor Lins e Silva
2026-04-07 17:17:57 -03:00
parent 72c548b729
commit cd8b245fdc
4 changed files with 38 additions and 5 deletions
+3
View File
@@ -37,6 +37,9 @@ Repository = "https://github.com/milla-jovovich/mempalace"
[project.scripts] [project.scripts]
mempalace = "mempalace:main" mempalace = "mempalace:main"
[project.optional-dependencies]
dev = ["pytest>=7.0", "ruff>=0.4.0"]
[dependency-groups] [dependency-groups]
dev = ["pytest>=7.0", "ruff>=0.4.0"] dev = ["pytest>=7.0", "ruff>=0.4.0"]
+35
View File
@@ -3,12 +3,30 @@ conftest.py — Shared fixtures for MemPalace tests.
Provides isolated palace and knowledge graph instances so tests never Provides isolated palace and knowledge graph instances so tests never
touch the user's real data or leak temp files on failure. 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 os
import shutil import shutil
import tempfile 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 chromadb
import pytest import pytest
@@ -16,6 +34,23 @@ from mempalace.config import MempalaceConfig
from mempalace.knowledge_graph import KnowledgeGraph 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 @pytest.fixture
def tmp_dir(): def tmp_dir():
"""Create and auto-cleanup a temporary directory.""" """Create and auto-cleanup a temporary directory."""
-3
View File
@@ -8,13 +8,10 @@ via monkeypatch to avoid touching real data.
import json import json
import pytest
def _patch_mcp_server(monkeypatch, config, palace_path, kg): def _patch_mcp_server(monkeypatch, config, palace_path, kg):
"""Patch the mcp_server module globals to use test fixtures.""" """Patch the mcp_server module globals to use test fixtures."""
from mempalace import mcp_server from mempalace import mcp_server
import chromadb
monkeypatch.setattr(mcp_server, "_config", config) monkeypatch.setattr(mcp_server, "_config", config)
monkeypatch.setattr(mcp_server, "_kg", kg) monkeypatch.setattr(mcp_server, "_kg", kg)
-2
View File
@@ -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). Tests the library-facing search interface (not the CLI print variant).
""" """
import chromadb
from mempalace.searcher import search_memories from mempalace.searcher import search_memories