Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(tests): keep the test suite from rewriting or adding files in the checkout (#1148)

Ruben Lütke8af159bb41da
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 35be80354a..24f7b18210 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -509,6 +509,8 @@ jobs:
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: ${{ needs.detect.outputs.duration_snapshot }}
+ - name: Snapshot the checkout before the tests
+ run: python scripts/ci/checkout_guard.py snapshot --out "$RUNNER_TEMP/checkout-before.json"
- name: Run shard
continue-on-error: true
env:
@@ -535,6 +537,10 @@ jobs:
find data | head -50
exit 1
fi
+ - name: The suite left the checkout as it found it
+ # A tracked file the tests rewrote (scripts/config-soll.json) or a file # i18n-allow: filename
+ # they left behind (report.xml) fails the shard; out/ holds its reports.
+ run: python scripts/ci/checkout_guard.py check --snapshot "$RUNNER_TEMP/checkout-before.json" --ignore out/
- name: Upload shard report
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
@@ -573,6 +579,8 @@ jobs:
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: ${{ needs.detect.outputs.duration_snapshot }}
+ - name: Snapshot the checkout before the tests
+ run: python scripts/ci/checkout_guard.py snapshot --out "$RUNNER_TEMP/checkout-before.json"
- name: Run shard
continue-on-error: true
env:
@@ -600,6 +608,10 @@ jobs:
find data | head -50
exit 1
fi
+ - name: The suite left the checkout as it found it
+ # A tracked file the tests rewrote (scripts/config-soll.json) or a file # i18n-allow: filename
+ # they left behind (report.xml) fails the shard; out/ holds its reports.
+ run: python scripts/ci/checkout_guard.py check --snapshot "$RUNNER_TEMP/checkout-before.json" --ignore out/
- name: Upload shard report
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
diff --git a/dodaios/core/config_writer.py b/dodaios/core/config_writer.py
index 2ee9222e4d..a243ce99c2 100644
--- a/dodaios/core/config_writer.py
+++ b/dodaios/core/config_writer.py
@@ -3322,6 +3322,10 @@ def _replace_with_retry(
# ----------------------------------------------------------------------
+#: Overrides the drift baseline path (tests point it at a temp copy).
+DRIFT_BASELINE_FILE_ENV = "JARVIS_DRIFT_BASELINE_FILE"
+
+
def _config_soll_path() -> Path: # i18n-allow
"""Locate ``scripts/config-soll.json`` relative to the repo root. # i18n-allow
@@ -3329,7 +3333,14 @@ def _config_soll_path() -> Path: # i18n-allow
``DEFAULT_CONFIG_FILE`` so the two paths stay consistent. On a headless
Linux VPS this file usually does not exist — callers must treat a missing
file as a graceful no-op.
+
+ ``JARVIS_DRIFT_BASELINE_FILE`` points it elsewhere: the test suite sets it to
+ a temp copy so a provider switch under test never rewrites the tracked
+ file. Unset (every real run), the path is the checkout's file.
"""
+ override = os.environ.get(DRIFT_BASELINE_FILE_ENV, "").strip()
+ if override:
+ return Path(override)
return PROJECT_ROOT / "scripts" / "config-soll.json" # i18n-allow
diff --git a/scripts/ci/checkout_guard.py b/scripts/ci/checkout_guard.py
new file mode 100644
index 0000000000..eedde4bd09
--- /dev/null
+++ b/scripts/ci/checkout_guard.py
@@ -0,0 +1,174 @@
+#!/usr/bin/env python3
+"""Prove that a test run left the checkout as it found it.
+
+A test run must never rewrite a tracked file (the drift baseline under
+``scripts/`` was flipped to another brain provider by a config-writer test) or
+leave a new file behind (the parallel runner wrote ``report.xml`` into the
+working directory).
+
+The guard compares two snapshots of ``git status``. A snapshot maps every path
+that is modified or untracked to its status, size and modification time, so a
+path that turns dirty, appears, or is rewritten again while already dirty all
+count as a change. Paths that were dirty before the run and stay untouched do
+not, so a developer's own edits never fail it.
+
+Used by:
+
+* ``tests/conftest.py``: a pytest session that rewrites a tracked file fails.
+ Untracked files are not judged there, because sibling processes of the
+ parallel runner create short-lived probe files of their own.
+* ``scripts/ci/run_tests_parallel.py``: the whole run, after every process has
+ ended, must leave no new or changed path outside its own outputs.
+* CI (``.github/workflows/ci.yml``): ``snapshot`` before the shard,
+ ``check`` after it.
+
+Commands::
+
+ python scripts/ci/checkout_guard.py snapshot --out before.json
+ python scripts/ci/checkout_guard.py check --snapshot before.json --ignore out/
+"""
+
+from __future__ import annotations
+
+import argparse
+import json
+import subprocess
+import sys
+from collections.abc import Iterable
+from pathlib import Path
+
+REPO_ROOT = Path(__file__).resolve().parents[2]
+
+Snapshot = dict[str, str]
+
+
+def snapshot(root: Path | None = None, *, untracked: bool = True) -> Snapshot | None:
+ """Return ``{path: "status:size:mtime"}`` for every dirty path, or None.
+
+ None means the state cannot be read (no git, not a checkout); callers then
+ skip the check instead of failing on it.
+ """
+ root = REPO_ROOT if root is None else root
+ try:
+ proc = subprocess.run( # noqa: S603
+ [ # noqa: S607
+ "git",
+ "-c",
+ "core.quotepath=off",
+ "status",
+ "--porcelain=v1",
+ "-z",
+ "--untracked-files=" + ("all" if untracked else "no"),
+ ],
+ cwd=root,
+ capture_output=True,
+ check=False,
+ timeout=60,
+ )
+ except (OSError, subprocess.SubprocessError):
+ return None
+ if proc.returncode != 0:
+ return None
+ entries = proc.stdout.decode("utf-8", errors="replace").split("\0")
+ state: Snapshot = {}
+ index = 0
+ while index < len(entries):
+ entry = entries[index]
+ index += 1
+ if len(entry) < 4:
+ continue
+ status, path = entry[:2], entry[3:]
+ if "R" in status or "C" in status:
+ index += 1 # the rename/copy source follows as its own field
+ state[path] = f"{status}:{_stamp(root / path)}"
+ return state
+
+
+def _stamp(path: Path) -> str:
+ try:
+ info = path.stat()
+ except OSError:
+ return "-"
+ if path.is_dir():
+ return "dir"
+ return f"{info.st_size}:{info.st_mtime_ns}"
+
+
+def changes(before: Snapshot, after: Snapshot, ignore: Iterable[str] = ()) -> list[str]:
+ """Paths whose state differs between the snapshots, minus ``ignore``.
+
+ An ``ignore`` entry ending in ``/`` covers a whole directory; any other
+ entry is one path. A path that was dirty before and is clean afterwards
+ counts too: the run reverted somebody's edit.
+ """
+ prefixes = tuple(_norm(item) for item in ignore if item)
+ found = []
+ for path in sorted(set(before) | set(after)):
+ if before.get(path) == after.get(path):
+ continue
+ if any(path == p.rstrip("/") or (p.endswith("/") and path.startswith(p)) for p in prefixes):
+ continue
+ found.append(path)
+ return found
+
+
+def _norm(item: str) -> str:
+ text = item.replace("\\", "/")
+ while text.startswith("./"):
+ text = text[2:]
+ return text
+
+
+def describe(paths: list[str], limit: int = 20) -> str:
+ lines = [f" {path}" for path in paths[:limit]]
+ if len(paths) > limit:
+ lines.append(f" ... and {len(paths) - limit} more")
+ return "\n".join(lines)
+
+
+def relative_ignores(paths: Iterable[Path], root: Path | None = None) -> list[str]:
+ """Repo-relative forms of output paths that live inside the checkout."""
+ root = REPO_ROOT if root is None else root
+ found = []
+ for path in paths:
+ try:
+ found.append(path.resolve().relative_to(root.resolve()).as_posix())
+ except ValueError:
+ continue # outside the checkout: git never sees it
+ return found
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
+ sub = parser.add_subparsers(dest="command", required=True)
+ snap = sub.add_parser("snapshot", help="Record the checkout's dirty paths.")
+ snap.add_argument("--out", type=Path, required=True)
+ check = sub.add_parser("check", help="Fail when the checkout changed since the snapshot.")
+ check.add_argument("--snapshot", type=Path, required=True)
+ check.add_argument("--ignore", action="append", default=[], help="Path or dir/ to skip.")
+ args = parser.parse_args(argv)
+
+ current = snapshot()
+ if current is None:
+ print("checkout_guard: git status is unavailable; nothing checked.")
+ return 0
+ if args.command == "snapshot":
+ args.out.parent.mkdir(parents=True, exist_ok=True)
+ args.out.write_text(json.dumps(current, indent=1, sort_keys=True) + "\n", encoding="utf-8")
+ print(f"checkout_guard: {len(current)} dirty path(s) recorded.")
+ return 0
+ before = json.loads(args.snapshot.read_text(encoding="utf-8"))
+ changed = changes(before, current, args.ignore)
+ if not changed:
+ print("checkout_guard: the checkout is as the snapshot found it.")
+ return 0
+ print(
+ "::error title=Tests changed the checkout::The test run modified tracked files or "
+ "left new files in the checkout. Point the writer at a temp path:"
+ )
+ print(describe(changed, limit=50))
+ return 1
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/scripts/ci/run_tests_parallel.py b/scripts/ci/run_tests_parallel.py
index ad45fa4e0a..f2142feabf 100644
--- a/scripts/ci/run_tests_parallel.py
+++ b/scripts/ci/run_tests_parallel.py
@@ -23,10 +23,15 @@ How it works (a per-file runner):
file on a loaded runner is not a hang.
Outputs: ``--report`` (JSON: failed test ids, flaky files, counts) and
-``--junit-out`` (merged JUnit XML), plus ``--durations-out`` (timings of files
+``--junit-out`` (merged JUnit XML), both in the run's temp dir unless given,
+plus ``--durations-out`` (timings of files
that passed on the first attempt or alone after a timeout — a killed run has no
timing, so a hang can never ratchet its own budget).
+The run must leave the checkout as it found it: a tracked file it rewrote
+or a new file it left behind (outside its own outputs) fails it, see
+``checkout_guard.py``.
+
The runner exits 1 when any test failed for real; whether that blocks the
build is ``test_ratchet.py``'s decision (known failures are baselined).
"""
@@ -49,6 +54,11 @@ from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass, field
from pathlib import Path
+try: # imported as scripts.ci.run_tests_parallel (tests, select_tests.py)
+ from scripts.ci import checkout_guard
+except ImportError: # run as a script: its own directory is on sys.path
+ import checkout_guard # type: ignore[no-redef]
+
REPO_ROOT = Path(__file__).resolve().parents[2]
DEFAULT_MARKERS = (
"not skip_ci and not slow and not openclaw_live and not voice_latency and not eval"
@@ -490,6 +500,16 @@ def merge_junit(paths: list[Path], out: Path) -> None:
ET.ElementTree(merged).write(out, encoding="utf-8", xml_declaration=True)
+def checkout_changes(before: checkout_guard.Snapshot | None, outputs: list[Path]) -> list[str]:
+ """Paths the run changed in the checkout, minus its own requested outputs."""
+ if before is None:
+ return []
+ after = checkout_guard.snapshot()
+ if after is None:
+ return []
+ return checkout_guard.changes(before, after, checkout_guard.relative_ignores(outputs))
+
+
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("roots", nargs="*", default=["tests"], help="Discovery roots.")
@@ -500,8 +520,10 @@ def main(argv: list[str] | None = None) -> int:
parser.add_argument("--pytest-args", default="", help="Extra args for every pytest run.")
parser.add_argument("--durations", type=Path, default=REPO_ROOT / "test_durations.json")
parser.add_argument("--durations-out", type=Path)
- parser.add_argument("--report", type=Path, default=Path("test-report.json"))
- parser.add_argument("--junit-out", type=Path, default=Path("report.xml"))
+ # Defaults live in the run's temp dir: a bare run must not drop files
+ # into the checkout it is testing.
+ parser.add_argument("--report", type=Path, help="Default: <run temp dir>/test-report.json.")
+ parser.add_argument("--junit-out", type=Path, help="Default: <run temp dir>/report.xml.")
parser.add_argument("--list", action="store_true", help="Print the shard's files and exit.")
parser.add_argument(
"--deadline-minutes",
@@ -535,6 +557,9 @@ def main(argv: list[str] | None = None) -> int:
batches = make_batches(mine, durations)
started = time.monotonic()
workdir = Path(tempfile.mkdtemp(prefix="jarvis-tests-"))
+ args.report = args.report or workdir / "test-report.json"
+ args.junit_out = args.junit_out or workdir / "report.xml"
+ checkout_before = checkout_guard.snapshot()
runner = Runner(args, durations, workdir)
all_runs: list[RunResult] = []
flaky: list[str] = []
@@ -598,6 +623,16 @@ def main(argv: list[str] | None = None) -> int:
f"{len(report['timeouts'])} timed-out files",
flush=True,
)
+ print(f"[runner] report: {args.report} junit: {args.junit_out}", flush=True)
+ outputs = [args.report, args.junit_out, *([args.durations_out] if args.durations_out else [])]
+ changed = checkout_changes(checkout_before, outputs)
+ if changed:
+ print(
+ "[runner] FAIL: the run modified tracked files or left new files in the "
+ "checkout. Point the writer at a temp path:\n" + checkout_guard.describe(changed),
+ flush=True,
+ )
+ return 1
return 1 if failed_ids else 0
diff --git a/tests/conftest.py b/tests/conftest.py
index cce3169b62..7cd03f490a 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -19,6 +19,12 @@ if str(_REPO_ROOT) not in sys.path:
_REPO_DATA_DIR = _REPO_ROOT / "data"
_REPO_DATA_DIR_EXISTED = _REPO_DATA_DIR.exists()
+# The tracked files a session must leave alone, recorded before any test runs
+# (see pytest_sessionfinish). None outside a git checkout: nothing to judge.
+from scripts.ci import checkout_guard # noqa: E402
+
+_TRACKED_BEFORE = checkout_guard.snapshot(_REPO_ROOT, untracked=False)
+
# Hermetic data dir (#458). ``dodaios.core.config.DATA_DIR`` is resolved once,
# at import, from ``JARVIS_DATA_DIR`` or ``<repo>/data``. In an installer-managed
# checkout ``<repo>/data`` is the user's live data, so any store without its own
@@ -40,6 +46,16 @@ from dodaios.core.config import MemoryConfig # noqa: E402
MemoryConfig.model_fields["data_dir"].default = str(_TEST_DATA_DIR)
MemoryConfig.model_rebuild(force=True)
+# The provider switches persist into the drift baseline under scripts/, a
+# tracked file; a test that switched the brain without its own redirect
+# rewrote it (brain.primary -> "openrouter"). Every session writes to a temp
+# copy instead, so readers still see real content.
+_TEST_DRIFT_BASELINE = _TEST_DATA_DIR / "config-soll.json" # i18n-allow: filename
+_REPO_DRIFT_BASELINE = _REPO_ROOT / "scripts" / "config-soll.json" # i18n-allow: filename
+if _REPO_DRIFT_BASELINE.is_file():
+ shutil.copyfile(_REPO_DRIFT_BASELINE, _TEST_DRIFT_BASELINE)
+os.environ["JARVIS_DRIFT_BASELINE_FILE"] = str(_TEST_DRIFT_BASELINE)
+
import pytest # noqa: E402
import pytest_asyncio # noqa: E402
@@ -68,35 +84,75 @@ def _repo_data_dir_leftovers() -> list[str]:
return entries or [_REPO_DATA_DIR.relative_to(_REPO_ROOT).as_posix() + "/"]
-def pytest_sessionfinish(session, exitstatus) -> None: # noqa: ANN001, ARG001
- """Fail the session when the suite wrote into ``<repo>/data`` (#458).
+def _changed_tracked_files() -> list[str]:
+ """Tracked files whose state changed since the session started."""
+ if _TRACKED_BEFORE is None:
+ return []
+ after = checkout_guard.snapshot(_REPO_ROOT, untracked=False)
+ if after is None:
+ return []
+ return checkout_guard.changes(_TRACKED_BEFORE, after)
- Every store must follow the session temp dir above. Checked once, by the
- controlling process (an xdist worker would see its siblings' writes).
- """
- if hasattr(session.config, "workerinput"):
- return
- leftovers = _repo_data_dir_leftovers()
- if not leftovers:
- return
- lines = [f" {entry}" for entry in leftovers[:20]]
- if len(leftovers) > 20:
- lines.append(f" ... and {len(leftovers) - 20} more")
- message = "\n".join([
- "The test session wrote into the checkout's data dir, which every store "
- "must leave alone (#458). Derive the path from the configured data dir:",
- *lines,
- ])
+
+def _fail_session(session, title: str, message: str) -> None: # noqa: ANN001
reporter = session.config.pluginmanager.get_plugin("terminalreporter")
if reporter is not None:
reporter.ensure_newline()
- reporter.write_sep("=", "stray writes into <repo>/data", red=True)
+ reporter.write_sep("=", title, red=True)
reporter.write_line(message)
else:
sys.stderr.write(message + "\n")
session.exitstatus = pytest.ExitCode.TESTS_FAILED
+def pytest_sessionfinish(session, exitstatus) -> None: # noqa: ANN001, ARG001
+ """Fail the session when the suite wrote into the checkout.
+
+ Two leaks are judged: entries under ``<repo>/data`` (#458; every store must
+ follow the session temp dir above) and tracked files the session rewrote
+ (the drift baseline and friends; write to a temp path instead).
+ Checked once, by the controlling process (an xdist worker would see its
+ siblings' writes).
+ """
+ if hasattr(session.config, "workerinput"):
+ return
+ leftovers = _repo_data_dir_leftovers()
+ if leftovers:
+ _fail_session(
+ session,
+ "stray writes into <repo>/data",
+ "The test session wrote into the checkout's data dir, which every store "
+ "must leave alone (#458). Derive the path from the configured data dir:\n"
+ + checkout_guard.describe(leftovers),
+ )
+ changed = _changed_tracked_files()
+ if changed:
+ _fail_session(
+ session,
+ "tracked files changed by the tests",
+ "The test session modified tracked files in the checkout. Point the "
+ "writer at a temp path (tmp_path, a monkeypatched path, or the "
+ "session's JARVIS_DATA_DIR):\n" + checkout_guard.describe(changed),
+ )
+
+
[email protected](autouse=True, scope="session")
+def _no_user_environment_writes():
+ """No test persists a ``JARVIS__*`` variable into the real user environment.
+
+ The provider switches mirror their choice into the Windows User-scope
+ environment (``HKCU\\Environment``), which outlives the test run: a brain
+ switch under test left ``JARVIS__BRAIN__PRIMARY=openrouter`` behind, and
+ the next app start booted that provider. ``os.environ`` is still updated;
+ a test that checks the registry layer patches the writer itself.
+ """
+ from dodaios.core import config_writer
+
+ with pytest.MonkeyPatch.context() as patch:
+ patch.setattr(config_writer, "_set_user_env_var_winreg", lambda name, value: None)
+ yield
+
+
@pytest.fixture(autouse=True)
def _not_the_main_instance(monkeypatch): # noqa: ANN001
"""No test inherits the desktop app's main-instance flag.
diff --git a/tests/unit/ci/test_checkout_guard.py b/tests/unit/ci/test_checkout_guard.py
new file mode 100644
index 0000000000..048bfe59dc
--- /dev/null
+++ b/tests/unit/ci/test_checkout_guard.py
@@ -0,0 +1,220 @@
+"""A test run leaves the checkout as it found it.
+
+Covers the snapshot guard (``scripts/ci/checkout_guard.py``), the session-end
+check in ``tests/conftest.py``, the parallel runner's defaults and check, and
+the redirect that keeps provider switches out of the tracked drift baseline.
+"""
+
+from __future__ import annotations
+
+import json
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+from dodaios.core import config_writer
+from scripts.ci import checkout_guard, run_tests_parallel
+
+
+def _git(root: Path, *args: str) -> None:
+ subprocess.run(["git", *args], cwd=root, check=True, capture_output=True) # noqa: S603, S607
+
+
+def repo(tmp_path: Path) -> Path:
+ root = tmp_path / "repo"
+ root.mkdir()
+ _git(root, "init", "-q")
+ _git(root, "config", "user.email", "[email protected]")
+ _git(root, "config", "user.name", "test")
+ _git(root, "config", "core.autocrlf", "false")
+ (root / "tracked.json").write_text('{"primary": "openai"}\n', encoding="utf-8")
+ (root / "edited.txt").write_text("one\n", encoding="utf-8")
+ _git(root, "add", ".")
+ _git(root, "commit", "-q", "-m", "init")
+ return root
+
+
+def _bump(path: Path, text: str) -> None:
+ """Rewrite ``path`` with a strictly newer mtime, as a later write would."""
+ before = path.stat().st_mtime_ns
+ path.write_text(text, encoding="utf-8")
+ os.utime(path, ns=(before + 10_000_000, before + 10_000_000))
+
+
+def test_a_clean_run_changes_nothing(repo: Path) -> None:
+ before = checkout_guard.snapshot(repo)
+ assert before == {}
+ assert checkout_guard.changes(before, checkout_guard.snapshot(repo)) == []
+
+
+def test_a_rewritten_tracked_file_and_a_new_file_are_reported(repo: Path) -> None:
+ before = checkout_guard.snapshot(repo)
+ (repo / "tracked.json").write_text('{"primary": "openrouter"}\n', encoding="utf-8")
+ (repo / "report.xml").write_text("<testsuites/>", encoding="utf-8")
+ after = checkout_guard.snapshot(repo)
+ assert checkout_guard.changes(before, after) == ["report.xml", "tracked.json"]
+
+
+def test_a_developers_own_edits_are_not_blamed_but_a_further_rewrite_is(repo: Path) -> None:
+ (repo / "edited.txt").write_text("two\n", encoding="utf-8")
+ before = checkout_guard.snapshot(repo)
+ assert list(before) == ["edited.txt"]
+ assert checkout_guard.changes(before, checkout_guard.snapshot(repo)) == []
+ _bump(repo / "edited.txt", "three\n")
+ assert checkout_guard.changes(before, checkout_guard.snapshot(repo)) == ["edited.txt"]
+
+
+def test_ignored_outputs_are_skipped(repo: Path) -> None:
+ before = checkout_guard.snapshot(repo)
+ (repo / "out").mkdir()
+ (repo / "out" / "junit.xml").write_text("<testsuites/>", encoding="utf-8")
+ (repo / "r.json").write_text("{}", encoding="utf-8")
+ after = checkout_guard.snapshot(repo)
+ assert checkout_guard.changes(before, after, ["out/", "r.json"]) == []
+ assert checkout_guard.changes(before, after, ["./out/"]) == ["r.json"]
+
+
+def test_tracked_only_snapshots_ignore_untracked_files(repo: Path) -> None:
+ (repo / "probe.py").write_text("", encoding="utf-8")
+ assert checkout_guard.snapshot(repo, untracked=False) == {}
+
+
+def test_outside_a_checkout_nothing_is_judged(tmp_path: Path) -> None:
+ assert checkout_guard.snapshot(tmp_path / "nowhere") is None
+
+
+def test_the_cli_snapshot_then_check_round_trip(repo: Path, monkeypatch, capsys) -> None: # noqa: ANN001
+ monkeypatch.setattr(checkout_guard, "REPO_ROOT", repo)
+ snap = repo.parent / "before.json"
+ assert checkout_guard.main(["snapshot", "--out", str(snap)]) == 0
+ assert json.loads(snap.read_text(encoding="utf-8")) == {}
+ assert checkout_guard.main(["check", "--snapshot", str(snap)]) == 0
+ (repo / "report.xml").write_text("<testsuites/>", encoding="utf-8")
+ assert checkout_guard.main(["check", "--snapshot", str(snap)]) == 1
+ assert "report.xml" in capsys.readouterr().out
+ (repo / "out").mkdir()
+ (repo / "out" / "x.xml").write_text("", encoding="utf-8")
+ assert checkout_guard.main(["check", "--snapshot", str(snap), "--ignore", "out/"]) == 1
+ (repo / "report.xml").unlink()
+ assert checkout_guard.main(["check", "--snapshot", str(snap), "--ignore", "out/"]) == 0
+
+
+# ------------------------------------------------------------- session guard
+
+
+def test_the_session_guard_reports_a_rewritten_tracked_file(repo: Path, monkeypatch) -> None: # noqa: ANN001
+ conftest = sys.modules["tests.conftest"]
+ monkeypatch.setattr(conftest, "_REPO_ROOT", repo)
+ monkeypatch.setattr(conftest, "_TRACKED_BEFORE", checkout_guard.snapshot(repo, untracked=False))
+ (repo / "probe.py").write_text("", encoding="utf-8") # untracked: a sibling's probe
+ assert conftest._changed_tracked_files() == []
+ (repo / "tracked.json").write_text('{"primary": "openrouter"}\n', encoding="utf-8")
+ assert conftest._changed_tracked_files() == ["tracked.json"]
+
+
+def test_the_session_guard_skips_a_session_outside_a_checkout(monkeypatch) -> None: # noqa: ANN001
+ conftest = sys.modules["tests.conftest"]
+ monkeypatch.setattr(conftest, "_TRACKED_BEFORE", None)
+ assert conftest._changed_tracked_files() == []
+
+
+# ------------------------------------------------------------ drift baseline redirect
+
+
+def test_the_session_writes_the_drift_baseline_to_a_temp_copy() -> None:
+ baseline = config_writer._config_soll_path() # i18n-allow: real config_writer identifier
+ real = config_writer.PROJECT_ROOT / "scripts" / "config-soll.json" # i18n-allow: filename
+ assert baseline.resolve() != real.resolve()
+ assert baseline.resolve().is_relative_to(Path(os.environ["JARVIS_DATA_DIR"]).resolve())
+
+
+def test_without_the_override_the_baseline_is_the_checkouts_file(monkeypatch) -> None: # noqa: ANN001
+ monkeypatch.delenv(config_writer.DRIFT_BASELINE_FILE_ENV, raising=False)
+ assert (
+ config_writer._config_soll_path() # i18n-allow: real config_writer identifier
+ == config_writer.PROJECT_ROOT / "scripts" / "config-soll.json" # i18n-allow: filename
+ )
+
+
+def test_a_brain_switch_under_test_leaves_the_tracked_baseline_alone(tmp_path: Path) -> None:
+ real = config_writer.PROJECT_ROOT / "scripts" / "config-soll.json" # i18n-allow: filename
+ before = real.read_bytes() if real.exists() else None
+ config = tmp_path / "jarvis.toml"
+ config.write_text('[brain]\nprimary = "openai"\n', encoding="utf-8")
+ config_writer.set_brain_primary("openrouter", path=config)
+ assert (real.read_bytes() if real.exists() else None) == before
+ baseline = config_writer._config_soll_path() # i18n-allow: real config_writer identifier
+ if baseline.exists():
+ assert json.loads(baseline.read_text(encoding="utf-8"))["brain"]["primary"] == "openrouter"
+
+
+def test_the_session_never_writes_the_user_environment() -> None:
+ assert config_writer._set_user_env_var_winreg("JARVIS__PROBE", "x") is None
+ if sys.platform == "win32":
+ import winreg
+
+ with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment") as key:
+ with pytest.raises(FileNotFoundError):
+ winreg.QueryValueEx(key, "JARVIS__PROBE")
+
+
+# ------------------------------------------------------------ parallel runner
+
+
+def test_the_runner_writes_its_reports_outside_the_checkout_by_default(
+ monkeypatch, tmp_path: Path # noqa: ANN001
+) -> None:
+ seen: dict[str, Path] = {}
+ monkeypatch.setattr(run_tests_parallel, "discover", lambda roots: [])
+ monkeypatch.setattr(
+ run_tests_parallel, "merge_junit", lambda paths, out: seen.update(junit=out)
+ )
+ monkeypatch.setattr(
+ run_tests_parallel,
+ "checkout_changes",
+ lambda before, outputs: seen.update(outs=outputs) or [],
+ )
+ code = run_tests_parallel.main(["tests", "--durations", str(tmp_path / "none.json")])
+ assert code == 0
+ report, junit = seen["outs"][0], seen["outs"][1]
+ assert junit == seen["junit"] and junit.name == "report.xml"
+ for path in (report, junit):
+ assert not path.resolve().is_relative_to(run_tests_parallel.REPO_ROOT.resolve())
+ assert json.loads(report.read_text(encoding="utf-8"))["files"] == 0
+
+
+def test_the_runner_fails_a_run_that_changed_the_checkout(
+ monkeypatch, tmp_path: Path # noqa: ANN001
+) -> None:
+ monkeypatch.setattr(run_tests_parallel, "discover", lambda roots: [])
+ monkeypatch.setattr(
+ run_tests_parallel, "checkout_changes", lambda before, outputs: ["report.xml"]
+ )
+ code = run_tests_parallel.main(
+ [
+ "tests",
+ "--durations",
+ str(tmp_path / "none.json"),
+ "--report",
+ str(tmp_path / "r.json"),
+ "--junit-out",
+ str(tmp_path / "r.xml"),
+ ]
+ )
+ assert code == 1
+
+
+def test_the_runner_does_not_blame_its_own_outputs(repo: Path, monkeypatch) -> None: # noqa: ANN001
+ monkeypatch.setattr(checkout_guard, "REPO_ROOT", repo)
+ before = checkout_guard.snapshot()
+ (repo / "out").mkdir()
+ outputs = [repo / "out" / "junit.xml", repo / "out" / "r.json"]
+ for path in outputs:
+ path.write_text("", encoding="utf-8")
+ assert run_tests_parallel.checkout_changes(before, outputs) == []
+ (repo / "report.xml").write_text("", encoding="utf-8")
+ assert run_tests_parallel.checkout_changes(before, outputs) == ["report.xml"]
diff --git a/tests/unit/ci/test_pipeline_scripts.py b/tests/unit/ci/test_pipeline_scripts.py
index ff8130a5e7..39245b86b5 100644
--- a/tests/unit/ci/test_pipeline_scripts.py
+++ b/tests/unit/ci/test_pipeline_scripts.py
@@ -239,7 +239,10 @@ def test_file_timeout_scales_with_history_but_is_bounded():
)
-def test_runner_reports_failures_and_passes(tmp_path):
+def test_runner_reports_failures_and_passes(tmp_path, monkeypatch):
+ # Sibling test processes may touch the shared checkout meanwhile; the
+ # checkout guard has its own tests (test_checkout_guard.py).
+ monkeypatch.setattr(run_tests_parallel, "checkout_changes", lambda before, outputs: [])
repo = run_tests_parallel.REPO_ROOT
rel = Path(f"tests/unit/ci/_runner_probe_{tmp_path.name}")
probe = repo / rel
@@ -356,6 +359,7 @@ def test_a_slow_file_enters_the_duration_history_and_the_run_stays_green(
)
# One file per batch, as a file with a long history runs.
monkeypatch.setattr(run_tests_parallel, "BATCH_MAX_FILES", 1)
+ monkeypatch.setattr(run_tests_parallel, "checkout_changes", lambda before, outputs: [])
try:
for name in (slow, quick):
(repo / name).write_text("def test_ok():\n assert True\n")