Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b86yyhhudsSLnwNUFY
31 lines
1.3 KiB
Bash
Executable File
31 lines
1.3 KiB
Bash
Executable File
#!/bin/bash
|
|
# PostToolUse hook for Edit|Write|MultiEdit. Runs the test class that matches the edited Java file.
|
|
# Green: silent, exit 0. Red: failure lines on stderr and exit 2, which Claude Code shows to Claude.
|
|
INPUT=$(cat)
|
|
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
|
|
case "$FILE" in *.java) ;; *) exit 0 ;; esac
|
|
|
|
BASE=$(basename "$FILE" .java)
|
|
case "$BASE" in
|
|
*Test) TESTCLASS="$BASE" ;;
|
|
*) TESTCLASS="${BASE}Test" ;;
|
|
esac
|
|
|
|
ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
|
|
cd "$ROOT" || exit 0
|
|
if ! find src/test -name "${TESTCLASS}.java" | grep -q .; then
|
|
exit 0 # no matching test class: nothing to run
|
|
fi
|
|
|
|
OUT=$(mvn -B -q test -Dtest="$TESTCLASS" -Dsurefire.failIfNoSpecifiedTests=false 2>&1)
|
|
STATUS=$?
|
|
if [ $STATUS -ne 0 ]; then
|
|
echo "Tests failed after editing $(basename "$FILE") ($TESTCLASS):" >&2
|
|
# Surefire's "Failures:" block carries the assertion messages (expected/actual); prefer it.
|
|
FOUND=$(echo "$OUT" | awk '/^\[ERROR\] (Failures|Errors):/{p=1} p{print} p&&/^\[ERROR\] Tests run:/{exit}' | head -20)
|
|
[ -z "$FOUND" ] && FOUND=$(echo "$OUT" | grep -E '^\[ERROR\]' | grep -v -E 'Help 1|re-run Maven|For more information|full stack trace|See .*surefire|MojoFailure|^\[ERROR\] *$' | head -15)
|
|
if [ -n "$FOUND" ]; then echo "$FOUND" >&2; else echo "$OUT" | tail -15 >&2; fi
|
|
exit 2
|
|
fi
|
|
exit 0
|