Add load_baseline reading an eval set from a git ref

Archive the set folder at the commit, unpack into a temp dir and read
cases and recorded responses into memory; the working tree stays untouched.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
torbenandClaude Sonnet 5.5 committed 2026-12-04 23:16:48 +01:00
1 parent e9c5913025
commit 8f3844d650
3 files changed
+276

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
import pytest
from chemenu.evals.baseline import GIT_REPO_ENV
@pytest.fixture(autouse=True)
def _isolated_git(monkeypatch: pytest.MonkeyPatch) -> None:
"""Keep git in tests away from the repository running them.
`git rebase --exec` and hooks export GIT_DIR; without this, `git init` and
`git commit` in a test would act on the checkout itself.
"""
for name in GIT_REPO_ENV:
monkeypatch.delenv(name, raising=False)
monkeypatch.setenv("GIT_CONFIG_GLOBAL", "/dev/null")
monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
+149
View File
@@ -0,0 +1,149 @@
"""load_baseline against real git repositories in tmp_path."""
import subprocess
from pathlib import Path
import pytest
from chemenu.evals.baseline import BaselineError, load_baseline
from chemenu.evals.compare import input_key
GIT = [
"git",
"-c",
"user.name=t",
"-c",
"user.email=t@example.invalid",
"-c",
"commit.gpgsign=false",
]
def git(repo: Path, *args: str) -> str:
result = subprocess.run( # noqa: S603
[*GIT, "-C", str(repo), *args], capture_output=True, text=True, check=True
)
return result.stdout.strip()
def write_set(repo: Path, value: int, response: str) -> Path:
set_dir = repo / "evals" / "demo"
(set_dir / "cases").mkdir(parents=True, exist_ok=True)
(set_dir / "recorded").mkdir(exist_ok=True)
(set_dir / "eval.yaml").write_text("name: demo\n")
(set_dir / "cases" / "a.yaml").write_text(f"input: {{x: {value}}}\n")
(set_dir / "recorded" / "a.json").write_text(response)
return set_dir
def commit(repo: Path, message: str) -> str:
git(repo, "add", "-A")
git(repo, "commit", "-q", "-m", message)
return git(repo, "rev-parse", "HEAD")
@pytest.fixture
def repo(tmp_path: Path) -> Path:
git(tmp_path, "init", "-q")
return tmp_path
def test_reads_cases_and_responses_at_ref(repo: Path) -> None:
set_dir = write_set(repo, 1, '{"v": 1}')
sha = commit(repo, "one")
git(repo, "tag", "v1")
write_set(repo, 2, '{"v": 2}')
commit(repo, "two")
write_set(repo, 3, '{"v": 3}')
for ref in ("HEAD~1", "v1", sha):
baseline = load_baseline(set_dir, ref)
assert baseline.ref == ref
assert baseline.commit == sha
assert baseline.cases is not None
case = baseline.cases["a"]
assert case.case_id == "a"
assert case.input_key == input_key({"x": 1})
assert case.response == '{"v": 1}'
def test_set_missing_at_ref_gives_none(repo: Path) -> None:
(repo / "README").write_text("x")
first = commit(repo, "one")
set_dir = write_set(repo, 1, "{}")
commit(repo, "two")
baseline = load_baseline(set_dir, first)
assert baseline.cases is None
assert baseline.commit == first
def test_unreadable_case_has_no_input_key(repo: Path) -> None:
set_dir = write_set(repo, 1, "{}")
(set_dir / "cases" / "bad.yaml").write_text("input: [unclosed\n")
(set_dir / "cases" / "noinput.yaml").write_text("expect: {}\n")
(set_dir / "recorded" / "noinput.json").write_bytes(b"\xff\xfe\x00{")
commit(repo, "one")
baseline = load_baseline(set_dir, "HEAD")
assert baseline.cases is not None
assert baseline.cases["bad"].input_key is None
assert baseline.cases["bad"].response is None
assert baseline.cases["noinput"].input_key is None
assert baseline.cases["noinput"].response_unreadable
assert not baseline.cases["a"].response_unreadable
assert baseline.cases["a"].input_key == input_key({"x": 1})
@pytest.mark.parametrize("ref", ["nope", "--output=x"])
def test_unknown_ref_raises(repo: Path, ref: str) -> None:
set_dir = write_set(repo, 1, "{}")
commit(repo, "one")
before = sorted(p.name for p in repo.iterdir())
with pytest.raises(BaselineError):
load_baseline(set_dir, ref)
assert sorted(p.name for p in repo.iterdir()) == before
assert not (repo / "x").exists()
def test_outside_repository_raises(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("GIT_CEILING_DIRECTORIES", str(tmp_path))
set_dir = write_set(tmp_path, 1, "{}")
with pytest.raises(BaselineError):
load_baseline(set_dir, "HEAD")
def test_working_tree_untouched(repo: Path) -> None:
set_dir = write_set(repo, 1, '{"v": 1}')
commit(repo, "one")
write_set(repo, 2, '{"v": 2}')
status = git(repo, "status", "--porcelain")
files = sorted(p for p in repo.rglob("*") if ".git" not in p.parts)
content = (set_dir / "recorded" / "a.json").read_text()
load_baseline(set_dir, "HEAD")
assert git(repo, "status", "--porcelain") == status
assert sorted(p for p in repo.rglob("*") if ".git" not in p.parts) == files
assert (set_dir / "recorded" / "a.json").read_text() == content
def test_ignores_inherited_git_dir(
repo: Path, tmp_path_factory: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch
) -> None:
set_dir = write_set(repo, 1, '{"v": 1}')
sha = commit(repo, "one")
other = tmp_path_factory.mktemp("other")
git(other, "init", "-q")
monkeypatch.setenv("GIT_DIR", str(other / ".git"))
monkeypatch.setenv("GIT_WORK_TREE", str(other))
baseline = load_baseline(set_dir, "HEAD")
assert baseline.commit == sha
def test_git_that_cannot_run_raises(repo: Path, monkeypatch: pytest.MonkeyPatch) -> None:
set_dir = write_set(repo, 1, "{}")
commit(repo, "one")
def broken(*args: object, **kwargs: object) -> None:
raise PermissionError("not executable")
monkeypatch.setattr(subprocess, "run", broken)
with pytest.raises(BaselineError, match="cannot run git"):
load_baseline(set_dir, "HEAD")