import json import pathlib import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from rule_hook.rules import blocks, Breach, command_breaches, Commit, commits, file_breaches, overrides from test_rank_todo import DECISIONS, README, TODO from test_env import isolated_env HOOK = pathlib.Path(__file__).resolve().parents[1] / "rule-hook" # Built from parts, so this file carries no attribution of its own. TRAILER = "Co-Authored-" + "By: Claude " def rules(breaches: list[Breach]) -> list[str]: return [breach.rule for breach in breaches] def hook(call: dict, cwd: str) -> subprocess.CompletedProcess: return subprocess.run([str(HOOK)], capture_output=True, cwd=cwd, env=isolated_env(), input=json.dumps(call), text=True) def git(cwd: str, *args: str) -> str: return subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *args], capture_output=True, check=True, cwd=cwd, env=isolated_env(), text=True).stdout class Commits(unittest.TestCase): def test_cd_and_dash_capital_c_move_the_commit(self): self.assertEqual(commits("cd a && git -C b commit -am 'x'", "/r"), [Commit("/r/a/b", ["x"], False)]) def test_each_message_spelling_is_read(self): self.assertEqual(commits("git commit --message=a -m b=c --message c -mz", "/r")[0].messages, ["a", "b=c", "c", "z"]) def test_a_file_message_is_seen(self): self.assertTrue(commits("git commit -F - < notes\nhello\nEOF\ngit commit -m a", "/r")), 1) self.assertEqual(len(commits("git status &&\n\ngit commit -m a", "/r")), 1) def test_an_arithmetic_shift_is_no_heredoc(self): self.assertEqual(len(commits("echo $((1<<2))\ngit commit -m a\n2", "/r")), 1) def test_a_line_break_separates_commands(self): self.assertEqual(commits("cd a\nGIT_EDITOR=true git commit -m x", "/r"), [Commit("/r/a", ["x"], False)]) class CommandBreaches(unittest.TestCase): def test_one_line_passes(self): self.assertEqual(command_breaches("git add . && git commit -m 'ABC-1 - Fix it'", "/r"), []) def test_a_heredoc_a_second_message_or_a_file_breaks_commit_one_line(self): for command in ["git commit -m \"$(cat <<'EOF'\nTitle\nEOF\n)\"", "git commit -m a -m b", "git commit -F msg.txt"]: with self.subTest(command): self.assertEqual(rules(command_breaches(command, "/r")), ["commit-one-line"]) def test_an_attribution_breaks_only_in_a_git_gh_or_tea_command(self): self.assertEqual(rules(command_breaches(f"tea pr create --description '{TRAILER}'", "/r")), ["no-ai-attribution"]) self.assertEqual(command_breaches(f"grep -rn '{TRAILER}' .", "/r"), []) def test_a_heredoc_body_is_no_shell_text(self): self.assertEqual(rules(command_breaches("git commit -F - <<'EOF'\nTitle\n\nIt's the body\nEOF\necho done", "/r")), ["commit-one-line"]) self.assertEqual(rules(command_breaches("git commit -m \"$(cat <<'EOF'\nIt's one line\nEOF\n)\"", "/r")), ["commit-one-line"]) def test_a_command_that_does_not_parse_breaks_nothing(self): self.assertEqual(command_breaches("git commit -m 'open", "/r"), []) class FileBreaches(unittest.TestCase): def test_an_image_needs_a_patch_version_or_a_digest(self): added = "FROM node:24 AS build\nFROM node:24.18.1-bookworm-slim\nFROM alpine@sha256:ab\nFROM debian:bookworm-20240926-slim\nFROM build\nFROM scratch\nFROM ${BASE}\nFROM python\nCOPY --from=build /a /a\nCOPY --from=0 /b /b\nCOPY --chown=1 --from=alpine:3 /c /c\n" self.assertEqual([breach.detail for breach in file_breaches("Dockerfile", "", added, added)], ["Dockerfile: node:24", "Dockerfile: python", "Dockerfile: alpine:3"]) def test_a_stage_named_outside_the_edit_is_no_image(self): self.assertEqual(file_breaches("app.dockerfile", "FROM node:24.1.0 AS base\n", "FROM base\n", "FROM node:24.1.0 AS base\nFROM base\n"), []) def test_a_workflow_checks_its_images_and_actions(self): added = " container: node:24\n image: mysql:8.4\n - image: 'mysql:8.4.7'\n - uses: actions/checkout@v4\n - uses: a/b@0123456789abcdef0123456789abcdef01234567\n - uses: ./local\n" self.assertEqual(rules(file_breaches("/r/.gitea/workflows/ci.yml", "", added, added)), ["image-pin", "image-pin", "action-pin"]) def test_a_compose_file_checks_its_images(self): self.assertEqual(rules(file_breaches("compose.yaml", "", " image: redis\n", " image: redis\n")), ["image-pin"]) def test_other_yaml_is_not_checked(self): added = "image: /assets/og.png\nimage: { type: string }\nuses: a/b@main\n" self.assertEqual(file_breaches("_config.yml", "", added, added), []) def test_an_ignore_file_sorts_each_block_split_by_blank_lines_and_comments(self): self.assertEqual(blocks("b\na\n\n# c\nd\n# e\nf\n"), [["b", "a"], ["d"], ["f"]]) self.assertEqual(rules(file_breaches(".dockerignore", "", "x", "b\na\n")), ["ignorefile-sorted"]) self.assertEqual(file_breaches(".gitignore", "", "x", "*\n\n!a\n!b\n"), []) def test_a_block_unsorted_before_the_edit_is_left_alone(self): self.assertEqual(file_breaches(".gitignore", "node_modules\n.env\n\ndist\n", "out", "node_modules\n.env\n\ndist\nout\n"), []) def test_an_attribution_in_any_file_breaks(self): self.assertEqual(rules(file_breaches("notes.md", "", TRAILER, TRAILER)), ["no-ai-attribution"]) class Overrides(unittest.TestCase): def test_a_marker_names_a_known_rule_and_a_reason(self): self.assertEqual(overrides("x # rule-override image-pin: vendor ships floating tags only\n# rule-override nope: x\n# rule-override action-pin:"), {"image-pin": "vendor ships floating tags only"}) def test_markdown_takes_an_html_comment(self): self.assertEqual(overrides(""), {"no-ai-attribution": "quotes the banned line"}) class Hook(unittest.TestCase): def setUp(self): self.repo = tempfile.mkdtemp() self.addCleanup(subprocess.run, ["rm", "-rf", self.repo]) git(self.repo, "init", "-q", "-b", "chunk") def test_a_breach_exits_2_naming_the_rule_and_the_override(self): ran = hook({"cwd": self.repo, "tool_input": {"file_path": f"{self.repo}/Dockerfile", "content": "FROM node:24\n"}, "tool_name": "Write"}, self.repo) self.assertEqual(ran.returncode, 2) self.assertIn("Blocked by rule image-pin: ", ran.stderr) self.assertIn("`# rule-override image-pin: `", ran.stderr) def test_an_override_passes_and_is_listed_for_its_branch(self): content = "# rule-override image-pin: the vendor tags no patch versions\nFROM vendor/tool:2\n" ran = hook({"cwd": self.repo, "tool_input": {"file_path": f"{self.repo}/sub/Dockerfile", "content": content}, "tool_name": "Write"}, self.repo) self.assertEqual((ran.returncode, ran.stderr), (0, "")) listed = subprocess.run([str(HOOK), "overrides"], capture_output=True, check=True, cwd=self.repo, env=isolated_env(), text=True).stdout self.assertRegex(listed, r"^\S+Z\tchunk\timage-pin\tthe vendor tags no patch versions\t\S+/sub/Dockerfile\n$") def test_an_edit_is_judged_on_the_file_it_leaves(self): pathlib.Path(self.repo, ".gitignore").write_text("a\nc\n") call = {"cwd": self.repo, "tool_input": {"file_path": f"{self.repo}/.gitignore", "new_string": "d", "old_string": "a"}, "tool_name": "Edit"} self.assertEqual(hook(call, self.repo).returncode, 2) def test_a_commit_while_an_unranked_todo_has_changes_is_blocked(self): pathlib.Path(self.repo, "docs").mkdir() for name, text in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO)]: pathlib.Path(self.repo, name).write_text(text) git(self.repo, "add", "README.md", "docs", "todo.md") git(self.repo, "commit", "-qm", "x", "--no-verify") call = {"cwd": self.repo, "tool_input": {"command": "git add -A && git commit -m x"}, "tool_name": "Bash"} self.assertEqual(hook(call, self.repo).returncode, 0, "todo.md is unchanged") pathlib.Path(self.repo, "todo.md").write_text(TODO + "\n") ran = hook(call, self.repo) self.assertEqual(ran.returncode, 2) self.assertIn("todo.md: not ranked", ran.stderr) def test_a_malformed_override_is_named(self): ran = hook({"cwd": self.repo, "tool_input": {"command": "git commit -m a -m b # rule-override commit-oneline: x"}, "tool_name": "Bash"}, self.repo) self.assertIn("names no rule below, or gives no reason", ran.stderr) def test_a_missing_field_breaks_nothing(self): for given in [{"command": None}, {"content": None, "file_path": f"{self.repo}/a.md"}, {"edits": [None], "file_path": f"{self.repo}/a.md"}]: with self.subTest(given): tool = "Bash" if "command" in given else "Write" if "content" in given else "MultiEdit" self.assertEqual(hook({"cwd": self.repo, "tool_input": given, "tool_name": tool}, self.repo).returncode, 0) self.assertEqual(hook({"cwd": self.repo, "tool_input": None, "tool_name": "Bash"}, self.repo).returncode, 1) def test_unreadable_input_does_not_block(self): ran = subprocess.run([str(HOOK)], capture_output=True, env=isolated_env(), input="{", text=True) self.assertEqual(ran.returncode, 1)