bash-hook-testinglisted
Install: claude install-skill oleksify/rnd-framework
# Bash Hook Testing
## Overview
Every hook script in `hooks/` has a corresponding test file in `tests/`. Tests use a custom framework (`test-helpers.sh`) that provides stdin-driven hook invocation, assertion helpers, and a pass/fail report. No external test runners are needed — each test file is a standalone bash script.
## Test File Structure
```bash
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/test-helpers.sh"
HOOK="${SCRIPT_DIR}/../hooks/<hook-name>.sh"
printf '%s\n' '--- Section Name ---'
# Test: description
run_hook "$HOOK" '{"tool_name":"Read","tool_input":{"file_path":"/some/path"}}'
assert_exit_code "description of expected exit code" 0
assert_eq "description of expected stdout" '{"hookSpecificOutput":...}' "$HOOK_STDOUT"
# ... more tests ...
report
```
Key points:
- File naming: `<hook-name>.test.sh` (e.g., `read-gate.test.sh`)
- Always call `report` at the end — it prints summary and exits 1 if any test failed
- Group related tests with `printf '%s\n' '--- Section Name ---'` headers
## Test Helpers API
### run_hook
```bash
run_hook "$HOOK" '{"tool_name":"Read","tool_input":{"file_path":"/foo"}}'
```
Feeds the JSON string as stdin to the hook script. After execution, three variables are set:
| Variable | Content |
|---|---|
| `HOOK_STDOUT` | Everything the hook printed to stdout |
| `HOOK_STDERR` | Everything the hook printed to stderr |
| `HOOK_EXIT` | The exit code (0, 2, etc.