Answer stability round 2: keep the line break that ends a heredoc, read a run of separators as one, check every untagged compose image, and reject a non-object tool input

This commit is contained in:
2026-10-12 23:20:57 +02:00
parent 2f2a770392
commit 2d3f3adebc
6 changed files with 63 additions and 46 deletions
+27 -13
View File
@@ -30,7 +30,7 @@ def git(cwd: str, *args: str) -> str:
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, True)])
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"])
@@ -41,8 +41,15 @@ class Commits(unittest.TestCase):
def test_other_git_commands_are_no_commit(self):
self.assertEqual(commits("git log --grep commit; echo git commit", "/r"), [])
def test_a_commit_after_a_heredoc_or_a_blank_line_is_seen(self):
self.assertEqual(len(commits("cat <<EOF > 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, False)])
self.assertEqual(commits("cd a\nGIT_EDITOR=true git commit -m x", "/r"), [Commit("/r/a", ["x"], False)])
class CommandBreaches(unittest.TestCase):
@@ -78,10 +85,8 @@ class FileBreaches(unittest.TestCase):
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):
def test_a_compose_file_checks_its_images(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"
@@ -103,6 +108,9 @@ 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("<!-- rule-override no-ai-attribution: quotes the banned line -->"), {"no-ai-attribution": "quotes the banned line"})
class Hook(unittest.TestCase):
def setUp(self):
@@ -128,23 +136,29 @@ class Hook(unittest.TestCase):
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_staging_an_unranked_todo_is_blocked(self):
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")
call = {"cwd": self.repo, "tool_input": {"command": "git commit -m x"}, "tool_name": "Bash"}
self.assertEqual(hook(call, self.repo).returncode, 0)
self.assertEqual(hook({**call, "tool_input": {"command": "git commit -am x"}}, self.repo).returncode, 0, "-a stages tracked files only")
git(self.repo, "add", "todo.md")
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"}]:
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):
self.assertEqual(hook({"cwd": self.repo, "tool_input": given, "tool_name": "Write" if "content" in given else "Bash"}, self.repo).returncode, 0)
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)