Add baseline report formats and --baseline/--format CLI switches
CI / checks (pull_request) Successful in 1m5s
CI / checks (pull_request) Successful in 1m5s
Text and Markdown comparison reports (regressions first, IDs escaped); exit code still comes from the current arm only, errors give exit 2. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8f3844d650
commit
74574b948c
4 files changed
+213
-4
No files matched your search
@@ -1,9 +1,13 @@
|
||||
from dataclasses import replace
|
||||
|
||||
from chemenu.evals.compare import (
|
||||
Baseline,
|
||||
BaselineCase,
|
||||
CaseComparison,
|
||||
SetComparison,
|
||||
compare,
|
||||
format_comparison,
|
||||
format_comparison_markdown,
|
||||
input_key,
|
||||
)
|
||||
from chemenu.evals.example import ExampleOutput
|
||||
@@ -198,3 +202,46 @@ def test_input_key_distinguishes_bool_and_int() -> None:
|
||||
assert input_key({"b": 1, "a": 2}) == input_key({"a": 2, "b": 1})
|
||||
assert input_key({"a": "ä"}) == '{"a": "ä"}'
|
||||
assert input_key({"a": object()}) is None
|
||||
|
||||
|
||||
def table_rows(markdown: str) -> list[str]:
|
||||
return [line for line in markdown.splitlines() if line.startswith("| ")][2:]
|
||||
|
||||
|
||||
def test_markdown_lists_regressions_first() -> None:
|
||||
old = baseline(bcase("a", GOOD), bcase("z", GOOD))
|
||||
result = run(["a", "z"], {"a": GOOD, "z": BAD}, old)
|
||||
rows = table_rows(format_comparison_markdown(result))
|
||||
assert [r.split(" | ")[0] for r in rows] == ["| z", "| a"]
|
||||
assert "regression" in rows[0]
|
||||
text_lines = format_comparison(result).splitlines()
|
||||
ordered = [ln.split(" ")[:2] for ln in text_lines if ln.startswith(("REGRESSION", "SAME"))]
|
||||
assert ordered == [["REGRESSION", "z"], ["SAME", "a"]]
|
||||
assert all(ln == ln.rstrip() for ln in text_lines)
|
||||
|
||||
|
||||
def test_markdown_escapes_case_ids() -> None:
|
||||
case_id = "a|b_c*d"
|
||||
result = run([case_id], {case_id: GOOD}, baseline(bcase(case_id, GOOD)))
|
||||
markdown = format_comparison_markdown(result)
|
||||
rows = table_rows(markdown)
|
||||
assert len(rows) == 1
|
||||
assert "a\\|b\\_c\\*d" in rows[0]
|
||||
header_rows = [ln for ln in markdown.splitlines() if ln.startswith(("| Case", "| ---"))]
|
||||
for row in [*rows, *header_rows]:
|
||||
assert row.replace("\\|", "").count("|") == 5
|
||||
renamed = SetComparison(
|
||||
replace(result.current, name="extract ~v2~\nnext"), "main", "abc", False, result.cases
|
||||
)
|
||||
heading = format_comparison_markdown(renamed).splitlines()[0]
|
||||
assert heading == "### extract \\~v2\\~ next"
|
||||
|
||||
|
||||
def test_report_notes_no_changed_responses() -> None:
|
||||
note = "no changed responses: the difference says nothing about the prompt"
|
||||
same = run(["a"], {"a": GOOD}, baseline(bcase("a", GOOD)))
|
||||
changed = run(["a"], {"a": GOOD}, baseline(bcase("a", GOOD_SPACED)))
|
||||
assert note in format_comparison(same)
|
||||
assert note in format_comparison_markdown(same)
|
||||
assert note not in format_comparison(changed)
|
||||
assert note not in format_comparison_markdown(changed)
|
||||
@@ -1,3 +1,4 @@
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -96,3 +97,71 @@ def test_exit_2_on_empty_set(tmp_path: Path, capsys: pytest.CaptureFixture[str])
|
||||
case.unlink()
|
||||
assert main([str(tmp_path)]) == 2
|
||||
assert "no cases found" in capsys.readouterr().err
|
||||
|
||||
|
||||
GIT = [
|
||||
"git",
|
||||
"-c",
|
||||
"user.name=t",
|
||||
"-c",
|
||||
"user.email=t@example.invalid",
|
||||
"-c",
|
||||
"commit.gpgsign=false",
|
||||
]
|
||||
|
||||
|
||||
def git(repo: Path, *args: str) -> None:
|
||||
subprocess.run([*GIT, "-C", str(repo), *args], capture_output=True, check=True) # noqa: S603
|
||||
|
||||
|
||||
def commit_all(repo: Path) -> None:
|
||||
git(repo, "add", "-A")
|
||||
git(repo, "commit", "-q", "-m", "x")
|
||||
|
||||
|
||||
def record(set_dir: Path, a: str, b: str) -> None:
|
||||
(set_dir / "recorded" / "a.json").write_text(a, encoding="utf-8")
|
||||
(set_dir / "recorded" / "b.json").write_text(b, encoding="utf-8")
|
||||
|
||||
|
||||
def test_baseline_exit_code_follows_current_arm(
|
||||
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
||||
) -> None:
|
||||
git(tmp_path, "init", "-q")
|
||||
set_dir = make_recorded(tmp_path / "demo", {"a": GOOD, "b": GOOD})
|
||||
commit_all(tmp_path)
|
||||
record(set_dir, GOOD, BAD)
|
||||
assert main([str(set_dir), "--baseline", "HEAD"]) == 1
|
||||
out = capsys.readouterr().out
|
||||
assert "demo: 1/2 passed" in out
|
||||
assert "REGRESSION b" in out
|
||||
commit_all(tmp_path)
|
||||
record(set_dir, GOOD, GOOD)
|
||||
assert main([str(set_dir), "--baseline", "HEAD"]) == 0
|
||||
assert "FIXED b" in capsys.readouterr().out
|
||||
|
||||
|
||||
def test_baseline_unknown_ref_exit_2(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
||||
git(tmp_path, "init", "-q")
|
||||
set_dir = make_recorded(tmp_path / "demo", {"a": GOOD, "b": GOOD})
|
||||
commit_all(tmp_path)
|
||||
assert main([str(set_dir), "--baseline", "no-such-ref"]) == 2
|
||||
captured = capsys.readouterr()
|
||||
assert captured.err.startswith("chemenu-eval: ")
|
||||
assert captured.out == ""
|
||||
|
||||
|
||||
def test_markdown_requires_baseline(tmp_path: Path) -> None:
|
||||
make_recorded(tmp_path, {"a": GOOD, "b": GOOD})
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main([str(tmp_path), "--format", "markdown"])
|
||||
assert excinfo.value.code == 2
|
||||
|
||||
|
||||
def test_baseline_new_set_exit_0(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
||||
git(tmp_path, "init", "-q")
|
||||
(tmp_path / "other.txt").write_text("x")
|
||||
commit_all(tmp_path)
|
||||
set_dir = make_recorded(tmp_path / "demo", {"a": GOOD, "b": GOOD})
|
||||
assert main([str(set_dir), "--baseline", "HEAD", "--format", "markdown"]) == 0
|
||||
assert "new set" in capsys.readouterr().out
|
||||
Reference in new issue
Block a user