#!/usr/bin/env python3 """Offline check of the rule documentation: every path:line and test name it cites must exist. Scans the markdown under docs/ (except docs/archive/), tasks.md and odd/tasks/. For each `path:line` or `path:start-end` reference it checks that the file exists and the line is in range. For each `test_*` name it checks that a test with that name is defined under tests/. An optional baseline at scripts/check-rule-docs.baseline may list accepted problems (file and message, no line numbers), so new drift fails while existing debt stays visible. That file is absent today, so every problem fails; `--update-baseline` writes it, and only a deliberate review may add entries. Historical logs (`*-log.md`) are not checked. No network access. The constants-to-sources check lives in tests/rules/test_rule_constants_sources.py. """ from __future__ import annotations import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] DOC_GLOBS = ["docs/**/*.md", "tasks.md", "odd/tasks/*.md"] BASELINE = ROOT / "scripts" / "check-rule-docs.baseline" EXCLUDE_PARTS = {"archive"} PATH_REF = re.compile( r"((?:core|modules|adapters|tests|scripts|static|app|docs)/[\w./-]+\.(?:json|py|ts|mjs|js|sh|md|html|css))" r"(?::(\d+)(?:[-–](\d+))?)?" ) TEST_REF = re.compile(r"\btest_[a-z0-9_]+\b") def doc_files() -> list[Path]: files: set[Path] = set() for pattern in DOC_GLOBS: for path in ROOT.glob(pattern): if path.is_file() and not (EXCLUDE_PARTS & set(path.parts)) and not path.name.endswith("-log.md"): files.add(path) return sorted(files) def defined_tests() -> set[str]: names: set[str] = {path.stem for path in (ROOT / "tests").rglob("test_*") if path.is_file()} for path in (ROOT / "tests").rglob("*"): if path.suffix in {".py", ".ts"} and path.is_file(): text = path.read_text(encoding="utf-8", errors="ignore") names.update(re.findall(r"def (test_\w+)", text)) names.update(re.findall(r"""test\(\s*["'`]([^"'`]+)""", text)) return names def check() -> list[str]: problems: list[str] = [] tests = defined_tests() for doc in doc_files(): rel_doc = doc.relative_to(ROOT) for lineno, line in enumerate(doc.read_text(encoding="utf-8").splitlines(), start=1): for match in PATH_REF.finditer(line): target = ROOT / match.group(1) if not target.is_file(): problems.append(f"{rel_doc}:{lineno}: missing file {match.group(1)}") continue if match.group(2): last = int(match.group(3) or match.group(2)) count = len(target.read_text(encoding="utf-8", errors="ignore").splitlines()) if last > count: problems.append(f"{rel_doc}:{lineno}: {match.group(1)} has {count} lines, cited {last}") for name in TEST_REF.findall(line): if name not in tests: problems.append(f"{rel_doc}:{lineno}: test not found: {name}") return problems def main() -> int: found = check() keyed = {re.sub(r"^([^:]+):\d+:", r"\1:", problem) for problem in found} if "--update-baseline" in sys.argv: BASELINE.write_text("\n".join(sorted(keyed)) + "\n", encoding="utf-8") print(f"check-rule-docs: baseline written with {len(keyed)} entries") return 0 known = set(BASELINE.read_text(encoding="utf-8").split("\n")) - {""} if BASELINE.exists() else set() new = [problem for problem in found if re.sub(r"^([^:]+):\d+:", r"\1:", problem) not in known] for problem in new: print("NEW " + problem) print(f"check-rule-docs: {len(new)} new problem(s); {len(found) - len(new)} known in baseline") return 1 if new else 0 if __name__ == "__main__": sys.exit(main())