import pathlib
import re
import subprocess
import sys
ROOT = pathlib.Path(__file__).resolve().parent.parent
TESTS_DIR = ROOT / "src" / "tests"
TESTING_MD = ROOT / "TESTING.md"
TABLE_HEADER = "|----------|------:|----------|"
ROW = re.compile(r"^\| (.+?) \| +(\d+) \| `(src/tests/[^`]+)` \|$")
def counts_from_cargo() -> dict[str, int]:
out = subprocess.run(
["cargo", "test", "--locked", "--profile", "ci", "--all-features", "--lib",
"--", "--list"],
capture_output=True,
text=True,
cwd=ROOT,
)
if out.returncode != 0:
sys.stderr.write(out.stderr)
raise SystemExit("cargo could not list the tests")
counts: dict[str, int] = {}
for line in out.stdout.splitlines():
if line.endswith(": test") and line.startswith("tests::"):
module = line.split("::")[1]
counts[module] = counts.get(module, 0) + 1
return counts
def existing_titles() -> dict[str, str]:
titles: dict[str, str] = {}
for line in TESTING_MD.read_text().splitlines():
m = ROW.match(line)
if m:
titles[m.group(3)] = m.group(1)
return titles
def generated_title(module: str) -> str:
words = [w.capitalize() for w in module.removesuffix("_test").split("_")]
return "Tests — " + " ".join("A2A" if w.lower() == "a2a" else w for w in words)
def build_rows() -> tuple[list[str], int, int]:
titles = existing_titles()
counts = counts_from_cargo()
rows, total = [], 0
for module, n in sorted(counts.items()):
rel = f"src/tests/{module}.rs"
rows.append(f"| {titles.get(rel, generated_title(module))} | {n} | `{rel}` |")
total += n
return rows, total, len(counts)
def main() -> int:
rows, total, modules = build_rows()
text = TESTING_MD.read_text()
lines = text.splitlines()
start = lines.index(TABLE_HEADER) + 1
end = start
while end < len(lines) and lines[end].startswith("|"):
end += 1
updated = "\n".join(lines[:start] + rows + lines[end:]) + "\n"
if "--check" in sys.argv:
if updated != text:
print(f"TESTING.md is stale: {modules} modules, {total} tests")
return 1
print("TESTING.md is current")
return 0
TESTING_MD.write_text(updated)
print(f"TESTING.md: {modules} modules, {total} tests")
return 0
if __name__ == "__main__":
raise SystemExit(main())