110 lines
2.6 KiB
Bash
Executable File
110 lines
2.6 KiB
Bash
Executable File
#!/bin/sh
|
|
# tests/harness.sh - Minimal POSIX test harness for VX.
|
|
#
|
|
# Source this file at the top of each test script. It provides:
|
|
# check_exit CMD EXPECTED_EXIT -- assert exit code
|
|
# check_eq ACTUAL EXPECTED MSG -- assert string equality
|
|
# check_file ACTUAL_FILE EXPECTED_FILE MSG -- assert file equality
|
|
# check_grep PATTERN FILE MSG -- assert pattern in file
|
|
# pass / fail / summary
|
|
#
|
|
# The test script must call summary() at the end.
|
|
|
|
_pass=0
|
|
_fail=0
|
|
_harness_tmpdir=""
|
|
|
|
# Resolve VX binary: prefer $VX env var, else ../builddir/vx from script dir
|
|
if [ -z "$VX" ]; then
|
|
_script_dir="$(cd "$(dirname "$0")" && pwd)"
|
|
VX="${_script_dir}/../../builddir/vx"
|
|
fi
|
|
export VX
|
|
|
|
setup_tmpdir() {
|
|
_harness_tmpdir="$(mktemp -d)"
|
|
cd "$_harness_tmpdir" || exit 1
|
|
}
|
|
|
|
cleanup_tmpdir() {
|
|
if [ -n "$_harness_tmpdir" ] && [ -d "$_harness_tmpdir" ]; then
|
|
rm -rf "$_harness_tmpdir"
|
|
fi
|
|
}
|
|
|
|
pass() {
|
|
_pass=$((_pass + 1))
|
|
printf " PASS: %s\n" "$1"
|
|
}
|
|
|
|
fail() {
|
|
_fail=$((_fail + 1))
|
|
printf " FAIL: %s\n" "$1"
|
|
if [ -n "$2" ]; then
|
|
printf " %s\n" "$2"
|
|
fi
|
|
}
|
|
|
|
# check_exit "description" expected_exit command [args...]
|
|
check_exit() {
|
|
_desc="$1"; shift
|
|
_expect="$1"; shift
|
|
"$@" >/dev/null 2>&1
|
|
_got=$?
|
|
if [ "$_got" -eq "$_expect" ]; then
|
|
pass "$_desc"
|
|
else
|
|
fail "$_desc" "expected exit=$_expect, got exit=$_got"
|
|
fi
|
|
}
|
|
|
|
# check_output "description" "expected_stdout" command [args...]
|
|
check_output() {
|
|
_desc="$1"; shift
|
|
_expect="$1"; shift
|
|
_got="$("$@" 2>/dev/null)" || true
|
|
if [ "$_got" = "$_expect" ]; then
|
|
pass "$_desc"
|
|
else
|
|
fail "$_desc" "expected: $(printf '%s' "$_expect" | head -c 200), got: $(printf '%s' "$_got" | head -c 200)"
|
|
fi
|
|
}
|
|
|
|
# check_eq "got" "expected" "description"
|
|
check_eq() {
|
|
if [ "$1" = "$2" ]; then
|
|
pass "$3"
|
|
else
|
|
fail "$3" "expected: $(printf '%s' "$2" | head -c 200), got: $(printf '%s' "$1" | head -c 200)"
|
|
fi
|
|
}
|
|
|
|
# check_file actual_file expected_file "description"
|
|
check_file() {
|
|
if diff -q "$1" "$2" >/dev/null 2>&1; then
|
|
pass "$3"
|
|
else
|
|
fail "$3" "files differ: $1 vs $2"
|
|
fi
|
|
}
|
|
|
|
# check_grep pattern file "description"
|
|
check_grep() {
|
|
if grep -q "$1" "$2" 2>/dev/null; then
|
|
pass "$3"
|
|
else
|
|
fail "$3" "pattern '$1' not found in $2"
|
|
fi
|
|
}
|
|
|
|
summary() {
|
|
cleanup_tmpdir
|
|
_total=$((_pass + _fail))
|
|
echo ""
|
|
printf "%s: %d passed, %d failed (of %d)\n" "$_test_name" "$_pass" "$_fail" "$_total"
|
|
if [ "$_fail" -gt 0 ]; then
|
|
exit 1
|
|
fi
|
|
exit 0
|
|
}
|