Answer stability round 1: sort imports, share rank_todo's check, scope YAML to compose and workflows, judge only newly unsorted ignore blocks, cut heredoc bodies, check COPY --from and container:, log what each override let through, keep CLAUDE.md lines for what the hook cannot see
This commit is contained in:
@@ -8,8 +8,8 @@ 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_env import isolated_env
|
||||
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.
|
||||
@@ -58,27 +58,43 @@ class CommandBreaches(unittest.TestCase):
|
||||
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 build\nFROM scratch\nFROM ${BASE}\nFROM python\n"
|
||||
self.assertEqual([breach.detail for breach in file_breaches("Dockerfile", "", added, added)], ["Dockerfile: FROM node:24", "Dockerfile: FROM python"])
|
||||
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_yaml_images_and_actions_are_checked(self):
|
||||
added = " 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("ci.yml", "", added, added)), ["image-pin", "action-pin"])
|
||||
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_but_not_the_one_it_builds(self):
|
||||
self.assertEqual(rules(file_breaches("compose.yaml", "", " image: redis\n", " image: redis\n")), ["image-pin"])
|
||||
after = " app:\n build: .\n image: myapp\n"
|
||||
self.assertEqual(file_breaches("docker-compose.yml", "", after, after), [])
|
||||
|
||||
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"])
|
||||
|
||||
@@ -105,7 +121,7 @@ class Hook(unittest.TestCase):
|
||||
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\n$")
|
||||
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")
|
||||
@@ -123,7 +139,12 @@ class Hook(unittest.TestCase):
|
||||
git(self.repo, "add", "todo.md")
|
||||
ran = hook(call, self.repo)
|
||||
self.assertEqual(ran.returncode, 2)
|
||||
self.assertIn("todo.md is not ranked", ran.stderr)
|
||||
self.assertIn("todo.md: not ranked", ran.stderr)
|
||||
|
||||
def test_a_missing_field_breaks_nothing(self):
|
||||
for given in [{"command": None}, {"content": None, "file_path": f"{self.repo}/a.md"}]:
|
||||
with self.subTest(given):
|
||||
self.assertEqual(hook({"cwd": self.repo, "tool_input": given, "tool_name": "Write" if "content" in given else "Bash"}, self.repo).returncode, 0)
|
||||
|
||||
def test_unreadable_input_does_not_block(self):
|
||||
ran = subprocess.run([str(HOOK)], capture_output=True, env=isolated_env(), input="{", text=True)
|
||||
|
||||
Reference in New Issue
Block a user