-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_pipeline_checks.py
More file actions
535 lines (475 loc) · 20.2 KB
/
test_pipeline_checks.py
File metadata and controls
535 lines (475 loc) · 20.2 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/env python3
import subprocess
import tempfile
import unittest
from unittest import mock
from pathlib import Path
import pipeline_checks
from pipeline_checks import (
build_review_context,
completeness_check,
detect_scope_from_paths,
file_whitelist_check,
infer_review_subject,
issue_guard_check,
parse_args,
)
class PipelineChecksTests(unittest.TestCase):
@mock.patch("pipeline_checks.run_gh_json")
def test_fetch_existing_prs_falls_back_to_rest_search_on_pr_list_failure(
self,
run_gh_json: mock.Mock,
) -> None:
run_gh_json.side_effect = [
subprocess.CalledProcessError(1, ["gh", "pr", "list"]),
{
"items": [
{
"number": 223,
"pull_request": {
"url": "https://api.github.com/repos/CodingThrust/problem-reductions/pulls/223"
},
}
]
},
{
"number": 223,
"head": {"ref": "issue-212-multiprocessor-scheduling"},
"html_url": "https://example.test/pull/223",
},
]
prs = pipeline_checks.fetch_existing_prs(
"CodingThrust/problem-reductions",
212,
)
self.assertEqual(
prs,
[
{
"number": 223,
"headRefName": "issue-212-multiprocessor-scheduling",
"url": "https://example.test/pull/223",
}
],
)
def test_detect_scope_reports_model_review_for_new_model_file(self) -> None:
scope = detect_scope_from_paths(
added_files=["src/models/graph/graph_partitioning.rs"],
changed_files=[
"src/models/graph/graph_partitioning.rs",
"src/unit_tests/models/graph/graph_partitioning.rs",
],
)
self.assertEqual(scope["review_type"], "model")
self.assertEqual(scope["models"][0]["category"], "graph")
self.assertEqual(scope["models"][0]["file_stem"], "graph_partitioning")
self.assertEqual(scope["models"][0]["problem_name"], "GraphPartitioning")
def test_detect_scope_reports_rule_review_for_new_rule_file(self) -> None:
scope = detect_scope_from_paths(
added_files=["src/rules/binpacking_ilp.rs"],
changed_files=["src/rules/binpacking_ilp.rs"],
)
self.assertEqual(scope["review_type"], "rule")
self.assertEqual(scope["rules"][0]["rule_stem"], "binpacking_ilp")
def test_detect_scope_reports_generic_when_no_new_model_or_rule_files(self) -> None:
scope = detect_scope_from_paths(
added_files=[],
changed_files=["src/lib.rs", "docs/paper/reductions.typ"],
)
self.assertEqual(scope["review_type"], "generic")
self.assertEqual(scope["models"], [])
self.assertEqual(scope["rules"], [])
def test_infer_review_subject_prefers_explicit_rule_metadata(self) -> None:
scope = {
"review_type": "rule",
"models": [],
"rules": [{"rule_stem": "binpacking_ilp"}],
"changed_files": ["src/rules/binpacking_ilp.rs"],
}
subject = infer_review_subject(
scope,
kind="rule",
name="binpacking_ilp",
source="BinPacking",
target="ILP",
)
self.assertEqual(subject["kind"], "rule")
self.assertEqual(subject["name"], "binpacking_ilp")
self.assertEqual(subject["source"], "BinPacking")
self.assertEqual(subject["target"], "ILP")
self.assertFalse(subject["inferred"])
def test_infer_review_subject_infers_model_from_scope(self) -> None:
scope = {
"review_type": "model",
"models": [{"problem_name": "GraphPartitioning"}],
"rules": [],
"changed_files": ["src/models/graph/graph_partitioning.rs"],
}
subject = infer_review_subject(scope)
self.assertEqual(subject["kind"], "model")
self.assertEqual(subject["name"], "GraphPartitioning")
self.assertTrue(subject["inferred"])
def test_file_whitelist_accepts_expected_model_files(self) -> None:
report = file_whitelist_check(
"model",
[
"src/models/graph/graph_partitioning.rs",
"src/unit_tests/models/graph/graph_partitioning.rs",
"src/example_db/model_builders.rs",
"docs/paper/reductions.typ",
],
)
self.assertTrue(report["ok"])
self.assertEqual(report["violations"], [])
def test_file_whitelist_flags_unexpected_model_files(self) -> None:
report = file_whitelist_check(
"model",
[
"src/models/graph/graph_partitioning.rs",
"Cargo.toml",
],
)
self.assertFalse(report["ok"])
self.assertEqual(report["violations"][0]["path"], "Cargo.toml")
def test_file_whitelist_accepts_expected_rule_files(self) -> None:
report = file_whitelist_check(
"rule",
[
"src/rules/binpacking_ilp.rs",
"src/rules/mod.rs",
"src/unit_tests/rules/binpacking_ilp.rs",
"src/example_db/rule_builders.rs",
"src/models/graph/bin_packing.rs",
"docs/paper/reductions.typ",
],
)
self.assertTrue(report["ok"])
self.assertEqual(report["violations"], [])
def test_model_completeness_reports_all_required_components(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
self._write(
repo / "src/models/graph/graph_partitioning.rs",
"""
inventory::submit! { ProblemSchemaEntry { name: "GraphPartitioning" } }
impl OptimizationProblem for GraphPartitioning<SimpleGraph> {}
crate::declare_variants! { opt GraphPartitioning<SimpleGraph> => "1.2^n" }
pub(crate) fn canonical_model_example_specs() -> Vec<ModelExampleSpec> { vec![] }
""",
)
self._write(
repo / "src/unit_tests/models/graph/graph_partitioning.rs",
"#[test]\nfn test_graph_partitioning_basic() {}\n",
)
self._write(repo / "src/example_db/model_builders.rs", "pub fn build_model_examples() {}\n")
self._write(
repo / "docs/paper/reductions.typ",
"""
#let display-name = (
"GraphPartitioning": [Graph Partitioning],
)
#problem-def("GraphPartitioning")[body][proof]
""",
)
report = completeness_check("model", repo, name="GraphPartitioning")
self.assertTrue(report["ok"])
self.assertEqual(report["missing"], [])
self.assertEqual(report["checks"]["paper_display_name"]["status"], "pass")
def test_model_completeness_flags_missing_paper_entries(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
self._write(
repo / "src/models/graph/graph_partitioning.rs",
"""
inventory::submit! { ProblemSchemaEntry { name: "GraphPartitioning" } }
impl OptimizationProblem for GraphPartitioning<SimpleGraph> {}
crate::declare_variants! { opt GraphPartitioning<SimpleGraph> => "1.2^n" }
pub(crate) fn canonical_model_example_specs() -> Vec<ModelExampleSpec> { vec![] }
""",
)
self._write(
repo / "src/unit_tests/models/graph/graph_partitioning.rs",
"#[test]\nfn test_graph_partitioning_basic() {}\n",
)
self._write(repo / "src/example_db/model_builders.rs", "pub fn build_model_examples() {}\n")
self._write(repo / "docs/paper/reductions.typ", "#let display-name = ()\n")
report = completeness_check("model", repo, name="GraphPartitioning")
self.assertFalse(report["ok"])
self.assertIn("paper_definition", report["missing"])
self.assertIn("paper_display_name", report["missing"])
def test_rule_completeness_reports_all_required_components(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
self._write(
repo / "src/rules/binpacking_ilp.rs",
"""
#[reduction(overhead = { num_vars = "num_items" })]
impl ReduceTo<ILP> for BinPacking {}
pub(crate) fn canonical_rule_example_specs() -> Vec<RuleExampleSpec> { vec![] }
""",
)
self._write(repo / "src/rules/mod.rs", "mod binpacking_ilp;\n")
self._write(
repo / "src/unit_tests/rules/binpacking_ilp.rs",
"#[test]\nfn test_binpacking_to_ilp_closed_loop() {}\n",
)
self._write(repo / "src/example_db/rule_builders.rs", "pub fn build_rule_examples() {}\n")
self._write(
repo / "docs/paper/reductions.typ",
'#reduction-rule("BinPacking", "ILP")[rule][proof]\n',
)
report = completeness_check(
"rule",
repo,
name="binpacking_ilp",
source="BinPacking",
target="ILP",
)
self.assertTrue(report["ok"])
self.assertEqual(report["checks"]["module_registration"]["status"], "pass")
self.assertEqual(report["checks"]["paper_rule"]["status"], "pass")
def test_rule_completeness_flags_missing_overhead_and_paper(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
self._write(
repo / "src/rules/binpacking_ilp.rs",
"""
#[reduction]
impl ReduceTo<ILP> for BinPacking {}
""",
)
self._write(repo / "src/rules/mod.rs", "")
self._write(
repo / "src/unit_tests/rules/binpacking_ilp.rs",
"#[test]\nfn test_binpacking_to_ilp_closed_loop() {}\n",
)
self._write(repo / "src/example_db/rule_builders.rs", "pub fn build_rule_examples() {}\n")
self._write(repo / "docs/paper/reductions.typ", "")
report = completeness_check(
"rule",
repo,
name="binpacking_ilp",
source="BinPacking",
target="ILP",
)
self.assertFalse(report["ok"])
self.assertIn("overhead_form", report["missing"])
self.assertIn("paper_rule", report["missing"])
self.assertIn("module_registration", report["missing"])
def test_issue_guards_pass_for_good_model_issue_without_existing_pr(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
report = issue_guard_check(
repo,
issue={
"number": 117,
"title": "[Model] GraphPartitioning",
"body": "Implement the model.",
"state": "OPEN",
"url": "https://example.test/issues/117",
"labels": [{"name": "Good"}],
"comments": [
{
"author": {"login": "maintainer"},
"body": "Use the paper notation.",
}
],
},
existing_prs=[],
)
self.assertTrue(report["ok"])
self.assertEqual(report["kind"], "model")
self.assertEqual(report["checks"]["good_label"]["status"], "pass")
self.assertEqual(report["checks"]["source_model"]["status"], "skip")
self.assertEqual(report["comments"][0]["author"], "maintainer")
self.assertEqual(report["action"], "create-pr")
def test_issue_guards_fail_when_good_label_missing(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
report = issue_guard_check(
repo,
issue={
"number": 118,
"title": "[Model] GraphPartitioning",
"body": "Implement the model.",
"state": "OPEN",
"url": "https://example.test/issues/118",
"labels": [{"name": "NeedsCheck"}],
"comments": [],
},
existing_prs=[],
)
self.assertFalse(report["ok"])
self.assertIn("good_label", report["missing"])
self.assertEqual(report["checks"]["good_label"]["status"], "fail")
def test_issue_guards_fail_rule_issue_when_target_model_missing(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
self._write(
repo / "src/models/misc/bin_packing.rs",
"pub struct BinPacking;\n",
)
report = issue_guard_check(
repo,
issue={
"number": 119,
"title": "[Rule] BinPacking to ILP",
"body": "Implement the reduction.",
"state": "OPEN",
"url": "https://example.test/issues/119",
"labels": [{"name": "Good"}],
"comments": [],
},
existing_prs=[],
)
self.assertFalse(report["ok"])
self.assertEqual(report["kind"], "rule")
self.assertEqual(report["source_problem"], "BinPacking")
self.assertEqual(report["target_problem"], "ILP")
self.assertEqual(report["checks"]["source_model"]["status"], "pass")
self.assertEqual(report["checks"]["target_model"]["status"], "fail")
self.assertIn("target_model", report["missing"])
def test_issue_guards_report_existing_open_pr_for_resume(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
report = issue_guard_check(
repo,
issue={
"number": 120,
"title": "[Model] GraphPartitioning",
"body": "Implement the model.",
"state": "OPEN",
"url": "https://example.test/issues/120",
"labels": [{"name": "Good"}],
"comments": [],
},
existing_prs=[
{
"number": 650,
"headRefName": "issue-120-graph-partitioning",
"url": "https://example.test/pull/650",
}
],
)
self.assertTrue(report["ok"])
self.assertEqual(report["action"], "resume-pr")
self.assertEqual(report["resume_pr"]["number"], 650)
self.assertEqual(report["resume_pr"]["head_ref_name"], "issue-120-graph-partitioning")
def test_issue_context_alias_matches_issue_guard_contract(self) -> None:
issue_context_check = getattr(pipeline_checks, "issue_context_check", None)
self.assertIsNotNone(issue_context_check)
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
report = issue_context_check(
repo,
issue={
"number": 121,
"title": "[Model] ExactCoverBy3Sets",
"body": "Implement the model.",
"state": "OPEN",
"url": "https://example.test/issues/121",
"labels": [{"name": "Good"}],
"comments": [],
},
existing_prs=[],
)
self.assertTrue(report["ok"])
self.assertEqual(report["issue_number"], 121)
self.assertEqual(report["kind"], "model")
self.assertEqual(report["action"], "create-pr")
def test_build_review_context_reports_model_bundle(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
repo = Path(tmpdir)
self._write(
repo / "src/models/graph/graph_partitioning.rs",
"""
inventory::submit! { ProblemSchemaEntry { name: "GraphPartitioning" } }
impl OptimizationProblem for GraphPartitioning<SimpleGraph> {}
crate::declare_variants! { opt GraphPartitioning<SimpleGraph> => "1.2^n" }
pub(crate) fn canonical_model_example_specs() -> Vec<ModelExampleSpec> { vec![] }
""",
)
self._write(
repo / "src/unit_tests/models/graph/graph_partitioning.rs",
"#[test]\nfn test_graph_partitioning_basic() {}\n",
)
self._write(
repo / "docs/paper/reductions.typ",
"""
#let display-name = (
"GraphPartitioning": [Graph Partitioning],
)
#problem-def("GraphPartitioning")[body][proof]
""",
)
scope = detect_scope_from_paths(
added_files=["src/models/graph/graph_partitioning.rs"],
changed_files=[
"src/models/graph/graph_partitioning.rs",
"src/unit_tests/models/graph/graph_partitioning.rs",
"docs/paper/reductions.typ",
],
)
context = build_review_context(
repo,
diff_stat=" 3 files changed, 20 insertions(+)\n",
scope=scope,
subject=infer_review_subject(scope),
)
self.assertEqual(context["subject"]["kind"], "model")
self.assertFalse(context["whitelist"]["skipped"])
self.assertTrue(context["whitelist"]["ok"])
self.assertFalse(context["completeness"]["skipped"])
self.assertTrue(context["completeness"]["ok"])
self.assertIn("GraphPartitioning", context["subject"]["name"])
def test_build_review_context_skips_checks_for_generic_scope(self) -> None:
scope = detect_scope_from_paths(
added_files=[],
changed_files=["src/lib.rs", "README.md"],
)
context = build_review_context(
".",
diff_stat=" 2 files changed, 3 insertions(+)\n",
scope=scope,
subject=infer_review_subject(scope),
)
self.assertEqual(context["subject"]["kind"], "generic")
self.assertTrue(context["whitelist"]["skipped"])
self.assertTrue(context["completeness"]["skipped"])
def test_parse_args_accepts_review_context(self) -> None:
args = parse_args(
[
"review-context",
"--repo-root",
".",
"--base",
"abc123",
"--head",
"def456",
"--format",
"json",
]
)
self.assertEqual(args.command, "review-context")
self.assertEqual(args.base, "abc123")
self.assertEqual(args.head, "def456")
def test_parse_args_accepts_issue_context(self) -> None:
args = parse_args(
[
"issue-context",
"--repo",
"CodingThrust/problem-reductions",
"--issue",
"117",
"--format",
"json",
]
)
self.assertEqual(args.command, "issue-context")
self.assertEqual(args.repo, "CodingThrust/problem-reductions")
self.assertEqual(args.issue, 117)
def _write(self, path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content.strip() + "\n")
if __name__ == "__main__":
unittest.main()