Files
claude-config/bin/repo_checks/test_tools_named_bare.py
T

38 lines
2.2 KiB
Python

import re
import unittest
from git_cli.git_repo import git
from repo_checks import ROOT
# A tool spelled by its place in an install folder, as opposed to a source file under bin/, which a reader opens by its path. A session the loop starts has the release's tools on PATH, so prose names a tool bare, and works wherever the release is installed.
BY_ITS_FOLDER = re.compile(r"(?:~|\$HOME)/\.claude/bin/[\w.<>-]+(?![\w.<>/-])")
# The plan, the changelog and the records: their text names a tool as it was spelled when the line was written, and nobody rewrites it.
HISTORY = ("CHANGELOG.md", "docs/", "todo.md")
# The lines a person runs in their own shell or a crontab, which lack the tools: only `build-release` is linked onto their `PATH`.
BY_HAND = {
"README.md": {"$HOME/.claude/bin/compress-archives", "~/.claude/bin/<tool>", "~/.claude/bin/build-release", "~/.claude/bin/notify", "~/.claude/bin/project-config"},
"presentation/README.md": {"~/.claude/bin/run-heavy"},
"presentation/build-pdf.py": {"~/.claude/bin/run-heavy"},
}
# settings.json names its hooks and its status line by their folder, and the test of the reviewer hook quotes it. Claude Code reads the file in every session, also one the human starts without the tools on PATH. A hook it cannot find doesn't refuse anything, so a bare name there would switch rule-hook off in that session.
SETTINGS = ("settings.json", "bin/review_rules/test_reviewer_context.py")
class ToolsNamedBare(unittest.TestCase):
def test_no_tracked_file_names_a_tool_by_its_install_folder(self) -> None:
tracked = git("ls-files", cwd=str(ROOT))
self.assertIsInstance(tracked, str, tracked)
found = []
for name in str(tracked).splitlines():
path = ROOT / name
if name.startswith(HISTORY) or name in SETTINGS or name == "bin/repo_checks/test_tools_named_bare.py" or not path.is_file():
continue
for number, line in enumerate(path.read_text(encoding="utf-8", errors="replace").splitlines(), 1):
found += [f"{name}:{number}: {spelled}" for spelled in BY_ITS_FOLDER.findall(line) if spelled not in BY_HAND.get(name, ())]
self.assertEqual(found, [], "name each tool bare, as in `todo-item`")
if __name__ == "__main__":
unittest.main()