Repository navigation
Expand file tree
/
Copy pathtest_compare.py
More file actions
297 lines (211 loc) · 14.6 KB
/
Copy pathtest_compare.py
File metadata and controls
297 lines (211 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
"""Tests for compare.py."""
import csv
import json
from compare import diff_runs, load_run, main, regressed, regression_reasons, render_pr, render_weekly
SCORES_HEADER = ["module", "score", "declarations", "kdoc_documented", "kdoc_total", "failing"]
FINDINGS_HEADER = ["module", "rule", "file", "line", "declaration", "detail"]
def write_run(directory, scores, findings, declarations=(), constructors=()):
directory.mkdir()
with open(directory / "api-quality-constructors-2026-01-01.csv", "w", newline="") as f:
w = csv.writer(f)
w.writerow(["module", "public", "file", "line", "declaration"])
w.writerows([module, "true", file, 1, declaration] for module, file, declaration in constructors)
with open(directory / "api-quality-declarations-2026-01-01.csv", "w", newline="") as f:
w = csv.writer(f)
w.writerow(["module", "kind", "documented", "file", "line", "declaration"])
w.writerows([module, "MEMBER_FUNCTION", "true", file, 1, declaration] for module, file, declaration in declarations)
with open(directory / "api-quality-scores-2026-01-01.csv", "w", newline="") as f:
w = csv.writer(f)
w.writerow(SCORES_HEADER)
w.writerows(scores)
with open(directory / "api-quality-findings-2026-01-01.csv", "w", newline="") as f:
w = csv.writer(f)
w.writerow(FINDINGS_HEADER)
w.writerows(findings)
return directory
def test_new_and_resolved_findings_are_compared_by_declaration(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 6, 10, 5, 10, "M4"]],
[["a-api", "M4", "A.kt", 3, "A.old()", "no KDoc"]])
after = write_run(tmp_path / "after", [["a-api", 6, 10, 5, 10, "M4"]],
[["a-api", "M4", "A.kt", 9, "A.new()", "no KDoc"]])
diff = diff_runs(load_run(before), load_run(after))
assert [f.declaration for f in diff.new] == ["A.new()"]
assert [f.declaration for f in diff.resolved] == ["A.old()"]
assert diff.score_changes == []
def test_line_changes_alone_are_not_findings(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 6, 10, 0, 0, "M4"]], [["a-api", "M4", "A.kt", 3, "A.f()", "no KDoc"]])
after = write_run(tmp_path / "after", [["a-api", 6, 10, 0, 0, "M4"]], [["a-api", "M4", "A.kt", 40, "A.f()", "no KDoc"]])
diff = diff_runs(load_run(before), load_run(after))
assert diff.new == [] and diff.resolved == []
def test_declaration_moving_between_modules_is_reported_as_moved(tmp_path):
before = write_run(tmp_path / "before",
[["a-api", 1, 100, 0, 0, "M1"], ["b-api", 6, 3, 0, 0, "M3"]],
[["a-api", "M1", "a/Entity.kt", 1, "Entity", "type name ends in Entity"]])
after = write_run(tmp_path / "after",
[["a-api", 2, 99, 0, 0, ""], ["b-api", 5, 4, 0, 0, "M1 M3"]],
[["b-api", "M1", "b/Entity.kt", 1, "Entity", "type name ends in Entity"]])
diff = diff_runs(load_run(before), load_run(after))
assert diff.new == [] and diff.resolved == []
assert [(s.module, t.module) for s, t in diff.moved] == [("a-api", "b-api")]
def test_pr_comment_says_so_when_nothing_was_added(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 7, 1, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 7, 1, 0, 0, ""]], [])
b, a = load_run(before), load_run(after)
comment = render_pr(b, a, diff_runs(b, a))
assert "✅ No new API quality findings." in comment
def test_weekly_json_groups_new_findings_by_module_and_rule(tmp_path, monkeypatch):
before = write_run(tmp_path / "before", [["a-api", 7, 2, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 6, 4, 0, 0, "M3"]], [
["a-api", "M3", "A.kt", 1, "A", "no KDoc"],
["a-api", "M3", "B.kt", 1, "B", "no KDoc"],
])
out_md, out_json = tmp_path / "report.md", tmp_path / "new.json"
monkeypatch.setattr("sys.argv", ["compare.py", "--before", str(before), "--after", str(after), "--mode", "weekly",
"--out-md", str(out_md), "--out-json", str(out_json), "--link-base", "https://gh/blob/sha",
"--weekly-report"])
main()
groups = json.loads(out_json.read_text())["new"]
assert [(g["module"], g["rule"], len(g["findings"])) for g in groups] == [("a-api", "M3", 2)]
assert groups[0]["findings"][0]["url"] == "https://gh/blob/sha/A.kt#L1"
assert "<!-- regressions-task -->\n\n| API | Rule | Findings |\n|---|---|---|\n| `a-api` | M3: Top-level declarations documented | 2 |" in out_md.read_text()
def test_new_finding_is_a_regression(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 7, 1, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 6, 2, 0, 0, "M3"]], [["a-api", "M3", "A.kt", 1, "A", "no KDoc"]])
b, a = load_run(before), load_run(after)
assert regressed(b, a, diff_runs(b, a))
def test_resolved_and_moved_findings_are_not_regressions_when_no_score_drops(tmp_path):
before = write_run(tmp_path / "before",
[["a-api", 5, 100, 0, 0, "M1 M3"], ["b-api", 5, 3, 0, 0, "M1 M3"]],
[["a-api", "M1", "a/Entity.kt", 1, "Entity", "type name ends in Entity"],
["a-api", "M3", "a/Old.kt", 1, "Old", "no KDoc"]])
after = write_run(tmp_path / "after",
[["a-api", 7, 98, 0, 0, ""], ["b-api", 5, 4, 0, 0, "M1 M3"]],
[["b-api", "M1", "b/Entity.kt", 1, "Entity", "type name ends in Entity"]])
b, a = load_run(before), load_run(after)
assert not regressed(b, a, diff_runs(b, a))
def test_weekly_title_is_configurable(tmp_path, monkeypatch):
before = write_run(tmp_path / "before", [["a-api", 7, 1, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 7, 1, 0, 0, ""]], [])
out_md = tmp_path / "report.md"
monkeypatch.setattr("sys.argv", ["compare.py", "--before", str(before), "--after", str(after), "--mode", "weekly",
"--out-md", str(out_md), "--title", "Since the baseline"])
main()
assert out_md.read_text().startswith("## Since the baseline")
def test_same_name_in_two_packages_of_a_module_is_still_new(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 6, 10, 0, 0, "M4"]], [["a-api", "M4", "a/one/A.kt", 3, "A.f()", "no KDoc"]])
after = write_run(tmp_path / "after", [["a-api", 6, 11, 0, 0, "M4"]], [
["a-api", "M4", "a/one/A.kt", 3, "A.f()", "no KDoc"],
["a-api", "M4", "a/two/A.kt", 3, "A.f()", "no KDoc"],
])
diff = diff_runs(load_run(before), load_run(after))
assert [f.file for f in diff.new] == ["a/two/A.kt"]
def test_score_drop_from_a_move_is_a_regression_that_names_the_move(tmp_path):
before = write_run(tmp_path / "before",
[["a-api", 6, 100, 0, 0, "M1"], ["b-api", 7, 3, 0, 0, ""]],
[["a-api", "M1", "a/Entity.kt", 1, "Entity", "type name ends in Entity"]])
after = write_run(tmp_path / "after",
[["a-api", 7, 99, 0, 0, ""], ["b-api", 6, 4, 0, 0, "M1"]],
[["b-api", "M1", "b/Entity.kt", 1, "Entity", "type name ends in Entity"]])
b, a = load_run(before), load_run(after)
reasons = regression_reasons(b, a, diff_runs(b, a))
assert len(reasons) == 1
assert "`b-api`: Score 7/7 → 6/7" in reasons[0] and "moved in from `a-api`" in reasons[0]
def test_pr_comment_lists_every_new_finding_with_a_link_to_its_line(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 7, 1, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 6, 7, 0, 0, "M3"]],
[["a-api", "M3", f"src/F{i}.kt", i, f"F{i}", "no KDoc"] for i in range(6)])
b, a = load_run(before), load_run(after)
comment = render_pr(b, a, diff_runs(b, a), rules_url="https://rules", link_base="https://gh/blob/sha")
assert all(f"| `a-api` | [F{i}.kt:{i}](https://gh/blob/sha/src/F{i}.kt#L{i}) | `F{i}`: no KDoc<br>**Fix:** Add a KDoc line saying what it's for. |"
in comment for i in range(6))
assert "[API quality rules and scores](https://rules)" in comment
def test_weekly_report_lists_each_regression_once_and_resolved_by_module(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 7, 2, 0, 0, ""], ["b-api", 6, 2, 0, 0, "M7"]],
[["b-api", "M7", "B.kt", 1, "B.x", "public var"]])
after = write_run(tmp_path / "after", [["a-api", 6, 4, 0, 0, "M3"], ["b-api", 7, 2, 0, 0, ""]], [
["a-api", "M3", "A.kt", 1, "A", "no KDoc"],
["a-api", "M3", "C.kt", 1, "C", "no KDoc"],
])
b, a = load_run(before), load_run(after)
report = render_weekly(b, a, diff_runs(b, a), weekly_report=True)
assert report.count("| `a-api` | M3") == 1
assert "| `b-api` | M7 |" in report
assert "| `a-api` | 7/7 | 6/7 |" in report
assert "score effect" not in report.lower()
def test_weekly_report_opens_with_a_summary_and_has_an_o2_placeholder(tmp_path):
base = write_run(tmp_path / "base", [["a-api", 5, 2, 0, 0, "M3 M4"]], [])
before = write_run(tmp_path / "before", [["a-api", 7, 2, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 6, 4, 0, 0, "M3"]], [["a-api", "M3", "A.kt", 1, "A", "no KDoc"]])
b, a = load_run(before), load_run(after)
report = render_weekly(b, a, diff_runs(b, a), baseline=load_run(base), weekly_report=True)
assert report.startswith("## Summary\n\n| Metric | Baseline | Last week | This week | Goal |")
assert "| Score, weighted by declarations | 71.4% | 100.0% | 85.7% | 75% |" in report
assert "| New findings | | | 1 | |" in report
assert "| Finding tasks untriaged after 14 days | | | — <!-- awaiting-triage --> | 0 |" in report
assert report.index("## O1: API code quality") < report.index("## O2: API review process\n\n[Under construction]")
def test_moving_the_debt_to_another_copy_of_a_name_in_one_module_is_new(tmp_path):
copies = [("a-api", "a/one/A.kt", "A.f()"), ("a-api", "a/two/A.kt", "A.f()")]
before = write_run(tmp_path / "before", [["a-api", 6, 10, 0, 0, "M4"]], [["a-api", "M4", "a/one/A.kt", 3, "A.f()", "no KDoc"]], copies)
after = write_run(tmp_path / "after", [["a-api", 6, 10, 0, 0, "M4"]], [["a-api", "M4", "a/two/A.kt", 3, "A.f()", "no KDoc"]], copies)
b, a = load_run(before), load_run(after)
diff = diff_runs(b, a)
assert [f.file for f in diff.new] == ["a/two/A.kt"] and [f.file for f in diff.resolved] == ["a/one/A.kt"]
assert regressed(b, a, diff)
def test_renamed_file_is_not_a_change(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 6, 10, 0, 0, "M4"]], [["a-api", "M4", "a/Old.kt", 3, "A.f()", "no KDoc"]])
after = write_run(tmp_path / "after", [["a-api", 6, 10, 0, 0, "M4"]], [["a-api", "M4", "a/New.kt", 3, "A.f()", "no KDoc"]])
diff = diff_runs(load_run(before), load_run(after))
assert diff.new == [] and diff.resolved == [] and diff.moved == []
def test_same_name_getting_worse_in_another_module_is_not_a_move(tmp_path):
copies = [("a-api", "a/A.kt", "A.f()"), ("b-api", "b/A.kt", "A.f()")]
before = write_run(tmp_path / "before", [["a-api", 6, 10, 0, 0, "M4"], ["b-api", 6, 10, 0, 0, "M4"]],
[["a-api", "M4", "a/A.kt", 3, "A.f()", "no KDoc"]], copies)
after = write_run(tmp_path / "after", [["a-api", 6, 10, 0, 0, "M4"], ["b-api", 6, 10, 0, 0, "M4"]],
[["b-api", "M4", "b/A.kt", 3, "A.f()", "no KDoc"]], copies)
b, a = load_run(before), load_run(after)
diff = diff_runs(b, a)
assert diff.moved == [] and [f.module for f in diff.new] == ["b-api"]
assert regressed(b, a, diff)
def test_hiding_one_constructor_and_exposing_a_same_named_one_is_new(tmp_path):
constructors = [("a-api", "a/one/A.kt", "A.constructor(Any)"), ("a-api", "a/two/A.kt", "A.constructor(Any)")]
before = write_run(tmp_path / "before", [["a-api", 6, 10, 0, 0, "M6"]],
[["a-api", "M6", "a/one/A.kt", 3, "A.constructor(Any)", "Any in Any"]], constructors=constructors)
after = write_run(tmp_path / "after", [["a-api", 6, 10, 0, 0, "M6"]],
[["a-api", "M6", "a/two/A.kt", 3, "A.constructor(Any)", "Any in Any"]], constructors=constructors)
b, a = load_run(before), load_run(after)
diff = diff_runs(b, a)
assert [f.file for f in diff.new] == ["a/two/A.kt"]
assert regressed(b, a, diff)
def test_pr_comment_keeps_generic_types_in_code(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 7, 1, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 6, 1, 0, 0, "M7"]],
[["a-api", "M7", "A.kt", 1, "A.items", "MutableList in MutableList<String>"]])
b, a = load_run(before), load_run(after)
assert "`A.items`: MutableList in `MutableList<String>`" in render_pr(b, a, diff_runs(b, a))
def test_comparison_lists_each_regression_with_its_line_details_and_fix(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 7, 1, 0, 0, ""]], [])
after = write_run(tmp_path / "after", [["a-api", 6, 2, 0, 0, "M7"]], [["a-api", "M7", "src/A.kt", 4, "A.x", "public var"]])
b, a = load_run(before), load_run(after)
report = render_weekly(b, a, diff_runs(b, a), "API quality: x compared with y", link_base="https://gh/blob/sha")
assert "| API | Line | Details | Fix |" in report and "<!-- regressions-task -->" not in report
assert "| `a-api` | [A.kt:4](https://gh/blob/sha/src/A.kt#L4) | `A.x`: public var | Expose a `val`" in report
def test_rules_file_matches_the_scorer():
import re
from pathlib import Path
from compare import RULES, SHORT_FIXES, load_rules
scorer = (Path(__file__).parent / "Scorer.java").read_text()
scorer_rules = re.search(r"String\[\] RULES = \{([^}]*)\}", scorer).group(1)
assert [r["id"] for r in load_rules()] == re.findall(r'"(M\d+)"', scorer_rules)
assert all(RULES[rule][0] and RULES[rule][1] and RULES[rule][2] and SHORT_FIXES[rule] for rule in RULES)
def test_weekly_summary_tracks_open_findings_and_kdoc_coverage(tmp_path):
before = write_run(tmp_path / "before", [["a-api", 6, 4, 1, 4, "M4"]], [
["a-api", "M4", "A.kt", 1, "A.f()", "no KDoc"],
["a-api", "M4", "A.kt", 2, "A.g()", "no KDoc"],
["a-api", "M4", "A.kt", 3, "A.h()", "no KDoc"],
])
after = write_run(tmp_path / "after", [["a-api", 6, 4, 3, 4, "M4"]], [["a-api", "M4", "A.kt", 1, "A.f()", "no KDoc"]])
b, a = load_run(before), load_run(after)
report = render_weekly(b, a, diff_runs(b, a), weekly_report=True)
assert "| Open findings | 3 | 1 | |" in report
assert "| KDoc coverage of public declarations | 25.0% | 75.0% | |" in report

