lib-runner.sh (2348B)
1 ## tests/lib-runner.sh — shared helpers for test suite drivers. 2 ## 3 ## Sourced by tests/run-suite.sh (in-container suite driver) and may be 4 ## sourced by other test entry points. Pure-shell, no side effects on 5 ## source. Callers set $ARCH and $NAMES (optional fixture filter) before 6 ## invoking the helpers. 7 8 # discover <dir> <ext> 9 # List basenames under <dir> matching <name>.<ext>, excluding leading 10 # underscore (reserved for harness fragments like _run-pp.scm). 11 discover() { 12 dir=$1; ext=$2 13 ls "$dir" 2>/dev/null \ 14 | sed -n "s/^\\([^_][^.]*\\)\\.$ext\$/\\1/p" \ 15 | sort -u 16 } 17 18 report() { 19 label=$1; status=$2 20 echo " $status $label" 21 } 22 23 show_diff() { 24 expected=$1; actual=$2 25 echo " --- expected ---" 26 printf '%s\n' "$expected" | sed 's/^/ /' 27 echo " --- actual ---" 28 printf '%s\n' "$actual" | sed 's/^/ /' 29 } 30 31 # fail <label> [<heading>] [<log-file>] 32 # Emit a FAIL row plus an optional indented heading and indented log 33 # contents. Lets every suite handle a failed sub-step without re-running 34 # the failing command to capture its stderr. 35 fail() { 36 label=$1 37 report "$label" FAIL 38 [ -n "${2:-}" ] && echo " $2" || : 39 if [ -n "${3:-}" ] && [ -e "${3:-}" ]; then 40 sed 's/^/ /' "$3" >&2 || true 41 fi 42 } 43 44 # compare_runtime <label> <expected-stdout> <expected-exit> <actual-stdout> <actual-exit> 45 # PASS iff stdout and exit both match; otherwise FAIL with a diff and/or 46 # exit-code mismatch line. 47 compare_runtime() { 48 lbl=$1; exp_out=$2; exp_exit=$3; act_out=$4; act_exit=$5 49 if [ "$act_out" = "$exp_out" ] && [ "$act_exit" = "$exp_exit" ]; then 50 report "$lbl" PASS 51 else 52 report "$lbl" FAIL 53 if [ "$act_out" != "$exp_out" ]; then show_diff "$exp_out" "$act_out"; fi 54 if [ "$act_exit" != "$exp_exit" ]; then 55 echo " exit: expected $exp_exit, got $act_exit" 56 fi 57 fi 58 } 59 60 # read_expected <path> [<default>] 61 # Print contents of <path> if it exists; else <default> (empty if 62 # omitted). Trailing-newline behavior follows cat: every suite already 63 # compares with `[ "$a" = "$b" ]` after $(cat …) so the strip-trailing- 64 # newline of $() is uniform. 65 read_expected() { 66 path=$1; default=${2:-} 67 if [ -e "$path" ]; then 68 cat "$path" 69 else 70 printf '%s' "$default" 71 fi 72 }