fix(tests): hook-running suites stub the claude CLI fail-closed
Inline stubs ran claude probe before checking the stub won PATH, and three suites ran the hook with no stub. A shared helper checks resolution, controls from another cwd and exits on any miss (BLK-043 family).
This commit is contained in:
1 parent
d93f06cb9b
commit
dc5f127ea5
8 files changed
+124
-34
No files matched your search
@@ -12,7 +12,7 @@ check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
|
||||
check_has() { case "$2" in *"$3"*) pass=$((pass+1));; *) fail=$((fail+1));
|
||||
printf 'FAIL %s: [%s] does not contain [%s]\n' "$1" "$2" "$3";; esac; }
|
||||
FX="$(mktemp -d)"; trap 'rm -rf "$FX"' EXIT
|
||||
FX="$(mktemp -d)" || exit 1; trap 'rm -rf "$FX"' EXIT
|
||||
mkdir -p "$FX/bin" "$FX/home/.claude/lib/profiles"
|
||||
cp "$ROOT/lib/cc.sh" "$ROOT/lib/profile.sh" "$FX/home/.claude/lib/"
|
||||
printf '{"enabledPlugins": {"x@skills-dir": true}}\n' > "$FX/home/.claude/lib/profiles/webx.json"
|
||||
@@ -34,6 +34,11 @@ cat > "$FX/bin/claude" <<'EOT'
|
||||
echo "TMPDIR=${TMPDIR-unset}"; echo "canary=${CC_ENV_CANARY-unset}"; } >> "$CC_LOG"
|
||||
EOT
|
||||
chmod +x "$FX/bin/"*
|
||||
# shellcheck source=lib/tests/claude-stub.sh
|
||||
. "$ROOT/lib/tests/claude-stub.sh"
|
||||
for c in claude systemd-run dtach; do
|
||||
PATH="$FX/bin:$PATH" stub_first_or_die "$c" "$FX/bin/$c"
|
||||
done
|
||||
export CC_LOG="$FX/log" HOME="$FX/home"
|
||||
PATH="$FX/bin:$PATH"
|
||||
# shellcheck disable=SC1091
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/tests/claude-stub.sh — sourced by the hook-running suites (BLK-043).
|
||||
# A hook run that reaches the real claude CLI starts a session that re-runs
|
||||
# the hook: fork bomb. claude_stub_install puts a logging stub first on PATH
|
||||
# and exits the calling suite unless the stub is really the one resolved.
|
||||
# bash 3.2 safe.
|
||||
|
||||
# stub_first_or_die <cmd> <expected-path>: exit 1 unless <cmd> resolves there
|
||||
stub_first_or_die() {
|
||||
if [ "$(command -v "$1")" != "$2" ]; then
|
||||
echo "$1 stub not first on PATH" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# claude_stub_install <work-dir>: stub in <work-dir>/stub, log in
|
||||
# <work-dir>/claude.log, PATH and GIT_CEILING_DIRECTORIES exported
|
||||
claude_stub_install() {
|
||||
local dir
|
||||
if [ -z "${1:-}" ] || [ ! -d "$1" ]; then
|
||||
echo "claude_stub_install: work dir missing" >&2
|
||||
exit 1
|
||||
fi
|
||||
dir=$(cd -P "$1" && pwd) || exit 1
|
||||
CLAUDE_LOG="$dir/claude.log"
|
||||
: >"$CLAUDE_LOG" || exit 1
|
||||
mkdir -p "$dir/stub" || exit 1
|
||||
printf '#!/bin/sh\necho "$*" >>"%s"\n' "$CLAUDE_LOG" \
|
||||
>"$dir/stub/claude" || exit 1
|
||||
chmod +x "$dir/stub/claude" || exit 1
|
||||
export PATH="$dir/stub:$PATH"
|
||||
stub_first_or_die claude "$dir/stub/claude"
|
||||
(cd / && claude probe)
|
||||
if [ "$(claude_stub_calls)" != 1 ]; then
|
||||
echo "claude stub positive control failed" >&2
|
||||
exit 1
|
||||
fi
|
||||
: >"$CLAUDE_LOG" || exit 1
|
||||
export GIT_CEILING_DIRECTORIES="$dir"
|
||||
}
|
||||
|
||||
# claude_stub_calls: number of claude calls logged since the install
|
||||
claude_stub_calls() { wc -l <"$CLAUDE_LOG" | tr -d ' '; }
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/tests/claude-stub.test.sh
|
||||
# Regression for BLK-043 residuals: claude_stub_install must fail closed
|
||||
# (never leave the real claude CLI first on PATH) and survive a relative dir.
|
||||
# shellcheck disable=SC1090,SC2329 # HELPER override; chmod shadow is called by the helper
|
||||
set -u
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
HELPER="${CLAUDE_STUB_LIB:-$ROOT/lib/tests/claude-stub.sh}"
|
||||
WORK=$(mktemp -d) || exit 1
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
pass=0; fail=0
|
||||
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
|
||||
|
||||
# (a) fail-closed: chmod is a no-op so the stub stays 0644, command -v
|
||||
# skips it and finds the fake claude (writes a sentinel) further on PATH.
|
||||
mkdir -p "$WORK/a/work" "$WORK/a/fake"
|
||||
printf '#!/bin/sh\ntouch "%s"\n' "$WORK/a/sentinel" >"$WORK/a/fake/claude"
|
||||
chmod +x "$WORK/a/fake/claude"
|
||||
err=$( ( chmod() { :; }; PATH="$WORK/a/fake:$PATH"
|
||||
. "$HELPER"; claude_stub_install "$WORK/a/work" ) 2>&1 >/dev/null )
|
||||
rc=$?
|
||||
check Ta-rc "$rc" 1
|
||||
case "$err" in *"claude stub not first on PATH"*) m=yes ;; *) m=no ;; esac
|
||||
check Ta-message "$m" yes
|
||||
check Ta-sentinel-absent "$([ -e "$WORK/a/sentinel" ] && echo yes || echo no)" no
|
||||
|
||||
# (b) relative dir: the stub still resolves after a cd elsewhere.
|
||||
mkdir -p "$WORK/b/rel"
|
||||
got=$( cd "$WORK/b" && . "$HELPER" && claude_stub_install rel \
|
||||
&& cd / && command -v claude )
|
||||
check Tb-relative-resolves "$got" "$WORK/b/rel/stub/claude"
|
||||
|
||||
# (c) happy path
|
||||
mkdir -p "$WORK/c"
|
||||
out=$( . "$HELPER"; claude_stub_install "$WORK/c" && claude_stub_calls )
|
||||
check Tc-happy "$out" 0
|
||||
|
||||
printf 'claude-stub: %d passed, %d failed\n' "$pass" "$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
@@ -6,39 +6,31 @@ set -u
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
LIB="${DETECT_PLUGINS_LIB:-$ROOT/lib/detect-plugins.sh}"
|
||||
HOOK="$ROOT/hooks/session-start.sh"
|
||||
WORK=$(mktemp -d)
|
||||
WORK=$(mktemp -d) || exit 1
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
pass=0; fail=0
|
||||
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
|
||||
calls() { wc -l <"$CLAUDE_LOG" | tr -d ' '; }
|
||||
|
||||
# Stub claude: logs every call, never starts a session.
|
||||
CLAUDE_LOG="$WORK/claude.log"; : >"$CLAUDE_LOG"
|
||||
mkdir -p "$WORK/stub"
|
||||
printf '#!/bin/sh\necho "$*" >>"%s"\n' "$CLAUDE_LOG" >"$WORK/stub/claude"
|
||||
chmod +x "$WORK/stub/claude"
|
||||
export PATH="$WORK/stub:$PATH"
|
||||
|
||||
# Positive control: the stub is reached through PATH.
|
||||
claude probe
|
||||
check T0-stub-reached "$(calls)" 1
|
||||
: >"$CLAUDE_LOG"
|
||||
# Stub claude: logs every call, never starts a session; fails closed.
|
||||
# shellcheck source=lib/tests/claude-stub.sh
|
||||
. "$ROOT/lib/tests/claude-stub.sh"
|
||||
claude_stub_install "$WORK"
|
||||
|
||||
# detect_plan, no .claude.json: pro, no claude call.
|
||||
mkdir -p "$WORK/empty"
|
||||
out=$(HOME="$WORK/empty" bash -c "source '$LIB'; detect_plan")
|
||||
out=$(HOME="$WORK/empty" bash -c 'source "$1"; detect_plan' _ "$LIB")
|
||||
check T1-no-json-plan "$out" pro
|
||||
check T1-no-json-calls "$(calls)" 0
|
||||
check T1-no-json-calls "$(claude_stub_calls)" 0
|
||||
|
||||
# detect_plan, max markers: max, no claude call.
|
||||
mkdir -p "$WORK/maxhome"
|
||||
printf '{"tengu_cobalt_compass": true, "tengu_harbor": true}\n' \
|
||||
>"$WORK/maxhome/.claude.json"
|
||||
: >"$CLAUDE_LOG"
|
||||
out=$(HOME="$WORK/maxhome" bash -c "source '$LIB'; detect_plan")
|
||||
out=$(HOME="$WORK/maxhome" bash -c 'source "$1"; detect_plan' _ "$LIB")
|
||||
check T2-max-plan "$out" max
|
||||
check T2-max-calls "$(calls)" 0
|
||||
check T2-max-calls "$(claude_stub_calls)" 0
|
||||
|
||||
# Hook HOME: real ~/.claude entries + max fixture (no fallback needed).
|
||||
mkdir -p "$WORK/hookhome/.claude" "$WORK/proj"
|
||||
@@ -68,7 +60,7 @@ chmod +x "$WORK/stub/git"
|
||||
( cd "$WORK/proj" && env -u SESSION_START_RUNNING HOME="$WORK/hookhome" \
|
||||
SESSION_START_OFFLINE=1 bash "$HOOK" </dev/null >/dev/null 2>&1 )
|
||||
check T4-child-sees-guard "$(head -1 "$GIT_LOG")" 1
|
||||
check T4-hook-no-claude-calls "$(calls)" 0
|
||||
check T4-hook-no-claude-calls "$(claude_stub_calls)" 0
|
||||
|
||||
printf 'detect-plan: %d passed, %d failed\n' "$pass" "$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
@@ -2,8 +2,11 @@
|
||||
# lib/tests/graphify-gate.test.sh — "propose graphify" threshold signal (BDR-097).
|
||||
set -u
|
||||
LIB="$(cd "$(dirname "$0")/../.." && pwd)/lib/graphify-gate.sh"
|
||||
WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
|
||||
WORK="$(mktemp -d)" || exit 1; trap 'rm -rf "$WORK"' EXIT
|
||||
pass=0; fail=0
|
||||
. "$(dirname "$0")/claude-stub.sh"
|
||||
mkdir -p "$WORK/cs" || exit 1
|
||||
claude_stub_install "$WORK/cs"
|
||||
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
|
||||
# gate(dir) -> the signal line, or "silent"
|
||||
@@ -72,5 +75,7 @@ check S3-from-subdir "$(banner "$d/app/sub" | grep -c 'graphify query')" 1
|
||||
out=$(banner "$WORK/plain")
|
||||
check S4-not-a-repo "$(grep -c 'graphify query' <<<"$out")/$(grep -c 'Claude Code config' <<<"$out")" 0/1
|
||||
|
||||
check no-claude-calls "$(claude_stub_calls)" 0
|
||||
|
||||
echo "PASS=$pass FAIL=$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
@@ -9,7 +9,7 @@ MEM="$ROOT/lib/memory.sh"
|
||||
pass=0; fail=0
|
||||
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
|
||||
FIX=$(mktemp -d); trap 'rm -rf "$FIX"' EXIT
|
||||
FIX=$(mktemp -d) || exit 1; trap 'rm -rf "$FIX"' EXIT
|
||||
cd "$FIX" || exit 1
|
||||
git init -q . && git config user.email t@t && git config user.name t
|
||||
export MEMORY_DATE=2026-10-27
|
||||
@@ -686,6 +686,12 @@ for t in decisions learnings blockers evals; do
|
||||
check "T13-template-$t-v2" "$(sed -n 2p "$ROOT/templates/memory/$t.md")" 'format: 2'
|
||||
check "T13b-template-$t-no-index" "$(grep -c '^## Index$' "$ROOT/templates/memory/$t.md")" 0
|
||||
done
|
||||
# Stub work dir outside $FIX: the fixture repo's `git add -A` must not
|
||||
# commit the stub. Installed here so earlier cases see the repo unceilinged.
|
||||
# shellcheck source=lib/tests/claude-stub.sh
|
||||
. "$ROOT/lib/tests/claude-stub.sh"
|
||||
CSW=$(mktemp -d) || exit 1; trap 'rm -rf "$FIX" "$CSW"' EXIT
|
||||
claude_stub_install "$CSW"
|
||||
banner() { (cd "$1" && SESSION_START_OFFLINE=1 bash "$ROOT/hooks/session-start.sh" \
|
||||
</dev/null 2>/dev/null) | grep -c "${2:-registry v1}"; }
|
||||
check T13c-banner-v1-v2 "$(banner "$FIX/v1h" 'registry v1: 1 file(s) (memory.sh check)')/$(banner "$FIX")" 1/0
|
||||
@@ -720,5 +726,6 @@ mkdir 'a=b' && printf '# Decisions\n\n## BDR-001 - legacy\n' > 'a=b/decisions.md
|
||||
err=$(bash "$MEM" check 'a=b' 2>&1 >/dev/null </dev/null)
|
||||
check T19c-assign-dir-read "$(printf '%s\n' "$err" | grep -c '^memory: v1: ./a=b/decisions.md$')" 1
|
||||
|
||||
check no-claude-calls "$(claude_stub_calls)" 0
|
||||
echo "PASS=$pass FAIL=$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
@@ -7,7 +7,7 @@ set -u
|
||||
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
H="$ROOT/hooks/push-guard.sh"
|
||||
WORK=$(mktemp -d)
|
||||
WORK=$(mktemp -d) || exit 1
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
pass=0; fail=0
|
||||
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
@@ -352,6 +352,10 @@ check T43a-soft-rule "$(grep -c 'manual-push mode' <<<"$soft" | tr -d ' ')" 1
|
||||
check T43b-clearance "$(grep -c "does not clear it: the user types \`! git push\`" <<<"$soft")" 1
|
||||
|
||||
# ── session banner ──
|
||||
# shellcheck source=lib/tests/claude-stub.sh
|
||||
. "$ROOT/lib/tests/claude-stub.sh"
|
||||
mkdir -p "$WORK/cs" || exit 1
|
||||
claude_stub_install "$WORK/cs"
|
||||
banner() { # banner <dir>
|
||||
(cd "$1" && SESSION_START_OFFLINE=1 bash "$ROOT/hooks/session-start.sh" \
|
||||
</dev/null 2>/dev/null)
|
||||
@@ -422,4 +426,5 @@ check T63b-no-root-glob "$(jq -r '(.permissions.ask + .permissions.deny)[]' \
|
||||
check T63c-parens-balanced "$(jq -r '(.permissions.ask + .permissions.deny)[]
|
||||
| select(test("^[A-Za-z]+\\(") and (endswith(")") | not))' "$S")" ""
|
||||
|
||||
check no-claude-calls "$(claude_stub_calls)" 0
|
||||
printf 'PASS=%s FAIL=%s\n' "$pass" "$fail"; [ "$fail" -eq 0 ]
|
||||
@@ -4,7 +4,7 @@
|
||||
set -u
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
SS="$ROOT/hooks/session-start.sh"
|
||||
WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
|
||||
WORK="$(mktemp -d)" || exit 1; trap 'rm -rf "$WORK"' EXIT
|
||||
export SESSION_START_OFFLINE=1
|
||||
pass=0; fail=0
|
||||
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
@@ -12,16 +12,9 @@ check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
|
||||
# Stub claude (BLK-043): a hook run that calls the CLI logs a line here
|
||||
# instead of starting a session that re-runs this hook.
|
||||
CLAUDE_LOG="$WORK/claude.log"; : >"$CLAUDE_LOG"
|
||||
mkdir -p "$WORK/stub"
|
||||
printf '#!/bin/sh\necho "$*" >>"%s"\n' "$CLAUDE_LOG" >"$WORK/stub/claude"
|
||||
chmod +x "$WORK/stub/claude"
|
||||
export PATH="$WORK/stub:$PATH"
|
||||
calls() { wc -l <"$CLAUDE_LOG" | tr -d ' '; }
|
||||
# SD0: positive control, the stub is reached through PATH
|
||||
claude probe
|
||||
check SD0-stub-reached "$(calls)" 1
|
||||
: >"$CLAUDE_LOG"
|
||||
# shellcheck source=lib/tests/claude-stub.sh
|
||||
. "$ROOT/lib/tests/claude-stub.sh"
|
||||
claude_stub_install "$WORK"
|
||||
|
||||
# mkproj <name> -> path with an empty memory dir
|
||||
mkproj() {
|
||||
@@ -251,7 +244,7 @@ check SD14d-banner "$(run "$p" "$WORK/startup.json" | grep -c '^$')" \
|
||||
"$(run "$p" "$WORK/startup.json" | grep -c '^$')"
|
||||
|
||||
# SD15: no hook run of this suite called the claude CLI (BLK-043)
|
||||
check SD15-no-claude-calls "$(calls)" 0
|
||||
check SD15-no-claude-calls "$(claude_stub_calls)" 0
|
||||
|
||||
printf 'session-digest: %d passed, %d failed\n' "$pass" "$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
Reference in new issue
Block a user