Files
claude-config/bin/test_citations.py
T

22 lines
1013 B
Python

import pathlib
import re
import subprocess
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
CITATION = re.compile(r'`?CLAUDE\.md`? → ("[^"]+"(?:,? (?:and )?"[^"]+")*)')
HEADING = re.compile(r"^#+ (.+?)(?: \(.*\))?$", re.MULTILINE)
LEAD = re.compile(r"^(?:- )?\*\*(.+?)(?:\*\*|[:.]| —)", re.MULTILINE)
NAME = re.compile(r'"([^"]+)"')
class Citations(unittest.TestCase):
def test_every_citation_names_a_heading_then_headings_or_bold_leads_of_claude_md(self):
text = (ROOT / "CLAUDE.md").read_text()
headings = set(HEADING.findall(text))
leads = headings | set(LEAD.findall(text))
tracked = subprocess.run(["git", "ls-files", "-z"], capture_output=True, check=True, cwd=ROOT, text=True).stdout.split("\0")
cited = [(path, NAME.findall(run)) for path in filter(None, tracked) for run in CITATION.findall((ROOT / path).read_text())]
self.assertTrue(cited)
self.assertEqual([f"{path}: {names}" for path, names in cited if names[0] not in headings or set(names[1:]) - leads], [])