|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Unit tests for check_adr_numbers.py.""" |
| 3 | + |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 10 | + |
| 11 | +from meta.scripts.check_adr_numbers import adr_files, violations |
| 12 | + |
| 13 | + |
| 14 | +def _paths(*names: str) -> list[Path]: |
| 15 | + return [Path(n) for n in names] |
| 16 | + |
| 17 | + |
| 18 | +class TestAdrFiles(unittest.TestCase): |
| 19 | + """Discovery has to span every docs/adr/ in the tree, and nothing else.""" |
| 20 | + |
| 21 | + def _tree(self, *rel: str) -> Path: |
| 22 | + root = Path(tempfile.mkdtemp()) |
| 23 | + for r in rel: |
| 24 | + p = root / r |
| 25 | + p.parent.mkdir(parents=True, exist_ok=True) |
| 26 | + p.write_text("# x\n") |
| 27 | + return root |
| 28 | + |
| 29 | + def test_finds_root_and_context_directories(self): |
| 30 | + root = self._tree("docs/adr/0001-a.md", "meta/docs/adr/0002-b.md") |
| 31 | + self.assertEqual(adr_files(root), _paths("docs/adr/0001-a.md", "meta/docs/adr/0002-b.md")) |
| 32 | + |
| 33 | + def test_ignores_markdown_outside_an_adr_directory(self): |
| 34 | + # A check that swept every *.md would flag the repo's ordinary docs. |
| 35 | + root = self._tree("docs/adr/0001-a.md", "docs/agents/domain.md", "README.md") |
| 36 | + self.assertEqual(adr_files(root), _paths("docs/adr/0001-a.md")) |
| 37 | + |
| 38 | + def test_ignores_an_adr_directory_not_under_docs(self): |
| 39 | + root = self._tree("adr/0001-a.md", "docs/adr/0002-b.md") |
| 40 | + self.assertEqual(adr_files(root), _paths("docs/adr/0002-b.md")) |
| 41 | + |
| 42 | + def test_skips_build_output_and_vcs_directories(self): |
| 43 | + # find_files' skip list is what keeps a bazel-* symlink from doubling every ADR. |
| 44 | + root = self._tree("docs/adr/0001-a.md", "bazel-out/docs/adr/0001-a.md") |
| 45 | + self.assertEqual(adr_files(root), _paths("docs/adr/0001-a.md")) |
| 46 | + |
| 47 | + |
| 48 | +class TestViolations(unittest.TestCase): |
| 49 | + def test_unique_numbers_pass(self): |
| 50 | + self.assertEqual(violations(_paths("docs/adr/0001-a.md", "meta/docs/adr/0002-b.md")), []) |
| 51 | + |
| 52 | + def test_duplicate_across_directories_fails(self): |
| 53 | + # The case global numbering exists to prevent: per-directory numbering produces this. |
| 54 | + found = violations(_paths("docs/adr/0001-a.md", "meta/docs/adr/0001-b.md")) |
| 55 | + self.assertEqual(len(found), 1) |
| 56 | + path, message = found[0] |
| 57 | + self.assertEqual(path, Path("meta/docs/adr/0001-b.md")) |
| 58 | + self.assertIn("duplicate ADR number 0001", message) |
| 59 | + self.assertIn("docs/adr/0001-a.md", message) |
| 60 | + |
| 61 | + def test_duplicate_within_one_directory_fails(self): |
| 62 | + found = violations(_paths("docs/adr/0001-a.md", "docs/adr/0001-b.md")) |
| 63 | + self.assertEqual(len(found), 1) |
| 64 | + |
| 65 | + def test_a_third_use_is_reported_too(self): |
| 66 | + # Reporting only the first collision would let a fix land and still leave a duplicate. |
| 67 | + found = violations(_paths("docs/adr/0001-a.md", "docs/adr/0001-b.md", "docs/adr/0001-c.md")) |
| 68 | + self.assertEqual(len(found), 2) |
| 69 | + |
| 70 | + def test_readme_is_exempt(self): |
| 71 | + self.assertEqual(violations(_paths("docs/adr/README.md", "docs/adr/0001-a.md")), []) |
| 72 | + |
| 73 | + def test_malformed_names_are_reported(self): |
| 74 | + for name in ( |
| 75 | + "1-a.md", |
| 76 | + "00001-a.md", |
| 77 | + "0001a.md", |
| 78 | + "0001-.md", |
| 79 | + "0001-Mixed-Case.md", |
| 80 | + "notes.md", |
| 81 | + ): |
| 82 | + with self.subTest(name=name): |
| 83 | + found = violations(_paths(f"docs/adr/{name}")) |
| 84 | + self.assertEqual(len(found), 1, f"{name} should be rejected") |
| 85 | + self.assertIn("not a valid ADR filename", found[0][1]) |
| 86 | + |
| 87 | + def test_malformed_name_is_not_also_counted_as_a_number(self): |
| 88 | + # A file that fails the shape has no number to collide with; reporting both would |
| 89 | + # double-count one mistake. |
| 90 | + found = violations(_paths("docs/adr/0001-a.md", "docs/adr/0001a.md")) |
| 91 | + self.assertEqual(len(found), 1) |
| 92 | + self.assertIn("not a valid ADR filename", found[0][1]) |
| 93 | + |
| 94 | + def test_valid_slugs_with_digits_are_accepted(self): |
| 95 | + self.assertEqual(violations(_paths("docs/adr/0007-use-oauth2-for-sso.md")), []) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + unittest.main() |
0 commit comments