From 17848366390d88a891d70922999d5659d4d8526a Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:12:29 -0600 Subject: [PATCH] audit: compare Cargo test inventories between two commits Testing the old #166 and #165 heads silently ran without 12 BC texture tests that main already had, because those heads predated the merge that added them; nothing flagged the smaller inventory. scripts/audit/test_inventory.py lists every test (and the ignored set) of a base and a candidate commit, each in a temporary detached worktree it always removes, and reports removed, added, newly ignored and newly un-ignored tests keyed by package, target and name. Unexplained losses exit 1; an --allow file with a reason per key explains intended ones. 951189c -> 5450612: removed 12 (all xenora-texture bc_formats), exit 1. 0cac6c4 -> 0cac6c4: 219 tests each side, no change, exit 0. Fixes #208 Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- CHANGELOG.md | 1 + docs/README.md | 1 + docs/testing/test-inventory.md | 44 ++++++ scripts/audit/test_inventory.py | 247 ++++++++++++++++++++++++++++++++ 4 files changed, 293 insertions(+) create mode 100644 docs/testing/test-inventory.md create mode 100644 scripts/audit/test_inventory.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cbf461..6f90d7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ remains unsupported. ### Added +- Added `scripts/audit/test_inventory.py`, which lists the Cargo test inventory of two commits in temporary detached worktrees and reports removed, added, newly ignored and newly un-ignored tests, failing on any loss not explained in an `--allow` file. Run against the old PR #166 head 5450612 and main 951189c, it reports the 12 BC texture tests that head was missing (exit 1); the same commit against itself reports no change. Default features only; see `docs/testing/test-inventory.md`. - Added complete gfx1030 instruction metadata generated from AMD's public RDNA 2 XML: 1,465 base forms across 21 encoding families with fields, operands, flags and allowed extension words. A committed, normalised snapshot lets `crates/isa/generate_rdna2_meta.py --check` verify the generated table offline; CI runs it. - Added `xenora_isa::meta::identify`, which finds any gfx1030 instruction's form and exact length, including literal, SDWA, DPP and NSA words, and rejects truncated, unassigned and disallowed-extension encodings. It agrees with every LLVM-assembled fixture and with LLVM 23.1.6 on a probe set covering each family and extension word. Identification does not make an instruction executable: 25 forms have semantic decoding. - Added `xenora shaders coverage [--json]` and `xenora shaders inspect ` for per-family coverage and stream listings that separate decoding failures from unsupported semantics. Each instruction is decoded at its stream offset and branch targets are checked against the whole stream's boundaries, so supported branches are no longer misreported as unsupported operands. `xenora_decoder::decode_at` decodes a single instruction for such callers. diff --git a/docs/README.md b/docs/README.md index 12b1144..b100236 100644 --- a/docs/README.md +++ b/docs/README.md @@ -32,6 +32,7 @@ either path. The [changelog](../CHANGELOG.md) records notable changes. ## Testing, validation, and benchmarks - [Engineering progress](progress.md) — Verification commands, metric definitions, and open release gates. +- [Test inventory comparison](testing/test-inventory.md) — Detect missing or newly ignored Cargo tests across commits. - [Persistent resource and translated rendering acceptance](testing/x9-acceptance.md) — Pinned upstream tests, native ARM64 results and unsupported cases. - [Baseline acceptance](testing/phase-02-baseline.md), [Phase 01 assurance](testing/phase01-assurance.md), and [independent reviews](testing/review-log.md) — Candidate-specific results and release findings. diff --git a/docs/testing/test-inventory.md b/docs/testing/test-inventory.md new file mode 100644 index 0000000..afcaa30 --- /dev/null +++ b/docs/testing/test-inventory.md @@ -0,0 +1,44 @@ +# Compare test inventories across commits + +Run this check from a Xenora checkout with Python 3.11 or later and the +native ARM64 Rust toolchain. Choose the base commit whose tests the candidate +should preserve. Comparing against `main` can catch tests missing from a +branch that predates their merge. + +```sh +python3 scripts/audit/test_inventory.py main HEAD +python3 scripts/audit/test_inventory.py main HEAD --allow intended-losses.tsv +python3 scripts/audit/test_inventory.py --self-test +``` + +The script resolves both refs to commits, then uses a temporary detached Git +worktree for each commit. It builds the workspace with the lockfile and +default features in offline mode, using Cargo's cached dependencies. It lists +tests and then lists ignored tests using the same build. It removes each +temporary worktree after listing, including when a build fails. Git needs +permission to update its worktree registry. + +Each test is identified by `package/kind:target/test_name`. The report shows +removed, added, newly ignored and newly un-ignored tests. Renames appear as a +removal and an addition. Exit code 1 means at least one unexplained removal or +newly ignored test. Exit code 0 means every loss was explained or there were +no losses. Exit code 2 means the check could not complete, for example because +a ref did not resolve or Cargo could not build it. + +To explain an intended removal or a newly ignored test, copy its exact key +from the report into a UTF-8 file, followed by a literal tab and a reason. +Use one entry per line. Blank lines and lines starting with `#` are allowed. + +```text +xenora-texture/test:bc_formats/old_case Replaced by new_case, which covers both endpoint orders. +``` + +The report still shows explained losses. Keep the reasons specific enough for +a reviewer to verify. + +The inventory covers Cargo's default workspace test targets for the host and +build configuration. Feature-gated targets such as those requiring +`gpu-tests` are outside that inventory. The check lists tests without +running their bodies or verifying CPU or GPU behavior. Benchmarks are +excluded. Rustdoc includes source positions in doctest names, so moving a +doctest can appear as a removal and an addition. diff --git a/scripts/audit/test_inventory.py b/scripts/audit/test_inventory.py new file mode 100644 index 0000000..62a9d22 --- /dev/null +++ b/scripts/audit/test_inventory.py @@ -0,0 +1,247 @@ +#!/usr/bin/env python3 +"""Compare Cargo test inventories at two Git commits.""" + +import argparse +import json +import os +from pathlib import Path +import re +import subprocess +import sys +import tempfile +import unittest + +RUNNING = re.compile(r"Running (?:unittests )?.+ \((.+)\)") +DOCTEST = re.compile(r"Doc-tests (\S+)") +TEST = re.compile(r"(.+): test") + + +def parse_list(output, metadata, checkout): + """Return (package, kind:target, test name) keys from a merged Cargo log.""" + packages = {p["id"]: p["name"] for p in metadata["packages"]} + doctests = {} + for package in metadata["packages"]: + for target in package["targets"]: + if target["doctest"]: + name = target["name"].replace("-", "_") + doctests.setdefault(name, set()).add( + (package["name"], f'doc:{target["name"]}') + ) + + executables = {} + suites = set() + tests = set() + suite = None + for line in output.splitlines(): + line = line.strip() + if line.startswith('{"reason":'): + message = json.loads(line) + if message["reason"] == "compiler-artifact": + executable = message["executable"] + package = packages.get(message["package_id"]) + if executable and package: + target = message["target"] + kind = target["kind"][0] + executables[Path(executable)] = (package, f'{kind}:{target["name"]}') + elif match := RUNNING.fullmatch(line): + executable = checkout / match[1] + suite = executables.get(executable) + if suite is None: + raise ValueError(f"no Cargo artifact for {line}") + suites.add(suite) + elif match := DOCTEST.fullmatch(line): + matches = doctests.get(match[1], set()) + if len(matches) != 1: + raise ValueError(f"cannot identify package for {line}") + suite = next(iter(matches)) + suites.add(suite) + elif match := TEST.fullmatch(line): + if suite is None: + raise ValueError(f"test listed before its target: {line}") + tests.add((*suite, match[1])) + + if not suites: + raise ValueError("Cargo listed no test targets") + return suites, tests + + +def run(args, cwd, *, merge=False): + result = subprocess.run( + args, cwd=cwd, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT if merge else subprocess.PIPE, + text=True, encoding="utf-8", + env={**os.environ, "CARGO_TERM_COLOR": "never", "GIT_NO_LAZY_FETCH": "1"}, + ) + if result.returncode: + detail = "\n".join((result.stderr or result.stdout).strip().splitlines()[-25:]) + raise RuntimeError(f"{' '.join(map(str, args))} exited {result.returncode}\n{detail}") + return result.stdout + + +def inventory(all_output, ignored_output, metadata, checkout): + suites, tests = parse_list(all_output, metadata, checkout) + ignored_suites, ignored = parse_list(ignored_output, metadata, checkout) + if suites != ignored_suites or not ignored <= tests: + raise ValueError("Cargo's full and ignored listings disagree") + return tests, ignored + + +def collect(repo, commit, checkout): + run(["git", "worktree", "add", "--detach", str(checkout), commit], repo) + try: + metadata = json.loads(run([ + "cargo", "metadata", "--offline", "--locked", "--no-deps", "--format-version", "1", + ], checkout)) + cargo = [ + "cargo", "test", "--offline", "--locked", "--workspace", + "--target-dir", str(checkout / "target"), + "--jobs", "2", "--message-format=json", "--", "--list", "--format", "terse", + ] + return inventory( + run(cargo, checkout, merge=True), + run([*cargo, "--ignored"], checkout, merge=True), + metadata, + checkout, + ) + finally: + run(["git", "worktree", "remove", "--force", str(checkout)], repo) + + +def read_allow(path): + """Read tab-separated inventory keys and reasons.""" + allowed = {} + for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): + if not line.strip() or line.lstrip().startswith("#"): + continue + key, _, reason = line.partition("\t") + parts = key.split("/", 2) + if len(parts) != 3 or not all(parts) or not reason.strip() or key in allowed: + raise ValueError(f"{path}:{number}: expected a unique test key and a tab-separated reason") + allowed[key] = reason.strip() + return allowed + + +def report(base, candidate, allowed): + before, before_ignored = base + after, after_ignored = candidate + common = before & after + changes = ( + ("Removed", before - after, True), + ("Added", after - before, False), + ("Newly ignored", (after_ignored - before_ignored) & common, True), + ("Newly un-ignored", (before_ignored - after_ignored) & common, False), + ) + blocked = 0 + for label, tests, is_loss in changes: + print(f"{label}: {len(tests)}") + for test in sorted(tests): + key = "/".join(test) + reason = allowed.get(key) if is_loss else None + print(f" {key}" + (f" (allowed: {reason})" if reason else "")) + blocked += is_loss and reason is None + print(f"Unapproved losses: {blocked}") + return int(blocked != 0) + + +def main(argv): + if argv == ["--self-test"]: + program = unittest.main(argv=[sys.argv[0]], exit=False) + return int(not program.result.wasSuccessful()) + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("base", help="base commit (normally main)") + parser.add_argument("candidate", help="candidate commit") + parser.add_argument("--allow", type=Path, metavar="FILE", help="explained test losses") + args = parser.parse_args(argv) + try: + allowed = read_allow(args.allow) if args.allow else {} + repo = Path(run(["git", "rev-parse", "--show-toplevel"], Path.cwd()).strip()) + commits = [ + run(["git", "rev-parse", "--verify", "--end-of-options", f"{ref}^{{commit}}"], repo).strip() + for ref in (args.base, args.candidate) + ] + print(f"Default Cargo features. Base {commits[0]}, candidate {commits[1]}", flush=True) + with tempfile.TemporaryDirectory(prefix="xenora-test-inventory-") as tmp: + root = Path(tmp).resolve() + base = collect(repo, commits[0], root / "base") + candidate = collect(repo, commits[1], root / "candidate") + print(f"Base tests: {len(base[0])}; candidate tests: {len(candidate[0])}") + return report(base, candidate, allowed) + except (OSError, RuntimeError, ValueError) as error: + print(f"test inventory: {error}", file=sys.stderr) + return 2 + except KeyboardInterrupt: + print("test inventory: interrupted", file=sys.stderr) + return 2 + + +class ParseListTests(unittest.TestCase): + checkout = Path("/checkout") + metadata = {"packages": [ + {"id": package, "name": package, "targets": [{"name": package, "doctest": True}]} + for package in ("one", "two") + ]} + + def listing(self, package, cases): + executable = f"/checkout/target/{package}/shared-testhash" + message = { + "reason": "compiler-artifact", "package_id": package, + "executable": executable, "target": {"name": "shared", "kind": ["test"]}, + } + return f"{json.dumps(message)}\n Running tests/shared.rs ({executable})\n{cases}\n" + + def test_packages_with_same_target_and_test_names(self): + output = self.listing("one", "module::case: test\nspeed: benchmark") + output += self.listing("two", "module::case: test") + output += " Doc-tests two\nsrc/lib.rs - run (line 12): test\n" + suites, tests = parse_list(output, self.metadata, self.checkout) + self.assertEqual(suites, {("one", "test:shared"), ("two", "test:shared"), ("two", "doc:two")}) + self.assertEqual(tests, { + ("one", "test:shared", "module::case"), ("two", "test:shared", "module::case"), + ("two", "doc:two", "src/lib.rs - run (line 12)"), + }) + + def test_empty_ignored_target_and_relative_executable(self): + all_output = self.listing("one", "module::case: test") + all_output = all_output.replace("(/checkout/target/", "(target/") + ignored_output = self.listing("one", "") + self.assertEqual(inventory(all_output, ignored_output, self.metadata, self.checkout), + ({("one", "test:shared", "module::case")}, set())) + + def test_rejects_unresolved_target(self): + with self.assertRaisesRegex(ValueError, "no Cargo artifact"): + parse_list("Running tests/shared.rs (target/shared)", self.metadata, self.checkout) + + def test_rejects_ignored_test_missing_from_full_list(self): + with self.assertRaisesRegex(ValueError, "listings disagree"): + inventory(self.listing("one", ""), self.listing("one", "case: test"), + self.metadata, self.checkout) + + +class ReportTests(unittest.TestCase): + base = ({("p", "test:t", "kept"), ("p", "test:t", "gone"), ("p", "test:t", "quiet")}, set()) + candidate = ({("p", "test:t", "kept"), ("p", "test:t", "quiet"), ("p", "test:t", "new")}, + {("p", "test:t", "quiet")}) + + def test_losses_fail_unless_explained(self): + import contextlib, io + with contextlib.redirect_stdout(io.StringIO()) as out: + self.assertEqual(report(self.base, self.candidate, {}), 1) + self.assertIn("Unapproved losses: 2", out.getvalue()) + allowed = {"p/test:t/gone": "replaced by new", "p/test:t/quiet": "needs hardware"} + with contextlib.redirect_stdout(io.StringIO()) as out: + self.assertEqual(report(self.base, self.candidate, allowed), 0) + self.assertIn("p/test:t/gone (allowed: replaced by new)", out.getvalue()) + + def test_allow_file_needs_a_key_and_a_reason(self): + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "allow.tsv" + path.write_text("# comment\n\np/test:t/gone\treplaced\n", encoding="utf-8") + self.assertEqual(read_allow(path), {"p/test:t/gone": "replaced"}) + path.write_text("p/test:t/gone\n", encoding="utf-8") + with self.assertRaises(ValueError): + read_allow(path) + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:]))