|
| 1 | +import ast |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import re |
| 5 | +import typing |
| 6 | + |
| 7 | + |
| 8 | +_REPO_ROOT: typing.Final = pathlib.Path(__file__).resolve().parent.parent |
| 9 | +_ADR_DIR: typing.Final = "docs/adr/" |
| 10 | +_CITATION: typing.Final = re.compile(r"docs/adr/\d{4}-[a-z0-9-]+\.md") |
| 11 | +_UNWALKED_DIR: typing.Final = "node_modules" |
| 12 | + |
| 13 | + |
| 14 | +def _python_files(root: pathlib.Path) -> list[pathlib.Path]: |
| 15 | + found: list[pathlib.Path] = [] |
| 16 | + for dirpath, dirnames, filenames in os.walk(root): |
| 17 | + dirnames[:] = sorted(name for name in dirnames if not name.startswith(".") and name != _UNWALKED_DIR) |
| 18 | + found.extend(pathlib.Path(dirpath, name) for name in sorted(filenames) if name.endswith(".py")) |
| 19 | + return found |
| 20 | + |
| 21 | + |
| 22 | +def _citations(source: str) -> set[str]: |
| 23 | + texts = [source] |
| 24 | + texts.extend( |
| 25 | + node.value |
| 26 | + for node in ast.walk(ast.parse(source)) |
| 27 | + if isinstance(node, ast.Constant) and isinstance(node.value, str) |
| 28 | + ) |
| 29 | + return {cited for text in texts for cited in _CITATION.findall(text)} |
| 30 | + |
| 31 | + |
| 32 | +def unresolved_citations(root: pathlib.Path) -> list[tuple[str, str]]: |
| 33 | + return sorted( |
| 34 | + (file.relative_to(root).as_posix(), cited) |
| 35 | + for file in _python_files(root) |
| 36 | + for cited in _citations(file.read_text(encoding="utf-8")) |
| 37 | + if not (root / cited).is_file() |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_every_adr_path_cited_from_python_resolves() -> None: |
| 42 | + """INVARIANT: a `docs/adr/NNNN-<slug>.md` path named anywhere in this repo's Python exists. |
| 43 | +
|
| 44 | + Broken by renaming, renumbering or pruning an ADR without following its citations. The |
| 45 | + offline link gate reads Markdown only, so a path in a docstring, a comment or a guard message |
| 46 | + is otherwise checked by nothing, and an `INVARIANT:` docstring that names its ADR silently |
| 47 | + loses the rationale the test depends on. A user who trips a guard is handed a link to follow. |
| 48 | + """ |
| 49 | + unresolved = unresolved_citations(_REPO_ROOT) |
| 50 | + |
| 51 | + assert unresolved == [], "\n".join(f"{file} cites {cited}" for file, cited in unresolved) |
| 52 | + |
| 53 | + |
| 54 | +def test_a_citation_of_a_missing_adr_is_reported_with_its_citing_file(tmp_path: pathlib.Path) -> None: |
| 55 | + """The scanner is exercised against a known result, so an empty scan cannot pass as a green one.""" |
| 56 | + (tmp_path / _ADR_DIR).mkdir(parents=True) |
| 57 | + (tmp_path / _ADR_DIR / "0001-kept.md").write_text("# kept\n", encoding="utf-8") |
| 58 | + (tmp_path / "pkg").mkdir() |
| 59 | + (tmp_path / "pkg" / "mod.py").write_text( |
| 60 | + f'"""Explained in {_ADR_DIR}0001-kept.md and {_ADR_DIR}9999-missing.md."""\n', |
| 61 | + encoding="utf-8", |
| 62 | + ) |
| 63 | + |
| 64 | + assert unresolved_citations(tmp_path) == [("pkg/mod.py", f"{_ADR_DIR}9999-missing.md")] |
| 65 | + |
| 66 | + |
| 67 | +def test_a_citation_split_across_adjacent_string_literals_is_found(tmp_path: pathlib.Path) -> None: |
| 68 | + """Python joins adjacent literals at parse time, which is what the `nack` guard message relies on.""" |
| 69 | + (tmp_path / "guard.py").write_text( |
| 70 | + f'MESSAGE = (\n "See https://example.invalid/blob/main/{_ADR_DIR}"\n "0003-split.md."\n)\n', |
| 71 | + encoding="utf-8", |
| 72 | + ) |
| 73 | + |
| 74 | + assert unresolved_citations(tmp_path) == [("guard.py", f"{_ADR_DIR}0003-split.md")] |
| 75 | + |
| 76 | + |
| 77 | +def test_a_citation_inside_a_hash_comment_is_found(tmp_path: pathlib.Path) -> None: |
| 78 | + """Comments never reach the AST, so the raw text is scanned as well.""" |
| 79 | + (tmp_path / "graph.py").write_text(f"# The rule is one-way, see {_ADR_DIR}0009-comment.md\n", encoding="utf-8") |
| 80 | + |
| 81 | + assert unresolved_citations(tmp_path) == [("graph.py", f"{_ADR_DIR}0009-comment.md")] |
| 82 | + |
| 83 | + |
| 84 | +def test_a_tree_with_no_citations_and_no_adr_directory_reports_nothing(tmp_path: pathlib.Path) -> None: |
| 85 | + (tmp_path / "plain.py").write_text("X = 1\n", encoding="utf-8") |
| 86 | + |
| 87 | + assert unresolved_citations(tmp_path) == [] |
| 88 | + |
| 89 | + |
| 90 | +def test_files_under_dot_directories_are_not_scanned(tmp_path: pathlib.Path) -> None: |
| 91 | + """A virtualenv or a cache is not this repo's citations.""" |
| 92 | + (tmp_path / ".venv" / "lib").mkdir(parents=True) |
| 93 | + (tmp_path / ".venv" / "lib" / "vendored.py").write_text(f"# {_ADR_DIR}0001-elsewhere.md\n", encoding="utf-8") |
| 94 | + (tmp_path / _UNWALKED_DIR).mkdir() |
| 95 | + (tmp_path / _UNWALKED_DIR / "dep.py").write_text(f"# {_ADR_DIR}0002-elsewhere.md\n", encoding="utf-8") |
| 96 | + |
| 97 | + assert unresolved_citations(tmp_path) == [] |
0 commit comments