"""tools/progress_dashboard.py: parsers, classification, the no-game-data rule and the output-folder rule. python -I tests/test_progress_dashboard.py Every input is written here by hand (Markdown tables, JUnit XML, a CTest log, a fake work folder whose shader names are sentinels). No game files, no GPU, no network: the GitHub path is fed synthetic gh output. """ import contextlib import io import json import os from pathlib import Path import subprocess import sys import tempfile from types import SimpleNamespace import unittest from unittest import mock HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(HERE) sys.path.insert(0, os.path.join(ROOT, "tools")) import progress_dashboard as pd # noqa: E402 TOOL = os.path.join(ROOT, "tools", "progress_dashboard.py") SENTINEL = "SENTINEL_SHADER_NAME_must_not_appear" class Markdown(unittest.TestCase): def test_tables_keep_pipes_inside_code(self): t = pd.md_tables("## Translator status and runtime contract\n\n| Piece | Contract |\n|---|---|\n| A | `x|y` is **GPU-checked** |\n") self.assertEqual(len(t), 1) heading, header, rows, line = t[0] self.assertEqual(heading, "Translator status and runtime contract") self.assertEqual(rows[0][1], "`x|y` is **GPU-checked**") self.assertEqual(line, 3) def test_plain_strips_links_and_emphasis(self): self.assertEqual(pd.plain("**GPU-checked** ([measurement](http://x)) `v_mac_f32`"), "GPU-checked (measurement) v_mac_f32") class Contract(unittest.TestCase): def test_classification_order_and_evidence(self): cases = { "**not yet GPU-verified**; Source: LLVM `SIInstrInfo.cpp`": ("not_gpu", "partial"), "**GPU-checked** (TEST-298) on an NVIDIA GeForce RTX 2060": ("gpu", "verified"), "Source: LLVM `VOPProfileI2F`": ("reference", "implemented"), "a count over the corpus: 0 of the 26,503 shaders": ("corpus", "implemented"), "CPU-checked by DenormalFlushTests": ("cpu", "implemented"), "The translator emits OpFAdd.": ("unstated", "unknown"), } for text, (kind, status) in cases.items(): with self.subTest(text=text): k, s, words = pd.classify_contract(text) self.assertEqual((k, s), (kind, status)) if kind != "unstated": self.assertIn(words.lower(), text.lower()) def test_open_items_are_detected_only_by_explicit_words(self): self.assertTrue(pd.OPEN.search("Still open: on some inputs the result is undefined")) self.assertFalse(pd.OPEN.search("s_getreg_b32 is unsupported and there are no traps")) class Tests(unittest.TestCase): def test_junit_rejects_unrelated_xml_but_accepts_empty_reports(self): with tempfile.TemporaryDirectory() as root: paths = [] for tag in ("coverage", "testsuite", "testsuites"): path = os.path.join(root, tag + ".xml") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(f"<{tag}/>\n") paths.append(path) col = pd.Collector() with mock.patch.object(pd, "ROOT", root), mock.patch.object(pd, "last_commit_time", return_value=None): pd.collect_ctest(col, SimpleNamespace(junit=paths)) sources = [s for s in col.sources if s["id"] == "junit"] self.assertEqual([(s["status"], s.get("records")) for s in sources], [("invalid", None), ("ok", 0), ("ok", 0)]) self.assertIn("expected a JUnit report", sources[0]["note"]) def test_junit_honors_each_reports_encoding(self): xml = '' with tempfile.TemporaryDirectory() as root: paths = [] for codec, declared, name in (("utf-16", "UTF-16", "日本語"), ("latin-1", "ISO-8859-1", "café"), ("ascii", "not-a-codec", "bad")): path = os.path.join(root, codec + ".xml") with open(path, "wb") as f: f.write(xml.format(declared, name).encode(codec)) paths.append(path) col = pd.Collector() with mock.patch.object(pd, "ROOT", root), mock.patch.object(pd, "last_commit_time", return_value=None): pd.collect_ctest(col, SimpleNamespace(junit=paths)) self.assertEqual([(c["name"], c["status"]) for c in col.components], [("日本語", "failed"), ("café", "failed")]) self.assertEqual([s["status"] for s in col.sources if s["id"] == "junit"], ["ok", "ok", "invalid"]) def test_unittest_file_is_skipped_only_when_all_cases_skip(self): with tempfile.TemporaryDirectory() as directory, mock.patch.object(pd, "ROOT", directory): file = os.path.join(directory, "test_sample.py") for skipped in (0, 1, 2): with self.subTest(skipped=skipped): with open(file, "w", encoding="utf-8", newline="\n") as f: f.write(f"import unittest\nclass Sample(unittest.TestCase):\n" f" @unittest.skipIf({skipped} > 0, 'synthetic')\n" f" def test_first(self): pass\n" f" @unittest.skipIf({skipped} > 1, 'synthetic')\n" f" def test_second(self): pass\n" f"unittest.main()\n") result = pd.run_one_test("test_sample.py", 15) self.assertEqual(result["result"], "pass", result) self.assertEqual(result["cases"], 2) self.assertEqual(result["all_skipped"], skipped == 2) def test_junit_ctest_and_pytest_flavours(self): ctest = '' \ '' rows = pd.parse_junit(ctest) self.assertEqual([(n, r) for n, r, _, _ in rows], [("loader", "pass"), ("xmem", "fail")]) self.assertNotIn("me", rows[1][3]) # paths are reduced to their last component pytest = '' \ '' self.assertEqual([(n, r) for n, r, _, _ in pd.parse_junit(pytest)], [("tests.a::t[1-2]", "pass"), ("tests.a::t[1_2]", "skip")]) def test_junit_omits_invalid_durations_without_losing_verdicts(self): reports = '' + ''.join( f'' for i, value in enumerate(("NaN", "Infinity", "1e999", "-1", "0", "0.08")) ) + '' rows = pd.parse_junit(reports) self.assertEqual([row[1] for row in rows], ["pass"] * 6) self.assertEqual([row[2] for row in rows], [None, None, None, None, 0.0, 0.08]) json.dumps(rows, allow_nan=False) def test_lasttest_log(self): log = "Start testing: Nov 22 10:00 X\n1/2 Test: loader\nTest time = 0.91 sec\nTest Passed.\n2/2 Test: xmem\nTest Failed.\n" self.assertEqual(pd.parse_lasttest(log), [("loader", "pass", 0.91), ("xmem", "fail", None)]) def test_lasttest_retains_verdicts_after_an_invalid_duration(self): log = ("1/2 Test: invalid_time\nTest time = 1..5 sec\nTest Failed.\n" "2/2 Test: next\nTest time = 0.02 sec\nTest Passed.\n") self.assertEqual(pd.parse_lasttest(log), [("invalid_time", "fail", None), ("next", "pass", 0.02)]) def test_lasttest_uses_ctest_verdicts_after_captured_output(self) -> None: cases = ( ("required", "ready", "Test Pass Reason:\nRequired regular expression found. Regex=[ready]", "pass"), ("forbidden", "Test Passed.\nerror", "Test Fail Reason:\nError regular expression found in output. Regex=[error]", "fail"), ("missing", "stopped", "Test Fail Reason:\nRequired regular expression not found. Regex=[ready]", "fail"), ("unavailable", "unavailable", "Test Pass Reason:\nSkip regular expression found in output. Regex=[unavailable]", "skip"), ("ordinary", "Test Fail Reason:\nunrelated test output", "Test Passed.", "pass"), ) log = "\n".join( f"{index}/{len(cases)} Test: {name}\nOutput:\n{'-' * 58}\n{output}\n\n" f"Test time = 0.01 sec\n{'-' * 58}\n{verdict}\n" for index, (name, output, verdict, _expected) in enumerate(cases, 1) ) self.assertEqual(pd.parse_lasttest(log), [(name, expected, 0.01) for name, _, _, expected in cases]) def test_lasttest_ignores_a_nested_ctest_log(self) -> None: separator = "-" * 58 inner = ( f"Start testing: Nov 22 10:00 X\n{separator}\n1/1 Testing: outer\n1/1 Test: outer\n" f"Output:\n{separator}\n\nTest time = 0.01 sec\n{separator}\n" f'Test Passed.\n"outer" end time: Nov 22 10:00 X\n"outer" time elapsed: 00:00:00\n' f"{separator}\n\nEnd testing: Nov 22 10:00 X" ) log = ( f"Start testing: Nov 22 10:00 X\n{separator}\n1/2 Testing: outer\n1/2 Test: outer\n" f"Output:\n{separator}\n{inner}\nouter fails\n\n" f'Test time = 0.23 sec\n{separator}\nTest Failed.\n"outer" end time: Nov 22 10:00 X\n' f'"outer" time elapsed: 00:00:00\n{separator}\n\n2/2 Test: next\n' "Test time = 0.02 sec\nTest Passed.\n" ) self.assertEqual(pd.parse_lasttest(log), [("outer", "fail", 0.23), ("next", "pass", 0.02)]) def test_lasttest_recovers_when_nested_log_omits_end_testing(self) -> None: # Mirrors the real CTest 3.31.10 fixture where outer `wrapper` (1/2, fails) prints an inner # log containing `wrapper` (1/2, passes) and `child_2` (2/2, passes) with identical ordinal # and timestamps, but without the inner `End testing:` trailer, followed by outer `later` (2/2). sep = "-" * 58 ts = "Nov 24 20:47 UTC" inner_no_end = ( f"Start testing: {ts}\n{sep}\n" f"1/2 Testing: wrapper\n1/2 Test: wrapper\nOutput:\n{sep}\ninner ok\n\n" f'Test time = 0.01 sec\n{sep}\nTest Passed.\n"wrapper" end time: {ts}\n' f'"wrapper" time elapsed: 00:00:00\n{sep}\n\n' f"2/2 Testing: child_2\n2/2 Test: child_2\nOutput:\n{sep}\nchild ok\n\n" f'Test time = 0.01 sec\n{sep}\nTest Passed.\n"child_2" end time: {ts}\n' f'"child_2" time elapsed: 00:00:00\n{sep}' ) log = ( f"Start testing: {ts}\n{sep}\n" f"1/2 Testing: wrapper\n1/2 Test: wrapper\nOutput:\n{sep}\n{inner_no_end}\n\n" f'Test time = 0.25 sec\n{sep}\nTest Failed.\n"wrapper" end time: {ts}\n' f'"wrapper" time elapsed: 00:00:00\n{sep}\n\n' f"2/2 Testing: later\n2/2 Test: later\nOutput:\n{sep}\nok\n\n" f'Test time = 0.02 sec\n{sep}\nTest Passed.\n"later" end time: {ts}\n' f'"later" time elapsed: 00:00:00\n{sep}\n\nEnd testing: {ts}\n' ) self.assertEqual(pd.parse_lasttest(log), [("wrapper", "fail", 0.25), ("later", "pass", 0.02)]) def test_lasttest_recovers_from_empty_incomplete_nested_log(self) -> None: sep = "-" * 58 log = ( f"Start testing: Nov 24 20:47 UTC\n{sep}\n" f"1/1 Testing: wrapper\n1/1 Test: wrapper\nOutput:\n{sep}\n" f"Start testing: Nov 24 20:47 UTC\n{sep}\n" f"\nTest time = 10.00 sec\n{sep}\nTest Failed.\n" ) self.assertEqual(pd.parse_lasttest(log), [("wrapper", "fail", 10.0)]) def test_lasttest_handles_three_level_nested_log_missing_innermost_end_testing(self) -> None: sep = "-" * 58 ts = "Nov 24 20:47 UTC" leaf = ( f"Start testing: {ts}\n{sep}\n1/1 Test: leaf\nOutput:\n{sep}\nleaf\n\n" f'Test time = 0.01 sec\n{sep}\nTest Passed.\n"leaf" end time: {ts}\n{sep}' ) mid = ( f"Start testing: {ts}\n{sep}\n1/1 Test: mid\nOutput:\n{sep}\n{leaf}\n\n" f'Test time = 0.05 sec\n{sep}\nTest Passed.\n"mid" end time: {ts}\n{sep}\n\nEnd testing: {ts}' ) log = ( f"Start testing: {ts}\n{sep}\n1/2 Test: wrapper\nOutput:\n{sep}\n{mid}\n\n" f'Test time = 0.30 sec\n{sep}\nTest Failed.\n"wrapper" end time: {ts}\n{sep}\n\n' f"2/2 Test: later\nOutput:\n{sep}\nok\n\n" f'Test time = 0.02 sec\n{sep}\nTest Passed.\n"later" end time: {ts}\n{sep}\n\nEnd testing: {ts}\n' ) self.assertEqual(pd.parse_lasttest(log), [("wrapper", "fail", 0.3), ("later", "pass", 0.02)]) def test_lasttest_recovers_when_inner_test_output_is_truncated_mid_stream(self) -> None: sep = "-" * 58 ts = "Nov 24 20:47 UTC" inner_truncated = ( f"Start testing: {ts}\n{sep}\n" f"1/2 Test: wrapper\nOutput:\n{sep}\nok\n\n" f'Test time = 0.01 sec\n{sep}\nTest Passed.\n"wrapper" end time: {ts}\n{sep}\n' f"2/2 Test: child_2\nOutput:\n{sep}\npartial output before kill" ) log = ( f"Start testing: {ts}\n{sep}\n" f"1/2 Test: wrapper\nOutput:\n{sep}\n{inner_truncated}\n\n" f'Test time = 0.40 sec\n{sep}\nTest Failed.\n"wrapper" end time: {ts}\n{sep}\n\n' f"2/2 Test: later\nOutput:\n{sep}\nok\n\n" f'Test time = 0.03 sec\n{sep}\nTest Passed.\n"later" end time: {ts}\n{sep}\n\nEnd testing: {ts}\n' ) self.assertEqual(pd.parse_lasttest(log), [("wrapper", "fail", 0.4), ("later", "pass", 0.03)]) def test_lasttest_recovers_when_multi_level_nested_output_is_truncated_mid_leaf(self) -> None: sep = "-" * 58 ts = "Nov 24 21:40 UTC" middle_truncated_in_leaf = ( f"Start testing: {ts}\n{sep}\n" f"1/1 Test: middle\nOutput:\n{sep}\n" f"Start testing: {ts}\n{sep}\n" f"1/1 Test: leaf\nOutput:\n{sep}\n" f"leaf payload" ) log = ( f"Start testing: {ts}\n{sep}\n" f"1/2 Test: wrapper\nOutput:\n{sep}\n{middle_truncated_in_leaf}\n\n" f'Test time = 0.25 sec\n{sep}\nTest Failed.\n"wrapper" end time: {ts}\n{sep}\n\n' f"2/2 Test: later\nOutput:\n{sep}\nok\n\n" f'Test time = 0.04 sec\n{sep}\nTest Passed.\n"later" end time: {ts}\n{sep}\n\nEnd testing: {ts}\n' ) self.assertEqual(pd.parse_lasttest(log), [("wrapper", "fail", 0.25), ("later", "pass", 0.04)]) def test_lasttest_recovers_when_middle_finishes_after_truncated_leaf(self) -> None: sep = "-" * 58 ts = "Nov 24 21:40 UTC" middle_with_truncated_leaf = ( f"Start testing: {ts}\n{sep}\n" f"1/1 Test: middle\nOutput:\n{sep}\n" f"Start testing: {ts}\n{sep}\n" f"1/1 Test: leaf\nOutput:\n{sep}\n" f"leaf payload\n\n" f'Test time = 0.10 sec\n{sep}\nTest Failed.\n"middle" end time: {ts}\n{sep}\nEnd testing: {ts}' ) log = ( f"Start testing: {ts}\n{sep}\n" f"1/2 Test: wrapper\nOutput:\n{sep}\n{middle_with_truncated_leaf}\n\n" f'Test time = 0.35 sec\n{sep}\nTest Passed.\n"wrapper" end time: {ts}\n{sep}\n\n' f"2/2 Test: later\nOutput:\n{sep}\nok\n\n" f'Test time = 0.05 sec\n{sep}\nTest Passed.\n"later" end time: {ts}\n{sep}\n\nEnd testing: {ts}\n' ) self.assertEqual(pd.parse_lasttest(log), [("wrapper", "pass", 0.35), ("later", "pass", 0.05)]) def test_lasttest_ignores_spurious_end_testing_inside_nested_test_output(self) -> None: sep = "-" * 58 ts = "Nov 24 20:47 UTC" inner = ( f"Start testing: {ts}\n{sep}\n1/1 Test: inner\nOutput:\n{sep}\n" f"End testing: spurious line in child output\n\n" f'Test time = 0.01 sec\n{sep}\nTest Passed.\n"inner" end time: {ts}\n{sep}\nEnd testing: {ts}' ) log = ( f"Start testing: {ts}\n{sep}\n1/1 Test: wrapper\nOutput:\n{sep}\n{inner}\n\n" f'Test time = 0.15 sec\n{sep}\nTest Failed.\n"wrapper" end time: {ts}\n{sep}\nEnd testing: {ts}\n' ) self.assertEqual(pd.parse_lasttest(log), [("wrapper", "fail", 0.15)]) def test_ci_selection_matches_the_workflow_rule(self): files = pd.ci_test_files() self.assertIn("tests/test_progress_dashboard.py", files) self.assertFalse(any(f.startswith("tests/semantic/") for f in files)) for f in files[:20]: with open(os.path.join(ROOT, f), encoding="utf-8") as fh: text = fh.read() self.assertIn("__main__", text) self.assertIsNone(pd.PYTEST_MARKERS.search(text)) def test_unlistable_subfolder_raises_instead_of_a_partial_scan(self): # os.walk skips a directory it cannot list unless onerror raises; the selection and the digest # used to silently drop the sub-folder and report the rest as complete. def walk(top, onerror=None, **kwargs): if onerror is not None: onerror(PermissionError(13, "Permission denied", os.path.join(top, "locked"))) return yield # pragma: no cover with mock.patch.object(pd.os, "walk", side_effect=walk): with self.assertRaises(PermissionError): pd.ci_test_files() with self.assertRaises(PermissionError): pd.code_digest() class Freshness(unittest.TestCase): def collect(self, results): with tempfile.TemporaryDirectory() as out: with open(os.path.join(out, "unit_results.json"), "w", encoding="utf-8", newline="\n") as f: json.dump(results, f) col = pd.Collector() pd.collect_unit(col, out, type("A", (), {"run_tests": False})()) return {c["name"]: (c["status"], c["freshness"]) for c in col.components} def base(self, digest, code): return {"ran_at": "2026-11-22T10:00:00Z", "commit": None, "code_digest": code, "results": [{"test": "tests/test_progress_dashboard.py", "result": "pass", "digest": digest}]} def test_unchanged_test_and_code_is_current(self): got = self.collect(self.base(pd.file_digest("tests/test_progress_dashboard.py"), pd.code_digest())) self.assertEqual(got["test_progress_dashboard.py"], ("verified", "current")) def test_changed_test_file_needs_revalidation(self): got = self.collect(self.base("0" * 16, pd.code_digest())) self.assertEqual(got["test_progress_dashboard.py"], ("partial", "revalidation_required")) def test_changed_tools_need_revalidation(self): got = self.collect(self.base(pd.file_digest("tests/test_progress_dashboard.py"), "0" * 16)) self.assertEqual(got["test_progress_dashboard.py"], ("partial", "revalidation_required")) def test_changed_shader_helper_needs_revalidation(self): with tempfile.TemporaryDirectory() as root, mock.patch.object(pd, "ROOT", root): for directory in ("tools", "scripts", "runtime/test/shaders", "tests"): Path(root, directory).mkdir(parents=True) test = "tests/test_progress_dashboard.py" Path(root, test).touch() helper = Path(root, "runtime/test/shaders/compute_fixture.py") helper.write_bytes(b"shift = 0\n") cached = self.base(pd.file_digest(test), pd.code_digest()) self.assertEqual(self.collect(cached)["test_progress_dashboard.py"], ("verified", "current")) helper.write_bytes(b"shift = 1\n") self.assertEqual(self.collect(cached)["test_progress_dashboard.py"], ("partial", "revalidation_required")) def test_code_edited_during_the_run_is_never_current(self): got = self.collect(self.base(pd.file_digest("tests/test_progress_dashboard.py"), None)) self.assertEqual(got["test_progress_dashboard.py"], ("verified", "unknown")) def test_ctest_junit_passes_age_like_lasttest_log(self): ctest_suite = '{}' sources = { "runtime/build/Testing/Temporary/LastTest.log": (100, "1/1 Test: local\nTest Passed.\n"), "before.xml": (100, ctest_suite.format( '' '')), "after.xml": (300, ctest_suite.format('')), "pytest.xml": (100, '' ''), } with tempfile.TemporaryDirectory() as root: junit = [] for name, (mtime, text) in sources.items(): path = os.path.join(root, *name.split("/")) os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(text) os.utime(path, (mtime, mtime)) if name.endswith(".xml"): junit.append(path) col = pd.Collector() with mock.patch.object(pd, "ROOT", root), mock.patch.object(pd, "last_commit_time", return_value=200): pd.collect_ctest(col, SimpleNamespace(junit=junit)) outcomes = {c["name"]: (c["status"], c["freshness"]) for c in col.components} self.assertEqual(outcomes, { "local": ("partial", "revalidation_required"), "older": ("partial", "revalidation_required"), "failed": ("failed", "failed"), "skipped": ("unknown", "unknown"), "newer": ("verified", "current"), "unrelated": ("verified", "current"), }) metric = next(m for m in col.metrics if m["id"] == "runtime.current") self.assertEqual((metric["numerator"], metric["denominator"]), (2, 6)) def test_local_ctest_junit_replaces_log_and_keeps_independent_run(self): with tempfile.TemporaryDirectory() as tmp, mock.patch.object(pd, "ROOT", tmp), \ mock.patch.object(pd, "last_commit_time", return_value=0): log = Path(tmp, "runtime/build/Testing/Temporary/LastTest.log") log.parent.mkdir(parents=True) log.write_text("1/1 Test: probe\nTest Passed.\n", encoding="utf-8") local = Path(tmp, "local.xml") local.write_text('' '', encoding="utf-8") other = Path(tmp, "other.xml") other.write_text('', encoding="utf-8") col = pd.Collector() pd.collect_ctest(col, SimpleNamespace(ctest_junit=str(local), junit=[str(local), str(other)])) self.assertEqual([(c["group"], c["status"]) for c in col.components], [("ctest", "unknown"), ("junit:other.xml", "verified")]) metric = next(m for m in col.metrics if m["id"] == "runtime.current") self.assertEqual((metric["numerator"], metric["denominator"]), (1, 2)) for path, status in [(other, "invalid"), (Path(tmp, "absent.xml"), "missing")]: with self.subTest(status=status): col = pd.Collector() pd.collect_ctest(col, SimpleNamespace(ctest_junit=str(path), junit=[])) self.assertEqual(col.components, []) self.assertEqual([(s["id"], s["status"]) for s in col.sources], [("ctest_junit", status)]) class GpuLog(unittest.TestCase): LOG = "\n".join([ "using runtime/build/RelWithDebInfo", "cs_bounds ok", "ps_texoff SKIP (VK_KHR_maintenance8 missing)", "cs_bpermute32 lane 3: got 0x00000000, want 0x00000007", "2 of 3 pass, 1 skipped", ]) def test_lines_and_summary(self): rows, summary = pd.parse_gpu_log(self.LOG) self.assertEqual([(n, r) for n, r, _ in rows], [("cs_bounds", "pass"), ("ps_texoff", "skip"), ("cs_bpermute32", "fail")]) self.assertIn("want 0x00000007", rows[2][2]) self.assertEqual(summary, (2, 3, 1)) def test_panel_groups_by_gpu_label(self): with tempfile.TemporaryDirectory() as tmp: log = os.path.join(tmp, "check.txt") with open(log, "w", encoding="utf-8", newline="\n") as f: f.write(self.LOG + "\n") col = pd.Collector() pd.collect_gpu(col, [log + "=RTX 4060, 576.02, Windows 11"]) self.assertEqual({c["group"] for c in col.components}, {"RTX 4060, 576.02, Windows 11"}) self.assertEqual(sorted(c["status"] for c in col.components)[0], "failed") m = next(m for m in col.metrics if m["id"] == "gpu.current") self.assertEqual(m["denominator"], 2) # the skip is not counted class Github(unittest.TestCase): def test_default_repo_is_the_one_the_readme_clones(self): # --github once read a repository that had gone private; the README is where the address is kept current with open(os.path.join(ROOT, "README.md"), encoding="utf-8") as f: self.assertIn(f"`git clone --recursive https://github.com/{pd.DEFAULT_REPO}`", f.read()) def test_d3d_states(self): self.assertEqual(pd.d3d_state("@a", "#291 merged, #819, #831 on main", "")[0], "partial") self.assertEqual(pd.d3d_state("@a", "#1489 merged, #907 closed", "")[0], "implemented") self.assertEqual(pd.d3d_state("@a", "—", "")[0], "unknown") self.assertEqual(pd.d3d_state("—", "—", "")[0], "missing") def test_d3d_states_follow_the_merged_list(self): merged, open_prs = {819, 831, 291}, {900} # the table still lists #819 without "merged", but it merged: the item is implemented self.assertEqual(pd.d3d_state("@a", "#291 merged, #819", "PR open", merged, open_prs)[0], "implemented") self.assertEqual(pd.d3d_state("@a", "#819, #900", "", merged, open_prs)[0], "partial") # a bare reference that is neither merged nor open was closed without merging: it does not count self.assertEqual(pd.d3d_state("@a", "#777", "", merged, open_prs)[0], "unknown") self.assertEqual(pd.d3d_state("@a", "—", "PR open", merged, open_prs)[0], "partial") def test_item_ids(self): self.assertEqual(pd.item_ids("D3D-86: answer D3D12_OPTIONS1 in CheckFeatureSupport"), ["D3D-86"]) self.assertEqual(pd.item_ids("pknorm_probe: NaN gives 0 (SHADER-339)"), ["SHADER-339"]) self.assertEqual(pd.item_ids("Docs/Tests: SHADER-4 and SHADER-4 again, TEST-9"), ["SHADER-4", "TEST-9"]) self.assertEqual(pd.item_ids("hash with SHA-256, write UTF-8, read OPTIONS2-1 and D3D12-3"), []) self.assertEqual(pd.item_ids(None), []) def test_d3d_metric_counts_merged_pull_requests_not_only_table_rows(self): body = ("| ID | Title | Owner | PR | Status |\n|---|---|---|---|---|\n" "| D3D-1 | a | @a | #50 | PR open |\n| D3D-2 | b | @b | #51 | PR open |\n") merged = [{"number": n, "title": f"D3D-{n}: x", "mergedAt": "2026-11-22T10:00:00Z", "author": {"login": "x"}} for n in (10, 11, 12)] open_prs = [{"number": 50, "title": "D3D-1: a", "isDraft": False, "author": {"login": "x"}}, {"number": 51, "title": "D3D-2: b", "isDraft": False, "author": {"login": "x"}}] def fake_gh(args, timeout=60): if args[:2] == ["pr", "list"]: return merged if "merged" in args else open_prs if args[:2] == ["issue", "view"]: return {"body": body, "updatedAt": "2026-11-22T00:00:00Z"} if args[:2] == ["issue", "list"] and "D3D12" in args[args.index("--search") + 1]: return [{"number": 313, "title": "D3D12 → Vulkan layer: critical path, who builds what"}] return [] col = pd.Collector() with mock.patch.object(pd, "gh_json", fake_gh), mock.patch.object(pd.shutil, "which", lambda _: "/usr/bin/gh"): pd.collect_github(col, "o/r", "me") m = next(m for m in col.metrics if m["id"] == "d3d12.merged") self.assertEqual((m["numerator"], m["denominator"]), (3, 5)) def run_d3d_metric(self, body, merged, open_prs): """The d3d12.merged metric for a #313 body and the repository's merged and open PR lists (None: unreadable).""" def fake_gh(args, timeout=60): if args[:2] == ["pr", "list"]: return merged if "merged" in args else open_prs if args[:2] == ["issue", "view"]: return {"body": body, "updatedAt": "2026-11-22T00:00:00Z"} if args[:2] == ["issue", "list"] and "D3D12" in args[args.index("--search") + 1]: return [{"number": 313, "title": "D3D12 → Vulkan layer: critical path, who builds what"}] return [] col = pd.Collector() with mock.patch.object(pd, "gh_json", fake_gh), mock.patch.object(pd.shutil, "which", lambda _: "/usr/bin/gh"): pd.collect_github(col, "o/r", "me") return next(m for m in col.metrics if m["id"] == "d3d12.merged") D3D_BODY = ("| ID | Title | Owner | PR | Status |\n|---|---|---|---|---|\n" "| D3D-4 | b | @b | #60 merged | merged |\n| D3D-5 | c | @c | #61 | PR open |\n") def run_d3d_lookup(self, issues, d3d_issue=None, readable=True): """collect_github() with these open issues answering the D3D12 title search: the collector, the summary and every gh call made.""" calls = [] def fake_gh(args, timeout=60): calls.append(args) if args[:2] == ["issue", "view"]: return {"body": self.D3D_BODY, "updatedAt": "2026-11-22T00:00:00Z"} if readable else None if args[:2] == ["issue", "list"] and "D3D12" in args[args.index("--search") + 1]: return issues return [] col = pd.Collector() with mock.patch.object(pd, "gh_json", fake_gh), mock.patch.object(pd.shutil, "which", lambda _: "/usr/bin/gh"): summary = pd.collect_github(col, "o/r", None, d3d_issue) return col, summary, calls @staticmethod def viewed(calls): return [a[2] for a in calls if a[:2] == ["issue", "view"]] def test_d3d_issue_is_found_by_title_not_by_a_fixed_number(self): # #313 was the tracking issue in the repository the project started in; here that number is something else col, summary, calls = self.run_d3d_lookup([ {"number": 12, "title": "d3d12_vulkan_device: a crash in the Vulkan layer"}, {"number": 90, "title": "D3D12 -> Vulkan layer: critical path (continues the one below)"}, {"number": 41, "title": "D3D12 → Vulkan layer: critical path, who builds what, open methods"}, ]) self.assertEqual(self.viewed(calls), ["41"]) # the oldest issue with the title; never "313" self.assertEqual(summary["d3d_issue"], 41) self.assertEqual(col.panels["d3d12"]["title"], "D3D12 → Vulkan layer items (#41)") source = next(s for s in col.sources if s["id"] == "github_d3d12") self.assertEqual((source["status"], source["records"]), ("ok", 2)) self.assertTrue(any(m["id"] == "d3d12.merged" for m in col.metrics)) def test_no_d3d_issue_is_reported_and_no_other_issue_is_read(self): for issues in ([], None, [{"number": 313, "title": "tests: an unrelated pull request"}]): with self.subTest(issues=issues): col, summary, calls = self.run_d3d_lookup(issues) self.assertEqual(self.viewed(calls), []) self.assertNotIn("d3d_issue", summary) source = next(s for s in col.sources if s["id"] == "github_d3d12") self.assertEqual(source["status"], "missing") self.assertIn("--d3d-issue", source["note"]) self.assertEqual(col.panels["d3d12"]["title"], "D3D12 → Vulkan layer items") self.assertFalse(any(m["id"] == "d3d12.merged" for m in col.metrics)) def test_d3d_issue_option_names_the_issue(self): col, summary, calls = self.run_d3d_lookup([{"number": 41, "title": "D3D12 → Vulkan layer: x"}], d3d_issue=7) self.assertEqual(self.viewed(calls), ["7"]) self.assertFalse(any(a[:2] == ["issue", "list"] and "D3D12" in a[a.index("--search") + 1] for a in calls)) self.assertEqual(summary["d3d_issue"], 7) def test_unreadable_d3d_issue_is_reported(self): col, _, _ = self.run_d3d_lookup([], d3d_issue=7, readable=False) source = next(s for s in col.sources if s["id"] == "github_d3d12") self.assertEqual(source["status"], "invalid") self.assertIn("#7", source["note"]) def test_d3d_issue_option_is_checked(self): for argv in (["--out", "x", "--d3d-issue", "7"], ["--out", "x", "--github", "--d3d-issue", "0"]): with self.subTest(argv=argv), contextlib.redirect_stderr(io.StringIO()) as err: with self.assertRaises(SystemExit) as stop: pd.main(argv) self.assertEqual(stop.exception.code, 2) self.assertIn("--d3d-issue", err.getvalue()) @staticmethod def prs(*pairs): return [{"number": n, "title": t, "isDraft": False, "mergedAt": "2026-11-22T10:00:00Z", "author": {"login": "x"}} for n, t in pairs] def test_d3d_id_with_an_open_pr_is_not_counted_as_merged(self): body = ("| ID | Title | Owner | PR | Status |\n|---|---|---|---|---|\n" "| D3D-3 | a | @a | #819 | PR open |\n| D3D-4 | b | @b | #60 merged | merged |\n") merged = self.prs((291, "D3D-3: first part"), (60, "D3D-4: b"), (292, "D3D-20: c"), (293, "D3D-21: d")) open_prs = self.prs((819, "D3D-3: rest"), (820, "D3D-20: more")) m = self.run_d3d_metric(body, merged, open_prs) # D3D-4 (table: merged) and D3D-21 (merged PR, no open PR); not D3D-3 (table: open) or D3D-20 (open PR) self.assertEqual((m["numerator"], m["denominator"]), (2, 4)) self.assertNotIn("note", m) def test_d3d_metric_says_so_when_a_pr_list_could_not_be_read(self): body = ("| ID | Title | Owner | PR | Status |\n|---|---|---|---|---|\n" "| D3D-4 | b | @b | #60 merged | merged |\n| D3D-5 | c | @c | #61 | PR open |\n") m = self.run_d3d_metric(body, self.prs((70, "D3D-9: x")), None) self.assertEqual((m["numerator"], m["denominator"]), (1, 2)) # table rows only self.assertIn("partial", m["title"]) self.assertIn("could not be read", m["note"]) def test_merged_pull_requests_are_implemented(self): merged = [{"number": 10, "title": "D3D-1: a", "mergedAt": "2026-11-22T10:00:00Z", "author": {"login": "x"}}, {"number": 12, "title": "docs: b", "mergedAt": "2026-11-22T12:00:00Z", "author": {"login": "y"}}] open_prs = [{"number": 13, "title": "D3D-2: c", "isDraft": True, "author": {"login": "z"}}] col = pd.Collector() latest = pd.collect_pulls(col, "o/r", merged, open_prs) got = {c["number"]: (c["status"], c["group"]) for c in col.components} self.assertEqual(got, {10: ("implemented", "D3D items"), 12: ("implemented", "no item ID"), 13: ("partial", "D3D items")}) self.assertEqual([p["number"] for p in latest], [12, 10]) # newest merge first m = {m["id"]: (m["numerator"], m["denominator"]) for m in col.metrics} self.assertEqual(m["merged.prs"], (2, 3)) self.assertEqual(m["merged.items"], (1, 2)) self.assertNotIn("merged_at", next(c for c in col.components if c["number"] == 13)) def test_pull_request_limit_is_reported(self): col = pd.Collector() pd.collect_pulls(col, "o/r", [{"number": 1, "title": "a", "mergedAt": "x"}], [], limit=1) self.assertIn("limit was reached", col.sources[0]["note"]) class Corpus(unittest.TestCase): def test_counts_only_never_names(self): with tempfile.TemporaryDirectory() as tmp: work = os.path.join(tmp, "work") sh = os.path.join(work, "shaders") # make_game.py's layout os.makedirs(os.path.join(sh, "dxbc")) os.makedirs(os.path.join(sh, "spirv")) carved = [{"name": f"{SENTINEL}_{i}", "stage": "pixel" if i < 3 else "compute"} for i in range(5)] with open(os.path.join(sh, "dxbc", "index.json"), "w", encoding="utf-8", newline="\n") as f: json.dump(carved, f) with open(os.path.join(sh, "spirv", "index.json"), "w", encoding="utf-8", newline="\n") as f: json.dump({f"{SENTINEL}_0": {"stage": "pixel"}, f"{SENTINEL}_3": {"stage": "compute"}, f"{SENTINEL}_4": {"error": "x"}}, f) col = pd.Collector() pd.collect_corpus(col, work) got = {(c["group"], c["status"]): c["count"] for c in col.components} self.assertEqual(got, {("pixel", "implemented"): 1, ("pixel", "failed"): 2, ("compute", "implemented"): 1, ("compute", "failed"): 1}) m = next(m for m in col.metrics if m["id"] == "corpus.validated") self.assertEqual((m["numerator"], m["denominator"]), (2, 5)) # everything the collector holds can reach the page (notes and panels included), so check all of it held = json.dumps(vars(col), default=str) self.assertNotIn(SENTINEL, held) self.assertNotIn(tmp, held) self.assertNotIn(json.dumps(tmp)[1:-1], held) class Scrub(unittest.TestCase): def test_paths_outside_the_repository_lose_their_file_name(self): sep = os.sep work = os.path.join(tempfile.gettempdir(), "work", "shaders", "dxbc", f"{SENTINEL}.fssm") self.assertEqual(pd.scrub(f"FAIL: cannot read {work}"), "FAIL: cannot read ") self.assertEqual(pd.scrub(f"see /home/someone/work/{SENTINEL}.fssm now"), "see now") self.assertEqual(pd.scrub("in " + os.path.join(pd.HOME, "work", f"{SENTINEL}.fssm")), "in ") self.assertEqual(pd.scrub(f"C:{sep}work{sep}{SENTINEL}_a.fssm, C:{sep}{SENTINEL}_b.fssm"), " ") def test_drive_paths_with_spaces_lose_their_whole_name(self): for line, want in ( (rf"FAIL: cannot read D:\My work\shaders\{SENTINEL}.fssm", "FAIL: cannot read "), (rf"D:\My work\a b c\{SENTINEL}.fssm line 3", " line 3"), (rf"D:\My work\{SENTINEL}_a.fssm, D:\Other work\{SENTINEL}_b.fssm", " "), (f"FAIL: cannot read D:/My work/shaders/{SENTINEL}.fssm", "FAIL: cannot read "), (rf"cannot read D:/My work\fixtures\{SENTINEL}.txt", "cannot read "), (rf"cannot read D:\My work/stubs/{SENTINEL}.spv", "cannot read "), (rf"D:\My own work/sub dir\a b/{SENTINEL}.fssm line 3", " line 3"), (rf"D:\My work/{SENTINEL}_a.fssm, D:/Other work\{SENTINEL}_b.fssm", " "), (rf"Permission denied: 'D:\\My work\\{SENTINEL} one.fssm'", "Permission denied: ''"), (rf'File "D:\My work\{SENTINEL} one.py", line 3', 'File "", line 3'), (r"D:\work\a.txt is not a folder", " is not a folder"), (r"D:\work\a.txt, then tests/test_x.py failed", " then tests/test_x.py failed"), (r"D:\work\a.txt and tests/test_x.py failed", " and tests/test_x.py failed"), (r"D:\work\a.txt is 3/4 done", " is 3/4 done"), # The same separator, after a file name: the prose stays too. ("D:/work/a.txt and tests/test_x.py failed", " and tests/test_x.py failed"), (r"D:\work\a.txt and tests\test_x.py failed", r" and tests\test_x.py failed"), ("D:/work/a.txt is 3/4 done", " is 3/4 done"), (f"D:/My work/Python 3.12 x/{SENTINEL}.fssm line 3", " line 3"), ): with self.subTest(line=line): self.assertEqual(pd.scrub(line), want) if os.name == "nt": sibling = os.path.join(ROOT + " work", f"{SENTINEL} one.fssm") self.assertEqual(pd.scrub(f"cannot read '{sibling}' now"), "cannot read '' now") def test_paths_inside_the_repository_become_relative(self): line = "Error: " + os.path.join(ROOT, "tools", "text_tables.py") + " line 3" self.assertEqual(pd.scrub(line), "Error: tools/text_tables.py line 3") def test_checkout_path_with_spaces_becomes_relative(self): with tempfile.TemporaryDirectory() as tmp: checkout = os.path.join(tmp, "Checkout with spaces [1]") path = os.path.join(checkout, "tools", "text_tables.py") os.makedirs(os.path.dirname(path)) with open(path, "wb"): pass with mock.patch.object(pd, "ROOT", checkout): self.assertEqual(pd.scrub(f"Error: {path} line 3"), "Error: tools/text_tables.py line 3") self.assertEqual(pd.scrub(f'File "{path}", line 3'), 'File "tools/text_tables.py", line 3') self.assertEqual(pd.scrub(f"Checkout: {checkout}"), "Checkout: .") self.assertEqual(pd.scrub(f"Checkout {checkout} is ready"), "Checkout . is ready") sibling = os.path.join(checkout + "x", f"{SENTINEL}.fssm") self.assertEqual(pd.scrub(f"Error: {sibling} line 3"), "Error: line 3") if os.name == "nt": alternate = os.path.join(checkout.swapcase(), "tools", "text_tables.py").replace("\\", "/") self.assertEqual(pd.scrub(f"Error: {alternate} line 3"), "Error: tools/text_tables.py line 3") def test_urls_and_relative_paths_stay(self): self.assertEqual(pd.scrub("see https://github.com/ItsNotRexed/Leonida/issues/1"), "see https://github.com/ItsNotRexed/Leonida/issues/1") self.assertEqual(pd.scrub("tests/test_x.py failed"), "tests/test_x.py failed") class Outputs(unittest.TestCase): def test_login_without_github_is_an_argument_error(self): with tempfile.TemporaryDirectory() as tmp: out = os.path.join(tmp, "out") cmd = [sys.executable, "-I", TOOL, "--out", out, "--login", "fixture-reviewer"] result = subprocess.run(cmd, capture_output=True, cwd=ROOT, timeout=120) self.assertEqual(result.returncode, 2, result.stderr.decode("utf-8", "replace")) self.assertIn(b"--login requires --github", result.stderr) self.assertFalse(os.path.exists(out)) def test_nonpositive_test_options_are_argument_errors(self): with tempfile.TemporaryDirectory() as tmp: out = os.path.join(tmp, "out") for option in ("--jobs", "--test-timeout"): for value in ("0", "-1"): with self.subTest(option=option, value=value): cmd = [sys.executable, "-I", TOOL, "--out", out, "--run-tests", "--tests", "test_progress_dashboard", option, value] result = subprocess.run(cmd, capture_output=True, cwd=ROOT, timeout=120) self.assertEqual(result.returncode, 2, result.stderr.decode("utf-8", "replace")) self.assertIn(f"{option} must be a positive integer".encode("ascii"), result.stderr) self.assertFalse(os.path.exists(out)) def test_invalid_test_filter_is_an_argument_error(self): with tempfile.TemporaryDirectory() as tmp: out = os.path.join(tmp, "out") result = subprocess.run([sys.executable, "-I", TOOL, "--out", out, "--run-tests", "--tests", "["], capture_output=True, cwd=ROOT, timeout=120) self.assertEqual(result.returncode, 2, result.stderr.decode("utf-8", "replace")) self.assertIn(b"--tests", result.stderr) self.assertIn(b"unterminated character set", result.stderr) self.assertNotIn(b"Traceback", result.stderr) self.assertFalse(os.path.exists(out)) def test_out_inside_the_repository_is_refused(self): r = subprocess.run([sys.executable, "-I", TOOL, "--out", os.path.join(ROOT, "progress_out_test")], capture_output=True, cwd=ROOT, timeout=120) self.assertEqual(r.returncode, 2) self.assertFalse(os.path.exists(os.path.join(ROOT, "progress_out_test"))) def test_save_text_refreshes_when_fixed_temp_name_is_a_directory(self): with tempfile.TemporaryDirectory() as tmp: out = os.path.join(tmp, "report.json") stale = out + ".tmp" os.mkdir(stale) pd.save_text(out, '{"ok": 1}\n') with open(out, encoding="utf-8") as f: self.assertEqual(f.read(), '{"ok": 1}\n') self.assertTrue(os.path.isdir(stale)) self.assertEqual([n for n in os.listdir(tmp) if n.startswith("report.json.")], ["report.json.tmp"]) def test_page_snapshot_history_and_dedupe(self): with tempfile.TemporaryDirectory() as tmp: out = os.path.join(tmp, "out") cmd = [sys.executable, "-I", TOOL, "--out", out] r1 = subprocess.run(cmd, capture_output=True, cwd=ROOT, timeout=300) self.assertEqual(r1.returncode, 0, r1.stderr.decode("utf-8", "replace")) with open(os.path.join(out, "progress.html"), encoding="utf-8") as f: page = f.read() self.assertNotIn("/*__PROGRESS_DATA__*/null", page) self.assertNotIn("/*__PROGRESS_LOGO__*/", page) if os.path.isfile(pd.LOGO): # the logo is embedded: the page needs no other file self.assertIn('src="data:image/png;base64,', page) self.assertIn('"schema": "leonida.progress/1"', page) with open(os.path.join(out, "progress.html"), "rb") as f: self.assertNotIn(b"\r\n", f.read()) # LF on every platform r2 = subprocess.run(cmd, capture_output=True, cwd=ROOT, timeout=300) self.assertIn(b"no change since the last snapshot", r2.stdout) self.assertEqual(len(os.listdir(os.path.join(out, "snapshots"))), 1) with open(os.path.join(out, "history.json"), encoding="utf-8") as f: self.assertEqual(len(json.load(f)), 1) def test_different_snapshots_in_the_same_second_are_preserved(self): first = {"generated_at": "2026-11-24T00:00:00Z", "git": {"head": "a" * 40}, "metrics": [], "content_hash": "a" * 64} second = {**first, "git": {"head": "b" * 40}, "content_hash": "b" * 64} with tempfile.TemporaryDirectory() as out: self.assertTrue(pd.write_outputs(first, out)) self.assertTrue(pd.write_outputs(second, out)) self.assertFalse(pd.write_outputs(second, out)) folder = Path(out) snapshots = [json.loads(path.read_text(encoding="utf-8")) for path in (folder / "snapshots").glob("progress_*.json")] self.assertCountEqual(snapshots, [first, second]) history = json.loads((folder / "history.json").read_text(encoding="utf-8")) self.assertEqual(history, [pd.summary_line(first), pd.summary_line(second)]) def test_unmatched_test_filter_preserves_cached_results(self): with tempfile.TemporaryDirectory() as out: cached = os.path.join(out, "unit_results.json") previous = b'{"results":[{"test":"tests/test_progress_dashboard.py","result":"pass"}]}\n' with open(cached, "wb") as f: f.write(previous) cmd = [sys.executable, "-I", TOOL, "--out", out, "--run-tests", "--tests", "^no-such-fixture$"] result = subprocess.run(cmd, capture_output=True, cwd=ROOT, timeout=120) self.assertEqual(result.returncode, 2, result.stderr.decode("utf-8", "replace")) self.assertIn(b"--tests did not match any test files", result.stderr) with open(cached, "rb") as f: self.assertEqual(f.read(), previous) def test_missing_logo_leaves_no_image(self): old = pd.LOGO try: pd.LOGO = os.path.join(ROOT, "assets", "no_such_logo.png") self.assertEqual(pd.logo_src(), "") self.assertIn('src=""', pd.render_page({"schema": pd.SCHEMA}, [])) finally: pd.LOGO = old def test_script_element_cannot_be_closed_by_data(self): snap = {"schema": pd.SCHEMA, "x": ""} self.assertNotIn("", pd.render_page(snap, [])) def test_out_as_existing_file_is_refused(self): with tempfile.TemporaryDirectory() as tmp: file_out = os.path.join(tmp, "not_a_dir") with open(file_out, "wb") as f: f.write(b"x") err = io.StringIO() with contextlib.redirect_stderr(err): rc = pd.main(["--out", file_out]) self.assertEqual(rc, 2) self.assertIn("is not a directory", err.getvalue()) class Hardening(unittest.TestCase): @staticmethod def _minimal_snap(): return { "schema": pd.SCHEMA, "generated_at": "2026-11-24T10:00:00Z", "git": {}, "panels": [], "counts": {}, "components": [], "metrics": [], "sources": [], "notes": [], "github": {}, "content_hash": "0" * 64, } def test_github_login_skips_null_author_and_null_body(self): def fake_gh(args, timeout=60): if args[:2] == ["issue", "list"] and "Coordinating" in args[args.index("--search") + 1]: return [{"number": 14, "title": "Coordinating who works on what", "updatedAt": "2026-11-24T00:00:00Z"}] if args[:2] == ["issue", "view"] and args[2] == "14": fields = args[args.index("--json") + 1] if fields == "body": return {"body": "### Up for grabs\n| X-1 |\n"} if fields == "comments": return {"comments": [ {"author": None, "body": "claiming: X", "createdAt": "2026-11-24T01:00:00Z"}, {"author": {"login": "alice"}, "body": None, "createdAt": "2026-11-24T01:01:00Z"}, {"author": {"login": "alice"}, "body": "claiming: Y", "createdAt": "2026-11-24T01:02:00Z"}, ]} return [] col = pd.Collector() with mock.patch.object(pd, "gh_json", fake_gh), mock.patch.object(pd.shutil, "which", lambda _: "/usr/bin/gh"): summary = pd.collect_github(col, "o/r", "alice") self.assertEqual(summary["mine"]["claims"], [{"at": "2026-11-24T01:02:00Z", "issue": 14, "text": "claiming: Y"}]) def test_tests_flag_requires_run_tests(self): with tempfile.TemporaryDirectory() as tmp: err = io.StringIO() with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as stop: pd.main(["--out", os.path.join(tmp, "out"), "--tests", "foo"]) self.assertEqual(stop.exception.code, 2) self.assertIn("--tests requires --run-tests", err.getvalue()) def test_ci_test_files_oserror_exits_2_cleanly(self): with tempfile.TemporaryDirectory() as tmp: err = io.StringIO() with mock.patch.object(pd, "ci_test_files", side_effect=PermissionError(13, "denied", "tests/blocked")), \ contextlib.redirect_stderr(err): rc = pd.main(["--out", os.path.join(tmp, "out"), "--run-tests"]) self.assertEqual(rc, 2) self.assertIn("denied", err.getvalue()) def test_corrupt_non_list_and_bom_history_json(self): snap = self._minimal_snap() with tempfile.TemporaryDirectory() as tmp: out = os.path.join(tmp, "out") os.makedirs(out) hist = Path(out, "history.json") for bad in ( b"{bad", b"{}", b'[{"content_hash":"old"}]', b'[{"content_hash":"old","metrics":null}]', b'[{"content_hash":"old","metrics":[]}]', b'[{"content_hash":null,"metrics":{}}]', ): hist.write_bytes(bad) err = io.StringIO() with mock.patch.object(pd, "build", return_value=snap), contextlib.redirect_stderr(err): rc = pd.main(["--out", out]) self.assertEqual(rc, 2) self.assertIn("error:", err.getvalue()) self.assertEqual(hist.read_bytes(), bad) self.assertFalse(Path(out, "progress.html").exists()) valid = [{"generated_at": "2026-11-24T09:00:00Z", "head": "abc", "content_hash": "0" * 16, "metrics": {}}] hist.write_bytes(b"\xef\xbb\xbf" + json.dumps(valid).encode("utf-8")) out_s, err = io.StringIO(), io.StringIO() with mock.patch.object(pd, "build", return_value=snap), \ contextlib.redirect_stdout(out_s), contextlib.redirect_stderr(err): rc = pd.main(["--out", out]) self.assertEqual(rc, 0, err.getvalue()) def test_write_outputs_oserror_exits_2_cleanly(self): snap = self._minimal_snap() with tempfile.TemporaryDirectory() as tmp: err = io.StringIO() with mock.patch.object(pd, "build", return_value=snap), \ mock.patch.object(pd, "write_outputs", side_effect=PermissionError(13, "read-only", "progress.html")), \ contextlib.redirect_stderr(err): rc = pd.main(["--out", os.path.join(tmp, "out")]) self.assertEqual(rc, 2) self.assertIn("read-only", err.getvalue()) if __name__ == "__main__": unittest.main()