"""tools/exe_checklist.py: the ASSUMPTION / UNVERIFIED / VERIFY notes, gathered for the executable's first look. The repository here is a small tree built in a temporary folder (one shim, one doc), so the counts are exact; the last test runs the tool on this repository to make sure every note it finds has a file, a line and some text. Standard library only; no game files. """ import contextlib import io import json import os from pathlib import Path import sys import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import exe_checklist as ec # noqa: E402 SHIM = """// demo: a shim with two notes. // // ASSUMPTION: the quota is 1 GiB until the executable // shows its own. #include constexpr GUID kDemoClass = {0x704c3f58, 0xe629, 0x4cc2, {0xb1, 0x97, 0x30, 0x51, 0x1b, 0x99, 0x6f, 0xe2}}; constexpr size_t kSize = 18; // UNVERIFIED, see the header // The SKU. UNVERIFIED: nothing // compares it. int f(); """ OTHER = """// no class here // ASSUMPTION: a note in a file that serves no runtime class. """ DOC = """# Demo Some text. - **Names:** fine. - **VERIFY:** the container names, from the executable's calls. **ASSUMPTION:** a paragraph note that spans two lines. - **Unverified:** the same note in the docs' prose spelling. - **Assumption** — the term's definition, not a note. - **Assumptions to confirm:** the plural spelling is a note too. """ SAVE_CLASS = "704c3f58-e629-4cc2-b197-30511b996fe2" class ExeChecklist(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.root = self.tmp.name os.makedirs(os.path.join(self.root, "runtime", "shims")) os.makedirs(os.path.join(self.root, "docs")) for rel, text in (("runtime/shims/demo.cpp", SHIM), ("runtime/shims/other.cpp", OTHER), ("docs/demo.md", DOC)): with open(os.path.join(self.root, rel), "w", encoding="utf-8", newline="\n") as f: f.write(text) def tearDown(self): self.tmp.cleanup() def test_notes_start_at_their_marker_and_skip_trailing_comments(self): notes = ec.collect(self.root) demo = [n for n in notes if n["file"] == "runtime/shims/demo.cpp"] self.assertEqual([(n["line"], n["kind"]) for n in demo], [(3, "ASSUMPTION"), (8, "UNVERIFIED")]) self.assertEqual(demo[0]["text"], "the quota is 1 GiB until the executable shows its own.") self.assertEqual(demo[1]["text"], "nothing compares it.") self.assertEqual(demo[0]["classes"], [SAVE_CLASS]) def test_one_class_scan_per_file_with_several_notes(self): many = "".join(f"// ASSUMPTION: note {i}\nint value;\n" for i in range(4)) with open(os.path.join(self.root, "runtime/shims/many.cpp"), "w", encoding="utf-8", newline="\n") as f: f.write(SHIM + many) seen, real = [], ec.served_classes def counting(source): seen.append(source) return real(source) ec.served_classes = counting try: notes = [n for n in ec.collect(self.root) if n["file"] == "runtime/shims/many.cpp"] finally: ec.served_classes = real self.assertEqual(len(seen), 3) # one scan per file with notes (demo.cpp, other.cpp, many.cpp) self.assertEqual(sum(1 for s in seen if "note 0" in s), 1) # not once per note self.assertEqual(len(notes), 6) # two in the header, four added self.assertEqual({tuple(n["classes"]) for n in notes}, {(SAVE_CLASS,)}) def test_doc_notes_are_the_list_item_or_paragraph(self): docs = [n for n in ec.collect(self.root) if n["file"] == "docs/demo.md"] self.assertEqual([(n["line"], n["kind"]) for n in docs], [(6, "VERIFY"), (9, "ASSUMPTION"), (12, "UNVERIFIED"), (14, "ASSUMPTION")]) self.assertEqual(docs[0]["text"], "the container names, from the executable's calls.") self.assertEqual(docs[1]["text"], "a paragraph note that spans two lines.") self.assertEqual(docs[2]["text"], "the same note in the docs' prose spelling.") self.assertEqual(docs[3]["text"], "the plural spelling is a note too.") def test_doc_notes_stop_at_numbered_and_plus_list_items(self): sample = """# Numbered and Plus Lists 1. **VERIFY:** first numbered note continuation line. 2. **VERIFY:** second numbered note. 3) **UNVERIFIED:** parenthesis numbered note. + **ASSUMPTION:** plus list note wrapped plus line. + **VERIFY:** second plus note. """ path = os.path.join(self.root, "docs", "lists.md") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(sample) notes = ec.doc_notes(path, "docs/lists.md") self.assertEqual(len(notes), 5) self.assertEqual([(n["line"], n["kind"]) for n in notes], [(2, "VERIFY"), (4, "VERIFY"), (5, "UNVERIFIED"), (6, "ASSUMPTION"), (8, "VERIFY")]) self.assertEqual(notes[0]["text"], "first numbered note continuation line.") self.assertEqual(notes[1]["text"], "second numbered note.") self.assertEqual(notes[2]["text"], "parenthesis numbered note.") self.assertEqual(notes[3]["text"], "plus list note wrapped plus line.") self.assertEqual(notes[4]["text"], "second plus note.") def test_doc_notes_bare_markers_nested_items_and_code_blocks(self): sample = """# Edge Cases 1. **VERIFY:** first item 2. 3. **VERIFY:** third item. - **VERIFY:** parent note + nested plus continuation 1. nested ordered continuation ```python + inside code fence 1. also code ``` - **VERIFY:** prose note 2024. the year it occurred + 5 cases passed. - next item. """ path = os.path.join(self.root, "docs", "edge_cases.md") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(sample) notes = ec.doc_notes(path, "docs/edge_cases.md") self.assertEqual([(n["line"], n["kind"]) for n in notes], [(2, "VERIFY"), (4, "VERIFY"), (5, "VERIFY"), (12, "VERIFY")]) self.assertEqual(notes[0]["text"], "first item") self.assertEqual(notes[1]["text"], "third item.") self.assertEqual(notes[2]["text"], "parent note + nested plus continuation 1. nested ordered continuation") self.assertEqual(notes[3]["text"], "prose note 2024. the year it occurred + 5 cases passed.") def test_doc_notes_nested_item_with_own_marker_stops_continuation(self): sample = """# Nested note with own marker - **VERIFY:** parent note - **VERIFY:** child note - next item. """ path = os.path.join(self.root, "docs", "nested_marker.md") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(sample) notes = ec.doc_notes(path, "docs/nested_marker.md") self.assertEqual([(n["line"], n["kind"], n["text"]) for n in notes], [(2, "VERIFY", "parent note"), (3, "VERIFY", "child note")]) def test_doc_notes_tilde_fences_are_recognised(self): sample = """# Tilde fences - **VERIFY:** before tilde fence ~~~ - **VERIFY:** inside tilde fence ~~~ - **VERIFY:** after tilde fence """ path = os.path.join(self.root, "docs", "tilde_fence.md") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(sample) notes = ec.doc_notes(path, "docs/tilde_fence.md") self.assertEqual([(n["line"], n["kind"], n["text"]) for n in notes], [(2, "VERIFY", "before tilde fence"), (6, "VERIFY", "after tilde fence")]) def test_doc_notes_unclosed_fence_does_not_hide_later_notes(self): sample = """# Unclosed fence ```python x = 1 - **VERIFY:** note after unclosed fence """ path = os.path.join(self.root, "docs", "unclosed_fence.md") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(sample) notes = ec.doc_notes(path, "docs/unclosed_fence.md") self.assertEqual([(n["line"], n["kind"], n["text"]) for n in notes], [(4, "VERIFY", "note after unclosed fence")]) def test_a_marker_inside_a_sentence_or_a_table_row_is_prose(self): with open(os.path.join(self.root, "docs", "demo.md"), "a", encoding="utf-8", newline="\n") as f: f.write("| `v_demo` | written without the console (**Assumption**: not a note) |\n" "| **Unverified:** nothing | `v_other` |\n" "\n" "A sentence that carries an **Assumption:** after its start is not a note either.\n" "\n" "- **A list item** whose next sentence says **Unverified:** this is not a note of its own.\n") docs = [n for n in ec.collect(self.root) if n["file"] == "docs/demo.md"] self.assertEqual([(n["line"], n["kind"]) for n in docs], [(6, "VERIFY"), (9, "ASSUMPTION"), (12, "UNVERIFIED"), (14, "ASSUMPTION")]) def test_called_notes_come_first(self): report = os.path.join(self.root, "x.gdk_calls.json") with open(report, "w", encoding="utf-8") as f: json.dump({"images": [{"classes": [{"class_id": SAVE_CLASS.upper()}]}]}, f) out = os.path.join(self.root, "checklist.md") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(ec.main(["--root", self.root, "--gdk-calls", report, "--out", out]), 0) with open(out, "rb") as f: raw = f.read() self.assertNotIn(b"\r\n", raw) lines = [l for l in raw.decode("utf-8").split("\n") if l.startswith("- [ ]")] self.assertIn("demo.cpp:3` ASSUMPTION **called**", lines[0]) self.assertIn("demo.cpp:8` UNVERIFIED **called**", lines[1]) self.assertIn("other.cpp:2` ASSUMPTION:", lines[2]) # serves no class: no tag, after the called ones self.assertEqual(len(lines), 7) def test_a_class_the_executable_does_not_call(self): report = os.path.join(self.root, "y.json") with open(report, "w", encoding="utf-8") as f: json.dump({"classes": [{"class_id": "00000000-0000-0000-0000-000000000001"}]}, f) # one image, no "images" text = ec.render(ec.collect(self.root), ec.called_classes([report])) self.assertIn("demo.cpp:3` ASSUMPTION (not called)", text) def test_verify_notes_in_code_are_collected(self): verify = "// pad demo.\n//\n// The pads (VERIFY: against the game).\n// VERIFY: the timeout is not honoured.\n" with open(os.path.join(self.root, "runtime", "shims", "verify.cpp"), "w", encoding="utf-8", newline="\n") as f: f.write(verify) notes = [n for n in ec.collect(self.root) if n["file"] == "runtime/shims/verify.cpp"] self.assertEqual([(n["line"], n["kind"]) for n in notes], [(3, "VERIFY"), (4, "VERIFY")]) self.assertEqual(notes[0]["text"], "against the game).") self.assertEqual(notes[1]["text"], "the timeout is not honoured.") def test_a_root_that_is_not_a_directory_is_exit_2(self): stderr = io.StringIO() with contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(io.StringIO()): self.assertEqual(ec.main(["--root", os.path.join(self.root, "no_such")]), 2) self.assertIn("not a directory", stderr.getvalue()) def test_an_out_that_is_a_directory_is_exit_2(self): stderr = io.StringIO() with contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(io.StringIO()): self.assertEqual(ec.main(["--root", self.root, "--out", os.path.join(self.root, "docs")]), 2) self.assertIn("is a directory", stderr.getvalue()) def test_an_unreadable_report_is_exit_1(self): stderr = io.StringIO() with contextlib.redirect_stderr(stderr): self.assertEqual(ec.main(["--root", self.root, "--gdk-calls", os.path.join(self.root, "missing.json")]), 1) self.assertIn("cannot read", stderr.getvalue()) def assert_report_output_rejected(self, reports, output): original = [report.read_bytes() for report in reports] args = ["--root", self.root, "--out", str(output)] for report in reports: args.extend(["--gdk-calls", str(report)]) stderr = io.StringIO() with contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(io.StringIO()): with self.assertRaises(SystemExit) as raised: ec.main(args) self.assertEqual(raised.exception.code, 2) self.assertIn("would overwrite", stderr.getvalue()) self.assertEqual([report.read_bytes() for report in reports], original) def test_an_out_that_cannot_be_written_is_exit_1(self): # --out under a folder that does not exist was a FileNotFoundError traceback. out = os.path.join(self.root, "missing_dir", "notes.md") stderr = io.StringIO() with contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(io.StringIO()): self.assertEqual(ec.main(["--root", self.root, "--out", out]), 1) self.assertIn("cannot write the checklist", stderr.getvalue()) def test_out_cannot_overwrite_any_input_report(self): first = Path(self.root, "first.json") second = Path(self.root, "second.json") first.write_bytes(b'{"classes": []}') second.write_bytes(b'{"images": []}') self.assert_report_output_rejected([first, second], second) def test_out_cannot_overwrite_a_hard_link_to_an_input_report(self): report = Path(self.root, "report.json") alias = Path(self.root, "output.md") report.write_bytes(b'{"classes": []}') try: os.link(report, alias) except (OSError, NotImplementedError) as err: self.skipTest(f"hard links unavailable: {err}") self.assert_report_output_rejected([report], alias) def test_this_repository(self): notes = ec.collect(ROOT) self.assertTrue(notes) for n in notes: self.assertTrue(n["text"].strip(), n) self.assertGreater(n["line"], 0) # the store shim's remaining unverified sizes (SKU_ID_SIZE, trial-id bound) are on the list self.assertTrue(any(n["file"] == "runtime/shims/xgameruntime_store.cpp" and "SKU_ID_SIZE" in n["text"] for n in notes)) self.assertTrue(any(n["file"] == "docs/runtime_imports.md" and n["kind"] == "UNVERIFIED" for n in notes)) # d3d12_vulkan.md's console assumptions carry the page-wide uppercase marker, so the first look lists them self.assertTrue(any(n["file"] == "docs/d3d12_vulkan.md" and n["kind"] == "ASSUMPTION" for n in notes)) # so does modding.md's note on the three settings only the executable can confirm self.assertTrue(any(n["file"] == "docs/modding.md" and n["kind"] == "ASSUMPTION" for n in notes)) class ExeChecklistEdgeCases(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.root = self.tmp.name os.makedirs(os.path.join(self.root, "runtime", "shims")) os.makedirs(os.path.join(self.root, "docs")) with open(os.path.join(self.root, "runtime", "shims", "demo.cpp"), "w", encoding="utf-8", newline="\n") as f: f.write(SHIM) def tearDown(self): self.tmp.cleanup() def test_plus_and_numbered_list_items_do_not_merge_and_strip_prefix(self): doc_path = os.path.join(self.root, "docs", "lists.md") with open(doc_path, "w", encoding="utf-8", newline="\n") as f: f.write( "1. **VERIFY:** first numbered item\n" "2. **VERIFY:** second numbered item\n" " continued on next line\n" "3) unrelated item\n" "+ **ASSUMPTION:** plus bullet item\n" "+ another bullet\n" ) notes = ec.doc_notes(doc_path, "docs/lists.md") self.assertEqual([(n["line"], n["kind"]) for n in notes], [(1, "VERIFY"), (2, "VERIFY"), (5, "ASSUMPTION")]) self.assertEqual([n["text"] for n in notes], [ "first numbered item", "second numbered item continued on next line", "plus bullet item", ]) def test_without_marker_keeps_the_note_when_the_bold_run_closes_after_it(self): for text, want in ( ("**VERIFY: check the stride** before release", "check the stride before release"), ("**VERIFY: check the offset**", "check the offset"), ("**VERIFY:** check the base", "check the base"), ("**VERIFY**: check the size", "check the size"), ("**ASSUMPTION (GFX10)**: wave32", "wave32"), ("**Note** not a marker", "**Note** not a marker"), # No colon: the bold words may be the note, so none of them is lost. ("**VERIFY the stride on a real exe**", "the stride on a real exe"), ("**Unverified fixed quota** on hardware", "fixed quota on hardware"), ): with self.subTest(text=text): self.assertEqual(ec.without_marker(text), want) def test_without_marker_preserves_text_before_later_colon_on_colonless_markers(self): doc_path = os.path.join(self.root, "docs", "colonless.md") with open(doc_path, "w", encoding="utf-8", newline="\n") as f: f.write( "- **Assumption** the table uses key: value\n" "- **Unverified** fixed 1 GiB quota\n" "\n" "**ASSUMPTION**: trailing colon outside bold\n" ) docs = ec.doc_notes(doc_path, "docs/colonless.md") self.assertEqual([n["text"] for n in docs], [ "the table uses key: value", "fixed 1 GiB quota", "trailing colon outside bold", ]) shim_path = os.path.join(self.root, "runtime", "shims", "colonless.cpp") with open(shim_path, "w", encoding="utf-8", newline="\n") as f: f.write( "// ASSUMPTION buffer uses key: value\n" "int a;\n" "// ASSUMPTION (no exe yet): keeps parenthetical strip\n" "int b;\n" "// VERIFY no colon at all\n" ) code = ec.code_notes(shim_path, "runtime/shims/colonless.cpp") self.assertEqual([n["text"] for n in code], [ "buffer uses key: value", "keeps parenthetical strip", "no colon at all", ]) def test_called_classes_reads_utf8_bom_report(self): report = os.path.join(self.root, "bom.json") payload = json.dumps({"images": [{"classes": [{"class_id": SAVE_CLASS.upper()}]}]}).encode("utf-8") with open(report, "wb") as f: f.write(b"\xef\xbb\xbf" + payload) out = os.path.join(self.root, "checklist.md") with contextlib.redirect_stdout(io.StringIO()): rc = ec.main(["--root", self.root, "--gdk-calls", report, "--out", out]) self.assertEqual(rc, 0) with open(out, encoding="utf-8") as f: text = f.read() self.assertIn("demo.cpp:3` ASSUMPTION **called**", text) def test_malformed_gdk_calls_json_shapes_exit_1(self): bad_payloads = [ "[]", '{"images": null}', '{"images": ["bad"]}', '{"classes": null}', '{"classes": ["bad"]}', '{"classes": [{"class_id": true}]}', '{"classes": [{"class_id": 0}]}', ] for idx, payload in enumerate(bad_payloads): with self.subTest(payload=payload): report = os.path.join(self.root, f"bad_{idx}.json") with open(report, "w", encoding="utf-8", newline="\n") as f: f.write(payload) stderr = io.StringIO() with contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(io.StringIO()): rc = ec.main(["--root", self.root, "--gdk-calls", report]) self.assertEqual(rc, 1) self.assertIn("cannot read the gdk_runtime_calls report", stderr.getvalue()) if __name__ == "__main__": unittest.main()