|
| 1 | +#!/usr/bin/env bash |
| 2 | +# tests/hooks/test-run-hook-relative-paths.sh |
| 3 | +# Bug e724-31a3: Verifies run-hook.sh resolves relative dispatcher paths |
| 4 | +# against its own hooks/ directory, so plugin.json can use relative paths |
| 5 | +# to minimize ${CLAUDE_PLUGIN_ROOT} occurrences in error messages. |
| 6 | +# |
| 7 | +# Usage: bash tests/hooks/test-run-hook-relative-paths.sh |
| 8 | + |
| 9 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | +REPO_ROOT="$(git rev-parse --show-toplevel)" |
| 11 | +DSO_PLUGIN_DIR="$REPO_ROOT/plugins/dso" |
| 12 | +RUN_HOOK="$DSO_PLUGIN_DIR/hooks/run-hook.sh" |
| 13 | + |
| 14 | +source "$REPO_ROOT/tests/lib/assert.sh" |
| 15 | + |
| 16 | +# ───────────────────────────────────────────────────────────── |
| 17 | +# test_run_hook_resolves_relative_path |
| 18 | +# run-hook.sh should resolve a relative path (e.g., "dispatchers/pre-bash.sh") |
| 19 | +# against its hooks/ directory, just as it resolves absolute paths. |
| 20 | +# We test this by creating a temp hook that echoes "OK" and passing it as |
| 21 | +# a relative path. |
| 22 | +# ───────────────────────────────────────────────────────────── |
| 23 | +TMPDIR_TEST=$(mktemp -d /tmp/test-run-hook-rel.XXXXXX) |
| 24 | +trap 'rm -rf "$TMPDIR_TEST"' EXIT |
| 25 | + |
| 26 | +# Create a minimal hook script in a relative subdir mirroring the dispatcher pattern |
| 27 | +mkdir -p "$TMPDIR_TEST/hooks/dispatchers" |
| 28 | +cat > "$TMPDIR_TEST/hooks/dispatchers/test-echo.sh" <<'HOOKEOF' |
| 29 | +#!/usr/bin/env bash |
| 30 | +echo "RELATIVE_OK" |
| 31 | +exit 0 |
| 32 | +HOOKEOF |
| 33 | +chmod +x "$TMPDIR_TEST/hooks/dispatchers/test-echo.sh" |
| 34 | + |
| 35 | +# Copy run-hook.sh to the temp hooks dir so it can resolve relative paths |
| 36 | +cp "$RUN_HOOK" "$TMPDIR_TEST/hooks/run-hook.sh" |
| 37 | +chmod +x "$TMPDIR_TEST/hooks/run-hook.sh" |
| 38 | + |
| 39 | +# Also need the hooks/lib dir for CLAUDE_PLUGIN_ROOT resolution fallback |
| 40 | +mkdir -p "$TMPDIR_TEST/hooks/lib" |
| 41 | + |
| 42 | +# Test: run-hook.sh with a relative dispatcher path should work |
| 43 | +output=$(CLAUDE_PLUGIN_ROOT="$TMPDIR_TEST" "$TMPDIR_TEST/hooks/run-hook.sh" "dispatchers/test-echo.sh" 2>&1) |
| 44 | +actual_exit=$? |
| 45 | + |
| 46 | +if [[ "$output" == *"RELATIVE_OK"* && "$actual_exit" -eq 0 ]]; then |
| 47 | + actual="resolved" |
| 48 | +else |
| 49 | + actual="not_resolved (exit=$actual_exit, output=$output)" |
| 50 | +fi |
| 51 | +assert_eq "test_run_hook_resolves_relative_path" "resolved" "$actual" |
| 52 | + |
| 53 | +# ───────────────────────────────────────────────────────────── |
| 54 | +# test_run_hook_absolute_path_still_works |
| 55 | +# Absolute paths must continue to work (backwards compatibility). |
| 56 | +# ───────────────────────────────────────────────────────────── |
| 57 | +output=$(CLAUDE_PLUGIN_ROOT="$TMPDIR_TEST" "$TMPDIR_TEST/hooks/run-hook.sh" "$TMPDIR_TEST/hooks/dispatchers/test-echo.sh" 2>&1) |
| 58 | +actual_exit=$? |
| 59 | + |
| 60 | +if [[ "$output" == *"RELATIVE_OK"* && "$actual_exit" -eq 0 ]]; then |
| 61 | + actual="works" |
| 62 | +else |
| 63 | + actual="broken (exit=$actual_exit, output=$output)" |
| 64 | +fi |
| 65 | +assert_eq "test_run_hook_absolute_path_still_works" "works" "$actual" |
| 66 | + |
| 67 | +print_summary |
0 commit comments