79 lines
3.2 KiB
Bash
79 lines
3.2 KiB
Bash
# shellcheck shell=bash
|
|
# Minimal test harness. Source from a test file, define test_* functions, call run_all.
|
|
# Each test runs in its own subshell with a fresh $T (temp dir) and cwd = $T.
|
|
|
|
HARNESS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
SCRIPTS="$(cd -- "$HARNESS_DIR/.." && pwd -P)"
|
|
SHIMS="$HARNESS_DIR/shims"
|
|
_PASS=0 _FAIL=0 _FAILED=()
|
|
_RM="$(command -v rm)"
|
|
_MKTEMP="$(command -v mktemp)" # resolved now: tests may shrink PATH later
|
|
_TMP_ROOT="$("$_MKTEMP" -d)"
|
|
trap '"$_RM" -rf -- "$_TMP_ROOT"' EXIT
|
|
|
|
_fail() { printf ' FAIL: %s\n' "$*" >&2; exit 1; }
|
|
|
|
# run CMD...: capture RC, OUT, ERR (stdin is /dev/null)
|
|
run() {
|
|
local o e
|
|
o="$("$_MKTEMP" -p "$_TMP_ROOT")" e="$("$_MKTEMP" -p "$_TMP_ROOT")"
|
|
RC=0
|
|
"$@" >"$o" 2>"$e" </dev/null || RC=$?
|
|
OUT="$(<"$o")" ERR="$(<"$e")"
|
|
"$_RM" -f -- "$o" "$e"
|
|
}
|
|
# run_in FILE CMD...: like run, but stdin comes from FILE.
|
|
run_in() {
|
|
local in="$1"; shift
|
|
local o e
|
|
o="$("$_MKTEMP" -p "$_TMP_ROOT")" e="$("$_MKTEMP" -p "$_TMP_ROOT")"
|
|
RC=0
|
|
"$@" >"$o" 2>"$e" <"$in" || RC=$?
|
|
OUT="$(<"$o")" ERR="$(<"$e")"
|
|
"$_RM" -f -- "$o" "$e"
|
|
}
|
|
|
|
# launch_like_terminal CMD...: start CMD in the background the way a terminal would:
|
|
# its own session/process group (so a signal reaches children too) with SIGINT at
|
|
# its default disposition (bash starts async jobs with SIGINT ignored). Sets LAUNCHED_PID.
|
|
launch_like_terminal() {
|
|
python3 -c 'import os,signal,sys; os.setsid(); signal.signal(signal.SIGINT, signal.SIG_DFL); os.execvp(sys.argv[1], sys.argv[1:])' "$@" >/dev/null 2>&1 &
|
|
LAUNCHED_PID=$!
|
|
}
|
|
# signal_group SIG: send SIG to the whole process group of LAUNCHED_PID, then reap it.
|
|
signal_group() { kill "-$1" -- "-$LAUNCHED_PID" 2>/dev/null; wait "$LAUNCHED_PID" 2>/dev/null; return 0; }
|
|
|
|
assert_rc() { [[ "$RC" == "$1" ]] || _fail "expected exit $1, got $RC${2:+ ($2)}
|
|
stdout: ${OUT:0:600}
|
|
stderr: ${ERR:0:1200}"; }
|
|
assert_eq() { [[ "$1" == "$2" ]] || _fail "${3:-values differ}
|
|
expected: $1
|
|
actual: $2"; }
|
|
assert_err_has() { [[ "$ERR" == *"$1"* ]] || _fail "stderr missing '$1'
|
|
stderr: ${ERR:0:1200}"; }
|
|
assert_out_has() { [[ "$OUT" == *"$1"* ]] || _fail "stdout missing '$1'
|
|
stdout: ${OUT:0:1200}"; }
|
|
assert_err_lacks() { [[ "$ERR" != *"$1"* ]] || _fail "stderr unexpectedly contains '$1'
|
|
stderr: ${ERR:0:1200}"; }
|
|
assert_file() { [[ -f "$1" ]] || _fail "expected file: $1"; }
|
|
assert_no_path() { [[ ! -e "$1" && ! -L "$1" ]] || _fail "unexpected path exists: $1"; }
|
|
assert_mode() { local m; m="$(stat -c %a -- "$1")"; [[ "$m" == "$2" ]] || _fail "mode of $1 is $m, expected $2"; }
|
|
assert_file_eq() { [[ "$(<"$1")" == "$2" ]] || _fail "contents of $1 differ
|
|
expected: $2
|
|
actual: $(<"$1")"; }
|
|
|
|
run_all() {
|
|
local filter="${1:-}" t
|
|
for t in $(declare -F | awk '{print $3}' | grep '^test_' || true); do
|
|
[[ -z "$filter" || "$t" == *"$filter"* ]] || continue
|
|
T="$("$_MKTEMP" -d -p "$_TMP_ROOT")"
|
|
if ( cd "$T" && export T && set +e && "$t" ) ; then
|
|
_PASS=$((_PASS + 1)); printf ' ok %s\n' "$t"
|
|
else
|
|
_FAIL=$((_FAIL + 1)); _FAILED+=("$t"); printf ' FAIL %s\n' "$t"
|
|
fi
|
|
done
|
|
printf '%s: %d passed, %d failed\n' "$(basename "$0")" "$_PASS" "$_FAIL"
|
|
((_FAIL == 0))
|
|
}
|