Files
makronix/tools/claude-build-gate-test.sh
lotherk 814ec71eaf tools/claude-build-gate.sh: the hook in POSIX sh, and its rules are tested (t0052, t0074)
The hook parsed the tool input with `python3 -c` and matched plain substrings:
any ">" and "releasedir" in one command was refused (a `2>&1` too), and
"git checkout" anywhere blocked the command.  tools/ is POSIX sh, no Python.

- jq when it is there (`.tool_input.command // empty`, fail closed when the
  field cannot be read), sed only as a fallback; dash -n clean.
- releasedir: the operation must target a path under it -- rm/mv/cp/touch/
  chmod/mkdir, `sed -i`, tee, or a redirection whose target is under
  releasedir.  A read with 2>/dev/null stays allowed.
- git reset --hard, git clean -f (any flag order), `git checkout -- .` and
  `git checkout .` in the root; the src/ rule matches src/ as a path
  component, so xsrc/ is not blocked.
- rm of the set lists with any flag order (rm -r -f, -fr, -Rf, long options).
- The allowed entry points named in the refusal are the root Makefile's.

tools/claude-build-gate-test.sh: 25 cases, each one command with its expected
exit code (16 blocked, 8 allowed, one unparsable input that must fail closed).
Measured: `sh tools/claude-build-gate-test.sh` -> 25 ok, 0 bad, exit 0;
dash -n on both scripts clean; .claude/settings.json still calls the same path.

tools/vmctl.py and tools/vmmon.py are deleted: building in a VM is gone
(2026-11-13) and nothing in the tree calls them (git grep finds only the
history files); tools/AGENTS.md already described them as removed.  No Python
in tools/ remains.
2026-11-15 08:57:55 +00:00

73 lines
2.5 KiB
Bash
Executable File

#!/bin/sh
# Test cases for tools/claude-build-gate.sh (t0052/t0074).
# Exit 0 when all cases behave as expected.
set -eu
HOOK=tools/claude-build-gate.sh
ok=0
fail=0
run() {
name=$1 expect=$2 cmd=$3
# build minimal hook JSON
json=$(printf '%s' "$cmd" | awk '
BEGIN { ORS="" }
{
gsub(/\\/, "\\\\")
gsub(/"/, "\\\"")
gsub(/\t/, "\\t")
if (NR > 1) printf "\\n"
printf "%s", $0
}')
set +e
printf '{"tool_name":"Bash","tool_input":{"command":"%s"}}' "$json" | sh "$HOOK" >/dev/null 2>&1
rc=$?
set -e
if [ "$rc" -eq "$expect" ]; then
printf 'ok %s (rc=%s)\n' "$name" "$rc"
ok=$((ok + 1))
else
printf 'BAD %s: expected rc=%s got rc=%s\n' "$name" "$expect" "$rc" >&2
printf ' cmd: %s\n' "$cmd" >&2
fail=$((fail + 1))
fi
}
# Blocked (exit 2)
run "git reset --hard" 2 'git reset --hard HEAD'
run "git clean -fdx" 2 'git clean -fdx'
run "git checkout -- ." 2 'git checkout -- .'
run "git checkout ." 2 'git checkout .'
run "rm -fr sets/lists variant" 2 'rm -fr src/distrib/sets/lists/makronix'
run "rm -r sets/lists variant" 2 'rm -r src/distrib/sets/lists'
run "rm -r -f sets/lists" 2 'rm -r -f src/distrib/sets/lists'
run "cd build&&make" 2 'cd build&&make'
run "make -Cbuild" 2 'make -Cbuild'
run "make -C build" 2 'make -C build'
run "systemd-run build" 2 'systemd-run --unit=x make release'
run "redirect to releasedir" 2 'echo hi > build/obj/releasedir/foo'
run "cp into releasedir" 2 'cp a build/obj/releasedir/images/a'
run "sed -i releasedir file" 2 'sed -i s/x/y/ build/obj/releasedir/file'
run "git reset in src" 2 'git -C src reset --hard'
run "rm sets/lists via different flags" 2 'rm -Rf src/distrib/sets/lists/makronix/mi'
# Allowed (exit 0)
run "grep reset harmless" 0 'grep reset src/Makefile'
run "ls releasedir read with 2>&1" 0 'ls build/obj/releasedir/images 2>/dev/null'
run "cat heredoc-like text" 0 'printf "%s" "reset src checkout clean"'
run "git checkout in xsrc" 0 'git -C xsrc checkout HEAD -- file'
run "git status" 0 'git status --short'
run "plain make" 0 'make release'
run "empty command" 0 ''
run "read releasedir path" 0 'find build/obj/releasedir -maxdepth 1 -type f'
# Fail closed on unparsable non-empty command-bearing input
set +e
printf '{"tool_name":"Bash","tool_input":{"command":' | sh "$HOOK" >/dev/null 2>&1
rc=$?
set -e
if [ "$rc" -eq 2 ]; then printf 'ok fail closed unparsable (rc=%s)\n' "$rc"; ok=$((ok + 1)); else printf 'BAD fail closed unparsable: expected 2 got %s\n' "$rc" >&2; fail=$((fail + 1)); fi
printf 'result: %s ok, %s bad\n' "$ok" "$fail"
[ "$fail" -eq 0 ]