Skip to content

Personal AI, On Personal Devices

Python

test(conftest): isolate the whole suite from the real ~/.openjarvis home (#816)

havolline1c85b9be7440b
diff --git a/tests/agents/test_claude_code.py b/tests/agents/test_claude_code.py
index 6eabfa37..6597fe10 100644
--- a/tests/agents/test_claude_code.py
+++ b/tests/agents/test_claude_code.py
@@ -83,40 +83,46 @@ class TestEnsureRunner:
with pytest.raises(RuntimeError, match="Node.js"):
agent._ensure_runner()
- def test_creates_runner_dir(self, tmp_path):
+ def test_creates_runner_dir(self, tmp_path, monkeypatch):
engine = MagicMock()
engine.engine_id = "mock"
agent = ClaudeCodeAgent(engine, "test-model")
+ # _ensure_runner() resolves its destination via get_config_dir(),
+ # which checks $OPENJARVIS_HOME before ever falling back to
+ # Path.home() -- so that's the env var to control here, not a
+ # Path.home() patch (which get_config_dir() never even calls once
+ # OPENJARVIS_HOME is set, e.g. by conftest.py's session-wide
+ # isolation fixture).
home_dir = tmp_path / "home"
home_dir.mkdir()
+ monkeypatch.setenv("OPENJARVIS_HOME", str(home_dir))
with (
patch("shutil.which", return_value="/usr/bin/node"),
- patch("pathlib.Path.home", return_value=home_dir),
patch("subprocess.run") as mock_run,
):
mock_run.return_value = _mock_proc()
- dest = home_dir / ".openjarvis" / "claude_code_runner"
+ dest = home_dir / "claude_code_runner"
result = agent._ensure_runner()
assert result == dest
mock_run.assert_called_once()
call_args = mock_run.call_args
assert "npm" in call_args[0][0][0]
- def test_skips_npm_install_when_node_modules_exists(self, tmp_path):
+ def test_skips_npm_install_when_node_modules_exists(self, tmp_path, monkeypatch):
engine = MagicMock()
engine.engine_id = "mock"
agent = ClaudeCodeAgent(engine, "test-model")
home_dir = tmp_path / "home"
- dest = home_dir / ".openjarvis" / "claude_code_runner"
+ dest = home_dir / "claude_code_runner"
dest.mkdir(parents=True)
(dest / "node_modules").mkdir()
+ monkeypatch.setenv("OPENJARVIS_HOME", str(home_dir))
with (
patch("shutil.which", return_value="/usr/bin/node"),
- patch("pathlib.Path.home", return_value=home_dir),
patch("subprocess.run") as mock_run,
):
agent._ensure_runner()
diff --git a/tests/conftest.py b/tests/conftest.py
index c9d0bb7c..cf3a7e05 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -4,14 +4,36 @@ from __future__ import annotations
import json
import os
+import shutil
+import tempfile
from pathlib import Path
from unittest.mock import MagicMock
import pytest
-from openjarvis.core.config import GpuInfo, HardwareInfo
-from openjarvis.core.events import EventBus, reset_event_bus
-from openjarvis.core.registry import (
+# Set BEFORE importing openjarvis.core.config: DEFAULT_CONFIG_DIR /
+# DEFAULT_CONFIG_PATH are resolved once at import time (the install-script
+# model, where OPENJARVIS_HOME is set before the process starts). Without
+# this, the very first import of openjarvis.core.config in the session --
+# this file imports it below, and it's transitively imported by nearly
+# everything -- permanently binds those constants (and the ~45 modules that
+# reference them) to the developer's real ~/.openjarvis for the rest of the
+# run, so tests touching e.g. AgentConfigEvolver or LearningOrchestrator
+# read/write real files under the developer's home directory (#787).
+_MISSING = object()
+_ORIGINAL_OPENJARVIS_HOME = os.environ.get("OPENJARVIS_HOME", _MISSING)
+_ORIGINAL_OPENJARVIS_CONFIG = os.environ.get("OPENJARVIS_CONFIG", _MISSING)
+_TEST_HOME = Path(tempfile.mkdtemp(prefix="openjarvis-test-home-"))
+_TEST_HOME_CLEANED = False
+os.environ["OPENJARVIS_HOME"] = str(_TEST_HOME)
+# An explicit config path takes precedence inside load_config(). It may point
+# at a developer's real file even when OPENJARVIS_HOME is isolated, so remove
+# it before config.py and its import-time constants are initialized.
+os.environ.pop("OPENJARVIS_CONFIG", None)
+
+from openjarvis.core.config import GpuInfo, HardwareInfo, load_config # noqa: E402
+from openjarvis.core.events import EventBus, reset_event_bus # noqa: E402
+from openjarvis.core.registry import ( # noqa: E402
AgentRegistry,
BenchmarkRegistry,
ChannelRegistry,
@@ -30,6 +52,45 @@ from openjarvis.core.registry import (
)
+def _cleanup_test_home() -> None:
+ """Restore import-time process state exactly once.
+
+ A session fixture is not instantiated for ``--collect-only`` and may not
+ be instantiated when collection fails. Keep the cleanup in a normal
+ function so both fixture teardown and pytest's unconditional unconfigure
+ hook can call it safely.
+ """
+ global _TEST_HOME_CLEANED
+
+ if _TEST_HOME_CLEANED:
+ return
+ _TEST_HOME_CLEANED = True
+
+ load_config.cache_clear()
+ shutil.rmtree(_TEST_HOME, ignore_errors=True)
+
+ if _ORIGINAL_OPENJARVIS_HOME is _MISSING:
+ os.environ.pop("OPENJARVIS_HOME", None)
+ else:
+ os.environ["OPENJARVIS_HOME"] = str(_ORIGINAL_OPENJARVIS_HOME)
+ if _ORIGINAL_OPENJARVIS_CONFIG is _MISSING:
+ os.environ.pop("OPENJARVIS_CONFIG", None)
+ else:
+ os.environ["OPENJARVIS_CONFIG"] = str(_ORIGINAL_OPENJARVIS_CONFIG)
+
+
+def pytest_unconfigure(config: pytest.Config) -> None:
+ """Clean up even when pytest never starts the session fixture."""
+ _cleanup_test_home()
+
+
[email protected](scope="session", autouse=True)
+def _cleanup_session_test_home():
+ """Keep the import-time home alive through normal fixture teardown."""
+ yield
+ _cleanup_test_home()
+
+
@pytest.fixture(autouse=True)
def _no_update_check(monkeypatch: pytest.MonkeyPatch) -> None:
"""Never let the CLI's PyPI update-check nag run during tests.
@@ -43,6 +104,24 @@ def _no_update_check(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OPENJARVIS_NO_UPDATE_CHECK", "1")
[email protected](autouse=True)
+def _isolated_openjarvis_home(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Re-assert OPENJARVIS_HOME and clear load_config()'s cache per test.
+
+ Two related leaks (#787): (1) something changing OPENJARVIS_HOME
+ without going through monkeypatch, so it never gets reverted for later
+ tests -- re-asserting it here every test is a cheap defense regardless
+ of cause; (2) load_config()'s functools.lru_cache(maxsize=1) serving a
+ config computed under a *different* test's environment/monkeypatched
+ values instead of the current test's.
+ """
+ monkeypatch.setenv("OPENJARVIS_HOME", str(_TEST_HOME))
+ monkeypatch.delenv("OPENJARVIS_CONFIG", raising=False)
+ load_config.cache_clear()
+ yield
+ load_config.cache_clear()
+
+
@pytest.fixture(autouse=True)
def _clean_registries() -> None:
"""Ensure each test starts with empty registries and a fresh event bus."""
diff --git a/tests/core/test_config_paths.py b/tests/core/test_config_paths.py
index 8660bb1c..40fe972f 100644
--- a/tests/core/test_config_paths.py
+++ b/tests/core/test_config_paths.py
@@ -7,6 +7,7 @@ Covers the single-root consolidation: ``$OPENJARVIS_HOME`` >
from __future__ import annotations
+import os
from pathlib import Path
import pytest
@@ -174,3 +175,23 @@ class TestLegacyConstantsHonorEnv:
credentials._default_path()
== (tmp_path / "oj" / "credentials.toml").resolve()
)
+
+ def test_constant_never_resolves_to_the_real_developer_home(self) -> None:
+ """Regression for #787: DEFAULT_CONFIG_DIR is resolved once, at the
+ first import of openjarvis.core.config anywhere in the test
+ session -- which happens via tests/conftest.py, before any test
+ body runs. If conftest.py doesn't set OPENJARVIS_HOME before that
+ import, this constant (and the ~45 modules that reference it)
+ permanently binds to the developer's real ~/.openjarvis for the
+ rest of the run, so tests touching e.g. AgentConfigEvolver or
+ LearningOrchestrator read/write real files there.
+
+ No monkeypatching here on purpose: this checks the *actual*
+ session-wide value tests/conftest.py's import-time setup already
+ produced, not a value manufactured by this test.
+ """
+ from openjarvis.core.config import DEFAULT_CONFIG_DIR
+
+ real_home = (Path.home() / ".openjarvis").resolve()
+ assert DEFAULT_CONFIG_DIR != real_home
+ assert "OPENJARVIS_CONFIG" not in os.environ
diff --git a/tests/prompt/test_persona_scope.py b/tests/prompt/test_persona_scope.py
index 8bb6e78e..ec47747b 100644
--- a/tests/prompt/test_persona_scope.py
+++ b/tests/prompt/test_persona_scope.py
@@ -1,10 +1,9 @@
"""Tests for #380 per-invocation persona scope (_resolve_persona)."""
-from pathlib import Path
-
import pytest
from openjarvis.core.config import MemoryFilesConfig
+from openjarvis.core.paths import get_config_dir
from openjarvis.prompt.builder import SystemPromptBuilder
@@ -21,7 +20,7 @@ def test_none_persona_disables_all_files():
def test_named_persona_resolves_to_personas_dir():
out = SystemPromptBuilder._resolve_persona(MemoryFilesConfig(persona_name="coder"))
- base = str(Path.home() / ".openjarvis" / "personas" / "coder")
+ base = str(get_config_dir() / "personas" / "coder")
assert out.soul_path == f"{base}/SOUL.md"
assert out.memory_path == f"{base}/MEMORY.md"
assert out.user_path == f"{base}/USER.md"
diff --git a/tests/test_conftest_lifecycle.py b/tests/test_conftest_lifecycle.py
new file mode 100644
index 00000000..52aef67f
--- /dev/null
+++ b/tests/test_conftest_lifecycle.py
@@ -0,0 +1,67 @@
+"""Regression tests for the session-wide test-home lifecycle."""
+
+from __future__ import annotations
+
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+
[email protected]("mode", ["collect-only", "collection-error"])
+def test_non_running_sessions_restore_environment_and_remove_test_home(
+ mode: str,
+) -> None:
+ """Cleanup must run when fixtures never start or collection aborts."""
+ repo_root = Path(__file__).resolve().parents[1]
+ script = r"""
+import os
+import pathlib
+import sys
+import tempfile
+
+import pytest
+
+created = []
+real_mkdtemp = tempfile.mkdtemp
+
+def recording_mkdtemp(*args, **kwargs):
+ path = real_mkdtemp(*args, **kwargs)
+ if kwargs.get("prefix") == "openjarvis-test-home-":
+ created.append(path)
+ return path
+
+tempfile.mkdtemp = recording_mkdtemp
+os.environ["OPENJARVIS_HOME"] = "/tmp/openjarvis-original-home-sentinel"
+os.environ["OPENJARVIS_CONFIG"] = "/tmp/openjarvis-original-config-sentinel"
+
+bad_test = pathlib.Path("tests") / f"test_bad_collection_{os.getpid()}.py"
+try:
+ if sys.argv[1] == "collect-only":
+ result = pytest.main(
+ ["--collect-only", "-q", "tests/core/test_config_paths.py"]
+ )
+ assert result == pytest.ExitCode.OK, result
+ else:
+ bad_test.write_text("def broken(:\n", encoding="utf-8")
+ result = pytest.main(["--collect-only", "-q", str(bad_test)])
+ assert result != pytest.ExitCode.OK, result
+finally:
+ bad_test.unlink(missing_ok=True)
+
+assert os.environ["OPENJARVIS_HOME"] == "/tmp/openjarvis-original-home-sentinel"
+assert os.environ["OPENJARVIS_CONFIG"] == "/tmp/openjarvis-original-config-sentinel"
+assert len(created) == 1, created
+assert not pathlib.Path(created[0]).exists(), created[0]
+"""
+ result = subprocess.run(
+ [sys.executable, "-c", script, mode],
+ cwd=repo_root,
+ capture_output=True,
+ text=True,
+ timeout=60,
+ check=False,
+ )
+
+ assert result.returncode == 0, result.stdout + result.stderr