/
/
/
1"""Tests for the shared baseline helper used by baseline-aware lint checks."""
2
3from pathlib import Path
4
5from scripts.lint_baseline import diff_baseline, load_baseline, render_baseline
6
7
8def test_render_and_load_roundtrip(tmp_path: Path) -> None:
9 """A rendered baseline reloads to the same per-file counts (and skips zero counts)."""
10 counts = {"b/file.py": 2, "a/file.py": 1, "c/skip.py": 0}
11 path = tmp_path / "baseline.txt"
12 path.write_text(render_baseline(counts, "header line"), encoding="utf-8")
13 assert load_baseline(path) == {"a/file.py": 1, "b/file.py": 2}
14
15
16def test_render_is_sorted_with_comment_header() -> None:
17 """The body is comment-prefixed header lines followed by path/count lines sorted by path."""
18 body = render_baseline({"z.py": 1, "a.py": 1}, "first\nsecond")
19 lines = body.splitlines()
20 assert lines[0] == "# first"
21 assert lines[1] == "# second"
22 assert lines[2:] == ["a.py\t1", "z.py\t1"]
23
24
25def test_load_missing_file_is_empty(tmp_path: Path) -> None:
26 """A missing baseline file yields an empty mapping rather than raising."""
27 assert load_baseline(tmp_path / "does_not_exist.txt") == {}
28
29
30def test_load_tolerates_bare_paths(tmp_path: Path) -> None:
31 """A line without a tab is treated as a single grandfathered violation."""
32 path = tmp_path / "baseline.txt"
33 path.write_text("# comment\n\nbare/path.py\nwith/tab.py\t3\n", encoding="utf-8")
34 assert load_baseline(path) == {"bare/path.py": 1, "with/tab.py": 3}
35
36
37def test_diff_reports_regressions_and_improvements() -> None:
38 """diff_baseline flags files above their baseline and those that improved below it."""
39 current = {"new.py": 1, "worse.py": 3, "same.py": 2, "better.py": 1}
40 baseline = {"worse.py": 2, "same.py": 2, "better.py": 4, "fixed.py": 1}
41 regressions, improvements = diff_baseline(current, baseline)
42 assert regressions == {"new.py": 1, "worse.py": 3}
43 assert improvements == {"better.py": 4, "fixed.py": 1}
44