Skip to content

Commit 30a2521

Browse files
etewiahclaude
andcommitted
Add test automation hooks and background watcher
- Claude Code post-edit hook that runs related tests after file edits - Background watcher script for continuous test monitoring - Supports NTFY push notifications for test failures Usage: ./scripts/watch-and-test.sh & # Background watcher # Hook runs automatically on Edit/Write in Claude Code 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c660da2 commit 30a2521

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

.claude/hooks/post-edit-tests.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
#
3+
# Claude Code Post-Edit Hook
4+
# Runs quick tests after file edits to catch issues early
5+
#
6+
7+
cd "$CLAUDE_PROJECT_DIR" || exit 0
8+
9+
# Get the file that was edited from environment
10+
FILE="${CLAUDE_TOOL_INPUT_file_path:-}"
11+
12+
# Skip if no file or not a Ruby/ERB file
13+
if [ -z "$FILE" ]; then
14+
exit 0
15+
fi
16+
17+
# Determine what tests to run based on file type
18+
case "$FILE" in
19+
*/models/*.rb)
20+
# Run related model specs
21+
MODEL_NAME=$(basename "$FILE" .rb)
22+
SPEC_FILE="spec/models/pwb/${MODEL_NAME}_spec.rb"
23+
if [ -f "$SPEC_FILE" ]; then
24+
echo "Running model tests for $MODEL_NAME..."
25+
bundle exec rspec "$SPEC_FILE" --fail-fast 2>&1 | tail -15
26+
fi
27+
;;
28+
*/helpers/*.rb)
29+
# Run helper specs
30+
HELPER_NAME=$(basename "$FILE" .rb)
31+
SPEC_FILE="spec/helpers/pwb/${HELPER_NAME}_spec.rb"
32+
if [ -f "$SPEC_FILE" ]; then
33+
echo "Running helper tests for $HELPER_NAME..."
34+
bundle exec rspec "$SPEC_FILE" --fail-fast 2>&1 | tail -15
35+
fi
36+
;;
37+
*/controllers/*.rb)
38+
# Run controller specs
39+
CONTROLLER_NAME=$(basename "$FILE" .rb)
40+
SPEC_FILE="spec/controllers/pwb/${CONTROLLER_NAME}_spec.rb"
41+
if [ -f "$SPEC_FILE" ]; then
42+
echo "Running controller tests for $CONTROLLER_NAME..."
43+
bundle exec rspec "$SPEC_FILE" --fail-fast 2>&1 | tail -15
44+
fi
45+
;;
46+
*/lib/tasks/*.rake)
47+
# Run rake task specs
48+
TASK_NAME=$(basename "$FILE" .rake)
49+
SPEC_FILE="spec/lib/tasks/${TASK_NAME}_spec.rb"
50+
if [ -f "$SPEC_FILE" ]; then
51+
echo "Running rake task tests for $TASK_NAME..."
52+
bundle exec rspec "$SPEC_FILE" --fail-fast 2>&1 | tail -15
53+
fi
54+
;;
55+
*.rb)
56+
# For other Ruby files, just check syntax
57+
echo "Checking Ruby syntax..."
58+
ruby -c "$FILE" 2>&1
59+
;;
60+
*.erb|*.liquid)
61+
# For templates, run a quick asset check
62+
echo "Template modified: $FILE"
63+
;;
64+
esac
65+
66+
exit 0

scripts/watch-and-test.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
#
3+
# Background test watcher - monitors git commits and runs tests
4+
#
5+
# Usage:
6+
# ./scripts/watch-and-test.sh # Run in foreground
7+
# ./scripts/watch-and-test.sh & # Run in background
8+
# nohup ./scripts/watch-and-test.sh & # Run and persist after terminal close
9+
#
10+
# Stop with: pkill -f watch-and-test.sh
11+
#
12+
13+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
14+
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
15+
cd "$PROJECT_ROOT"
16+
17+
NTFY_TOPIC="${NTFY_TOPIC:-}" # Set to your ntfy.sh topic for push notifications
18+
LAST_COMMIT=""
19+
20+
notify() {
21+
local title="$1"
22+
local message="$2"
23+
local priority="${3:-default}"
24+
25+
echo "[$title] $message"
26+
27+
# Desktop notification (macOS)
28+
if command -v osascript &> /dev/null; then
29+
osascript -e "display notification \"$message\" with title \"$title\""
30+
fi
31+
32+
# NTFY push notification
33+
if [ -n "$NTFY_TOPIC" ]; then
34+
curl -s -d "$message" -H "Title: $title" -H "Priority: $priority" \
35+
"https://ntfy.sh/$NTFY_TOPIC" > /dev/null
36+
fi
37+
}
38+
39+
run_tests() {
40+
local commit="$1"
41+
local commit_msg=$(git log -1 --format="%s" "$commit" 2>/dev/null)
42+
43+
echo ""
44+
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
45+
echo "🔍 Testing commit: $commit"
46+
echo " Message: $commit_msg"
47+
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
48+
49+
# Run RSpec tests
50+
echo "Running RSpec tests..."
51+
if bundle exec rspec --fail-fast 2>&1 | tail -20; then
52+
notify "✅ Tests Passed" "$commit_msg"
53+
else
54+
notify "❌ Tests Failed" "$commit_msg" "high"
55+
fi
56+
}
57+
58+
echo "👀 Watching for new commits..."
59+
echo " Press Ctrl+C to stop"
60+
echo ""
61+
62+
while true; do
63+
CURRENT_COMMIT=$(git rev-parse HEAD 2>/dev/null)
64+
65+
if [ "$CURRENT_COMMIT" != "$LAST_COMMIT" ] && [ -n "$LAST_COMMIT" ]; then
66+
run_tests "$CURRENT_COMMIT"
67+
fi
68+
69+
LAST_COMMIT="$CURRENT_COMMIT"
70+
sleep 5
71+
done

0 commit comments

Comments
 (0)