#!/usr/bin/env bash # Run all test-*.sh files in the repo (including plugins) and the bats suite. # Usage: bash tests/run-tests.sh [--bats-only] # # A script exiting 77 (the automake convention) is reported as SKIPPED, not # passed — a suite that can't run for lack of a binary must not read as green. # # TEST_DIR — override root to search for test-*.sh (default: REPO_ROOT); used by tests. set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" BATS="$REPO_ROOT/tests/run-bats.sh" BATS_ONLY=false [[ "${1:-}" == "--bats-only" ]] && BATS_ONLY=true SEARCH_ROOT="${TEST_DIR:-$REPO_ROOT}" FAILED=() SKIPPED=() PASSED=0 SKIP_EXIT=77 run_bats() { if [[ -x "$BATS" ]]; then echo "=== bats ===" bash "$BATS" echo "" fi } if $BATS_ONLY; then run_bats exit 0 fi run_bats # Collected with a `while read` loop rather than `mapfile` — macOS ships # /bin/bash 3.2, which has no `mapfile`. Process substitution (not a pipe) # keeps the loop in this shell so the appends survive. `sort` is still fed # newline-delimited output, exactly as before. SCRIPTS=() while IFS= read -r script; do SCRIPTS+=("$script") done < <( find "$SEARCH_ROOT" -name "test-*.sh" \ -not -path "*/.git/*" \ -not -path "*/.claude/worktrees/*" \ | sort ) # bash before 4.4 treats "${arr[@]}" on an empty array as unbound under # `set -u`, so every array expansion here uses the ${arr[@]+"${arr[@]}"} guard, # including the SKIPPED/FAILED loops already fenced by a count check. for script in ${SCRIPTS[@]+"${SCRIPTS[@]}"}; do rel="${script#"$SEARCH_ROOT/"}" echo "=== $rel ===" rc=0 bash "$script" || rc=$? if [[ $rc -eq 0 ]]; then PASSED=$((PASSED + 1)) elif [[ $rc -eq $SKIP_EXIT ]]; then SKIPPED+=("$rel") else FAILED+=("$rel") fi echo "" done echo "=== Summary: $PASSED passed, ${#SKIPPED[@]} skipped, ${#FAILED[@]} failed ===" if [[ ${#SKIPPED[@]} -gt 0 ]]; then echo "Skipped scripts:" for s in ${SKIPPED[@]+"${SKIPPED[@]}"}; do echo " $s" done fi if [[ ${#FAILED[@]} -gt 0 ]]; then echo "Failed scripts:" for s in ${FAILED[@]+"${FAILED[@]}"}; do echo " $s" done exit 1 fi