#!/usr/bin/env bash # ============================================================================= # tests/run.sh — Bash test runner # ============================================================================= # Discovers all *.test.sh files under tests/ and runs each function whose name # starts with "test_". Reports pass/fail counts and exits non-zero on failure. # ============================================================================= set -uo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PASS=0 FAIL=0 FAILED_TESTS=() while IFS= read -r test_file; do echo "── $(basename "$test_file") ──────────────────────" # Source the test file to get its functions if ! source "$test_file"; then echo " ✗ FAILED TO SOURCE: $test_file" FAIL=$((FAIL + 1)) FAILED_TESTS+=("SOURCE:$(basename "$test_file")") continue fi # Run every function starting with test_ for fn in $(declare -F | awk '{print $3}' | grep '^test_'); do if (set -e; "$fn") 2>&1 | sed 's/^/ /'; then echo " ✓ $fn" PASS=$((PASS + 1)) else echo " ✗ $fn" FAIL=$((FAIL + 1)) FAILED_TESTS+=("$fn") fi unset -f "$fn" done done < <(find "$SCRIPT_DIR" -name '*.test.sh' | sort) echo "" echo "===========================" echo " Passed: $PASS" echo " Failed: $FAIL" [[ $FAIL -gt 0 ]] && { echo " Failed tests: ${FAILED_TESTS[*]}"; exit 1; } exit 0