feat(rtk): add an rtk plugin that routes Bash calls through rtk #284
Executable
+97
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for plugins/rtk/.apm/hooks/route-through-rtk.sh, the PreToolUse hook
|
||||
# that routes a Bash tool call through `rtk hook claude` (#179).
|
||||
#
|
||||
# The hook acts only when run from a .claude/hooks/ path, so it is copied to the
|
||||
# path apm deploys it to and driven with real payloads, against a stub rtk, no
|
||||
# rtk at all, and the real rtk when one is installed.
|
||||
set -euo pipefail
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
HOOKS_DIR="$REPO_ROOT/plugins/rtk/.apm/hooks"
|
||||
SRC="$HOOKS_DIR/route-through-rtk.sh"
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
pass() { PASS=$((PASS + 1)); }
|
||||
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
|
||||
|
||||
[[ -f "$SRC" ]] || { echo "FAIL: $SRC does not exist"; exit 1; }
|
||||
REAL_RTK="$(command -v rtk || true)"
|
||||
TIMEOUT="$(command -v timeout)"
|
||||
|
||||
WORK="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
for d in .claude/hooks/rtk/.apm/hooks elsewhere; do
|
||||
mkdir -p "$WORK/$d"
|
||||
cp "$SRC" "$WORK/$d/"
|
||||
done
|
||||
HOOK="$WORK/.claude/hooks/rtk/.apm/hooks/route-through-rtk.sh"
|
||||
|
||||
# A PATH with only what the hook and the stub need: no rtk on it.
|
||||
mkdir -p "$WORK/base-bin"
|
||||
for tool in bash cat dirname; do
|
||||
ln -s "$(command -v "$tool")" "$WORK/base-bin/$tool"
|
||||
done
|
||||
|
||||
# A stub rtk that records its arguments and stdin and prints a marker.
|
||||
mkdir -p "$WORK/stub-bin"
|
||||
cat > "$WORK/stub-bin/rtk" << STUB
|
||||
#!/usr/bin/env bash
|
||||
printf '%s\n' "\$*" > "$WORK/stub-args"
|
||||
cat > "$WORK/stub-stdin"
|
||||
echo stub-output
|
||||
STUB
|
||||
chmod +x "$WORK/stub-bin/rtk"
|
||||
|
||||
payload() {
|
||||
printf '{"tool_name":"Bash","tool_input":{"command":"%s"}}' "$1"
|
||||
}
|
||||
|
||||
# run <hook> <PATH> <command>: sets OUT and RC.
|
||||
run() {
|
||||
RC=0
|
||||
OUT="$(payload "$3" | PATH="$2" "$TIMEOUT" 10 "$1" 2> "$WORK/stderr")" || RC=$?
|
||||
}
|
||||
|
||||
echo "== Stub rtk receives 'hook claude' and the payload"
|
||||
rm -f "$WORK/stub-args" "$WORK/stub-stdin"
|
||||
run "$HOOK" "$WORK/stub-bin:$WORK/base-bin" "git status"
|
||||
if [[ $RC -eq 0 && "$OUT" == stub-output ]]; then pass; else fail "want rc 0 and stub output, got $RC: $OUT"; fi
|
||||
if [[ "$(cat "$WORK/stub-args" 2> /dev/null)" == "hook claude" ]]; then pass; else fail "stub args: $(cat "$WORK/stub-args" 2> /dev/null)"; fi
|
||||
if [[ "$(cat "$WORK/stub-stdin" 2> /dev/null)" == "$(payload "git status")" ]]; then pass; else fail "stub stdin: $(cat "$WORK/stub-stdin" 2> /dev/null)"; fi
|
||||
|
||||
echo "== rtk absent: exit 0, no output"
|
||||
run "$HOOK" "$WORK/base-bin" "git status"
|
||||
if [[ $RC -eq 0 && -z "$OUT" && ! -s "$WORK/stderr" ]]; then pass; else fail "want silent exit 0, got $RC: $OUT $(cat "$WORK/stderr")"; fi
|
||||
|
||||
echo "== Runtime: acts only from .claude/hooks/"
|
||||
rm -f "$WORK/stub-args"
|
||||
run "$WORK/elsewhere/route-through-rtk.sh" "$WORK/stub-bin:$WORK/base-bin" "git status"
|
||||
if [[ $RC -eq 0 && -z "$OUT" && ! -e "$WORK/stub-args" ]]; then pass; else fail "elsewhere: want silent exit 0, got $RC: $OUT"; fi
|
||||
|
||||
echo "== Real rtk"
|
||||
if [[ -z "$REAL_RTK" ]]; then
|
||||
echo " SKIP: rtk is not installed; the real-rtk cases did not run"
|
||||
else
|
||||
REAL_PATH="$(dirname "$REAL_RTK"):$WORK/base-bin"
|
||||
run "$HOOK" "$REAL_PATH" "git status"
|
||||
if [[ $RC -eq 0 && "$OUT" == *'"command":"rtk git status"'* ]]; then pass; else fail "git status: want rewrite, got $RC: $OUT"; fi
|
||||
run "$HOOK" "$REAL_PATH" "echo hello"
|
||||
if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "echo hello: want no output, got $RC: $OUT"; fi
|
||||
fi
|
||||
|
||||
echo "== hooks.json"
|
||||
if python3 - "$HOOKS_DIR/hooks.json" << 'PY'
|
||||
import json, sys
|
||||
(entry,) = json.load(open(sys.argv[1]))["hooks"]["PreToolUse"]
|
||||
(hook,) = entry["hooks"]
|
||||
assert entry["matcher"] == "Bash"
|
||||
assert hook["command"] == "${CLAUDE_PLUGIN_ROOT}/.apm/hooks/route-through-rtk.sh"
|
||||
PY
|
||||
then pass; else fail "hooks.json does not wire the script as expected"; fi
|
||||
|
||||
echo
|
||||
echo "Passed: $PASS Failed: $FAIL"
|
||||
[[ $FAIL -eq 0 && $PASS -gt 0 ]]
|
||||
Reference in new issue
Block a user