diff options
Diffstat (limited to 'tests/hooks')
| -rwxr-xr-x | tests/hooks/test-run-hook-cmd-windows.sh | 223 | ||||
| -rwxr-xr-x | tests/hooks/test-session-start.sh | 100 |
2 files changed, 323 insertions, 0 deletions
diff --git a/tests/hooks/test-run-hook-cmd-windows.sh b/tests/hooks/test-run-hook-cmd-windows.sh new file mode 100755 index 0000000..b58c6d1 --- /dev/null +++ b/tests/hooks/test-run-hook-cmd-windows.sh @@ -0,0 +1,223 @@ +#!/usr/bin/env bash +# Tests for the Windows (cmd) half of hooks/run-hook.cmd: how it finds bash +# and whether the hook's exit code survives. Run from Git Bash on Windows; +# skips anywhere cmd.exe isn't available. +# +# Most cases need the fallback branches, which a machine with Git in +# Program Files never reaches. Those cases run a sandbox copy of the wrapper +# whose Program Files paths point at a directory that doesn't exist. +set -euo pipefail + +if ! command -v cmd >/dev/null 2>&1 || ! command -v cygpath >/dev/null 2>&1; then + echo "SKIP: cmd.exe not available (these tests run in Git Bash on Windows)" + exit 0 +fi + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" +PF_GIT='C:\Program Files\Git' +# Spelled out: inside Git Bash, cygpath maps its own install dir to "/". +PF_GIT_UNIX='/c/Program Files/Git' + +if [[ ! -e "$PF_GIT_UNIX/bin/bash.exe" ]]; then + echo "SKIP: needs Git for Windows in $PF_GIT to stand in for other installs" + exit 0 +fi + +FAILURES=0 +TEST_ROOT="$(mktemp -d)" +JUNCTIONS=() + +cleanup() { + # Remove junctions with cmd's rmdir first, which never touches the + # target, so nothing below can recurse into the real Git install. + local j left=0 + for j in "${JUNCTIONS[@]}"; do + cmd //c rmdir "$j" >/dev/null 2>&1 || true + if [[ -e "$(cygpath "$j")" ]]; then + echo "WARNING: junction $j still exists; not deleting $TEST_ROOT" >&2 + left=1 + fi + done + [[ "$left" -eq 0 ]] && rm -rf "$TEST_ROOT" +} +trap cleanup EXIT + +pass() { echo " [PASS] $1"; } +fail() { echo " [FAIL] $1"; FAILURES=$((FAILURES + 1)); } + +junction() { + local link="$1" target="$2" + # Keep Git Bash from rewriting /J into a path. + MSYS2_ARG_CONV_EXCL="*" cmd /c mklink /J "$link" "$target" >/dev/null + JUNCTIONS+=("$link") +} + +# A plugin root with probe hooks. $1 = "real" keeps the wrapper as is; +# "sandbox" points its Program Files paths at nothing. +make_plugin() { + local dir="$1" mode="$2" + mkdir -p "$dir/hooks" + if [[ "$mode" == sandbox ]]; then + sed -e 's#C:\\Program Files\\Git\\#C:\\no-such-program-files\\Git\\#g' \ + -e 's#C:\\Program Files (x86)\\Git\\#C:\\no-such-program-files\\Git\\#g' \ + "$REPO_ROOT/hooks/run-hook.cmd" > "$dir/hooks/run-hook.cmd" + if grep -q 'Program Files' "$dir/hooks/run-hook.cmd"; then + echo "sandbox wrapper still mentions Program Files" >&2 + exit 1 + fi + else + cp "$REPO_ROOT/hooks/run-hook.cmd" "$dir/hooks/run-hook.cmd" + fi + printf 'echo probe-ran\n' > "$dir/hooks/probe-ok" + printf 'echo probe-ran\nexit 7\n' > "$dir/hooks/probe-exit" +} + +# run_hook PLUGIN_DIR CWD PROBE [VAR=value...] -> sets OUT and RC +run_hook() { + local plugin="$1" cwd="$2" probe="$3" + shift 3 + RC=0 + # tr drops the NUL bytes the WSL launcher prints (UTF-16 text). + OUT="$(cd "$cwd" && env "$@" cmd //c "$(cygpath -w "$plugin")\\hooks\\run-hook.cmd" "$probe" 2>&1 | tr -d '\000'; exit "${PIPESTATUS[0]}")" || RC=$? +} + +SYS32_PATH="/c/Windows/System32" +GIT_USR_BIN="$PF_GIT_UNIX/usr/bin" +NO_LA='C:\no-such-localappdata' + +echo "run-hook.cmd (Windows) tests" + +# --- exit code of the hook survives the wrapper --- +real="$TEST_ROOT/real" +make_plugin "$real" real +run_hook "$real" "$TEST_ROOT" probe-exit +if [[ "$RC" -eq 7 && "$OUT" == *probe-ran* ]]; then + pass "hook exit code propagates through run-hook.cmd" +else + fail "hook exit code propagates through run-hook.cmd (rc=$RC, out=$OUT)" +fi + +sandbox="$TEST_ROOT/sandbox" +make_plugin "$sandbox" sandbox + +# --- per-user Git install (%LOCALAPPDATA%\Programs\Git) --- +la="$TEST_ROOT/localappdata" +mkdir -p "$la/Programs" +junction "$(cygpath -w "$la/Programs/Git")" "$PF_GIT" +run_hook "$sandbox" "$TEST_ROOT" probe-exit \ + LOCALAPPDATA="$(cygpath -w "$la")" PATH="$SYS32_PATH" +if [[ "$RC" -eq 7 && "$OUT" == *probe-ran* ]]; then + pass "per-user Git install is found and its exit code propagates" +else + fail "per-user Git install is found and its exit code propagates (rc=$RC, out=$OUT)" +fi + +# --- the System32 WSL launcher on PATH is skipped for a real bash --- +run_hook "$sandbox" "$TEST_ROOT" probe-ok \ + LOCALAPPDATA="$NO_LA" PATH="$SYS32_PATH:$GIT_USR_BIN" +if [[ "$RC" -eq 0 && "$OUT" == *probe-ran* ]]; then + pass "System32 bash.exe (WSL launcher) is skipped in favor of Git's bash on PATH" +else + fail "System32 bash.exe (WSL launcher) is skipped in favor of Git's bash on PATH (rc=$RC, out=$OUT)" +fi + +# --- a bash under a WindowsApps directory (the Store WSL stub) is skipped --- +apps_la="$TEST_ROOT/apps-localappdata" +apps="$apps_la/Microsoft/WindowsApps" +mkdir -p "$apps" +apps_marker="$TEST_ROOT/windowsapps-ran" +printf '@echo off\r\necho stub> "%s"\r\n' "$(cygpath -w "$apps_marker")" > "$apps/bash.cmd" +run_hook "$sandbox" "$TEST_ROOT" probe-ok \ + LOCALAPPDATA="$(cygpath -w "$apps_la")" PATH="$apps:$SYS32_PATH:$GIT_USR_BIN" +if [[ ! -e "$apps_marker" && "$OUT" == *probe-ran* ]]; then + pass "bash under WindowsApps (Store WSL stub) is skipped" +else + fail "bash under WindowsApps (Store WSL stub) is skipped (stub ran: $([[ -e $apps_marker ]] && echo yes || echo no), out=$OUT)" +fi + +# --- nothing in the current directory is run: not bash, not where --- +plant="$TEST_ROOT/planted-cwd" +mkdir -p "$plant" +for name in bash.cmd where.bat; do + printf '@echo off\r\necho planted> "%s"\r\n' "$(cygpath -w "$TEST_ROOT/ran-$name")" > "$plant/$name" +done +run_hook "$sandbox" "$plant" probe-ok \ + LOCALAPPDATA="$NO_LA" PATH="$SYS32_PATH:$GIT_USR_BIN" +for name in bash.cmd where.bat; do + if [[ ! -e "$TEST_ROOT/ran-$name" && "$OUT" == *probe-ran* ]]; then + pass "$name in the current directory is not run" + else + fail "$name in the current directory is not run (ran: $([[ -e $TEST_ROOT/ran-$name ]] && echo yes || echo no), out=$OUT)" + fi +done + +# --- PATH entries whose names cmd could misparse still work --- +# fake_bash DIR: a bash.cmd that reports it ran. +fake_bash() { + mkdir -p "$1" + printf '@echo off\r\necho fake-bash-ran\r\n' > "$1/bash.cmd" +} +for dir_name in 'caret^and%OS%percent' $'caf\u00e9-bin' 'extensionless'; do + dir="$TEST_ROOT/$dir_name" + case "$dir_name" in + extensionless) + # An extensionless "bash" first on PATH must be skipped. + mkdir -p "$dir" + printf 'not a program\n' > "$dir/bash" + fake_bash "$TEST_ROOT/after-extensionless" + path="$dir:$TEST_ROOT/after-extensionless:$SYS32_PATH" + ;; + *) + fake_bash "$dir" + path="$dir:$SYS32_PATH" + ;; + esac + run_hook "$sandbox" "$TEST_ROOT" probe-ok LOCALAPPDATA="$NO_LA" PATH="$path" + if [[ "$RC" -eq 0 && "$OUT" == *fake-bash-ran* ]]; then + pass "bash on PATH is found under $dir_name" + else + fail "bash on PATH is found under $dir_name (rc=$RC, out=$OUT)" + fi +done + +# --- unset SystemRoot must not let the WSL launcher through --- +run_hook "$sandbox" "$TEST_ROOT" probe-ok -u SystemRoot \ + LOCALAPPDATA="$NO_LA" PATH="$SYS32_PATH:$GIT_USR_BIN" +if [[ "$RC" -eq 0 && "$OUT" != *Subsystem* ]]; then + pass "unset SystemRoot does not run the WSL launcher" +else + fail "unset SystemRoot does not run the WSL launcher (rc=$RC, out=$OUT)" +fi + +# --- unset LOCALAPPDATA must not probe \Programs\Git on the current drive --- +drive_programs="/c/Programs" +if [[ -e "$drive_programs" ]]; then + echo " [SKIP] unset LOCALAPPDATA: C:\\Programs already exists" +else + mkdir "$drive_programs" + junction 'C:\Programs\Git' "$PF_GIT" + run_hook "$sandbox" "$TEST_ROOT" probe-ok -u LOCALAPPDATA PATH="$SYS32_PATH" + cmd //c rmdir 'C:\Programs\Git' >/dev/null 2>&1 || true + rmdir "$drive_programs" + if [[ "$RC" -eq 0 && "$OUT" != *probe-ran* ]]; then + pass "unset LOCALAPPDATA does not run \\Programs\\Git on the current drive" + else + fail "unset LOCALAPPDATA does not run \\Programs\\Git on the current drive (rc=$RC, out=$OUT)" + fi +fi + +# --- no usable bash: exit 0 quietly, without invoking the WSL launcher --- +run_hook "$sandbox" "$TEST_ROOT" probe-ok \ + LOCALAPPDATA="$NO_LA" PATH="$SYS32_PATH" +if [[ "$RC" -eq 0 && -z "$OUT" ]]; then + pass "no usable bash exits 0 with no output" +else + fail "no usable bash exits 0 with no output (rc=$RC, out=$OUT)" +fi + +if [[ "$FAILURES" -gt 0 ]]; then + echo "STATUS: FAILED ($FAILURES failure(s))" + exit 1 +fi +echo "STATUS: PASSED" diff --git a/tests/hooks/test-session-start.sh b/tests/hooks/test-session-start.sh index c150829..6ddf011 100755 --- a/tests/hooks/test-session-start.sh +++ b/tests/hooks/test-session-start.sh @@ -217,6 +217,106 @@ assert_command_output \ CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ bash "$HOOK_UNDER_TEST" +# Regression for #2310: Claude Code can spawn SessionStart:startup hooks with +# a broken/empty PATH (anthropics/claude-code#43127). session-start and +# run-hook.cmd's Unix half both shell out to external binaries (dirname, cat) +# that an empty PATH cannot resolve, even though bash itself was invoked by +# absolute path. Every call here should use bash's own builtins/redirection +# instead, so an empty PATH must not change the result. +bash_bin="$(command -v bash)" + +broken_path_home="$(make_home broken-path)" +assert_command_output \ + "session-start with empty PATH still emits nested additionalContext" \ + "nested" \ + "" \ + "" \ + "$broken_path_home" \ + PATH="" \ + CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$bash_bin" "$HOOK_UNDER_TEST" + +broken_path_wrapper_home="$(make_home broken-path-wrapper)" +assert_command_output \ + "run-hook.cmd with empty PATH still dispatches session-start" \ + "nested" \ + "" \ + "" \ + "$broken_path_wrapper_home" \ + PATH="" \ + CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$bash_bin" "$WRAPPER_UNDER_TEST" session-start + +# Run by bare filename from inside hooks/ ($0 has no slash), the plugin root +# must still resolve. +bare_name_home="$(make_home bare-name)" +assert_command_output \ + "session-start run by bare filename from hooks/ still emits nested additionalContext" \ + "nested" \ + "" \ + "" \ + "$bare_name_home" \ + CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$bash_bin" -c 'cd "$1" && exec "$2" session-start' _ "$REPO_ROOT/hooks" "$bash_bin" + +bare_name_wrapper_home="$(make_home bare-name-wrapper)" +assert_command_output \ + "run-hook.cmd run by bare filename from hooks/ still dispatches session-start" \ + "nested" \ + "" \ + "" \ + "$bare_name_wrapper_home" \ + CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$bash_bin" -c 'cd "$1" && exec "$2" run-hook.cmd session-start' _ "$REPO_ROOT/hooks" "$bash_bin" + +# run-hook.cmd's Windows half starts bash with a backslash path +# (C:\...\hooks\session-start), so $0 can use \ as its separator. The skill +# must still be found. On Windows use the real Windows path; elsewhere build +# a plugin root whose hook is reached through a name containing a backslash. +backslash_home="$(make_home backslash)" +if command -v cygpath >/dev/null 2>&1; then + backslash_cwd="$REPO_ROOT" + backslash_hook="$(cygpath -w "$HOOK_UNDER_TEST")" +else + backslash_cwd="$TEST_ROOT/backslash/root" + mkdir -p "$backslash_cwd" + ln -s "$REPO_ROOT/hooks" "$backslash_cwd/hooks" + ln -s "$REPO_ROOT/skills" "$backslash_cwd/skills" + ln -s "$REPO_ROOT/hooks/session-start" "$backslash_cwd/hooks\\session-start" + backslash_hook='hooks\session-start' +fi +assert_command_output \ + "session-start reached through a backslash path still reads the skill" \ + "nested" \ + "" \ + "Error reading using-superpowers skill" \ + "$backslash_home" \ + CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$bash_bin" -c 'cd "$1" && exec "$2" "$3"' _ "$backslash_cwd" "$bash_bin" "$backslash_hook" + +# When cat is on PATH, the JSON must still go through it: the pipe absorbs +# EPIPE on Windows + Git Bash (#1612). The stub records that it ran. +cat_stub_dir="$TEST_ROOT/cat-stub/bin" +cat_marker="$TEST_ROOT/cat-stub/used" +mkdir -p "$cat_stub_dir" +printf '#!%s\n: > "%s"\nexec /bin/cat "$@"\n' "$bash_bin" "$cat_marker" > "$cat_stub_dir/cat" +chmod +x "$cat_stub_dir/cat" +cat_stub_home="$(make_home cat-stub)" +assert_command_output \ + "session-start with cat on PATH emits nested additionalContext" \ + "nested" \ + "" \ + "" \ + "$cat_stub_home" \ + PATH="$cat_stub_dir" \ + CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$bash_bin" "$HOOK_UNDER_TEST" +if [[ -f "$cat_marker" ]]; then + pass "session-start pipes its JSON through cat when cat is on PATH" +else + fail "session-start pipes its JSON through cat when cat is on PATH" +fi + if [[ "$FAILURES" -gt 0 ]]; then echo "STATUS: FAILED ($FAILURES failure(s))" exit 1 |
