tests
This commit is contained in:
92
tests/run_examples.sh
Executable file
92
tests/run_examples.sh
Executable file
@@ -0,0 +1,92 @@
|
||||
#!/bin/bash
|
||||
# Example regression test runner
|
||||
# Usage: ./tests/run_examples.sh [--update]
|
||||
# --update: regenerate expected output and exit code files
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
SX="$ROOT_DIR/zig-out/bin/sx"
|
||||
EXPECTED_DIR="$SCRIPT_DIR/expected"
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
SKIP=0
|
||||
TIMEOUT_COUNT=0
|
||||
UPDATE=0
|
||||
TIMEOUT=10
|
||||
|
||||
if [[ "${1:-}" == "--update" ]]; then
|
||||
UPDATE=1
|
||||
fi
|
||||
|
||||
normalize() {
|
||||
sed 's/0x[0-9a-f]\{4,\}/0xADDR/g'
|
||||
}
|
||||
|
||||
for expected_file in "$EXPECTED_DIR"/*.txt; do
|
||||
name=$(basename "$expected_file" .txt)
|
||||
sx_file="$ROOT_DIR/examples/${name}.sx"
|
||||
exit_file="$EXPECTED_DIR/${name}.exit"
|
||||
|
||||
if [[ ! -f "$sx_file" ]]; then
|
||||
SKIP=$((SKIP + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
printf " %-30s" "$name"
|
||||
actual=$(timeout "$TIMEOUT" "$SX" run "$sx_file" 2>&1 | normalize)
|
||||
actual_exit=${PIPESTATUS[0]}
|
||||
|
||||
if [[ $actual_exit -eq 124 ]]; then
|
||||
TIMEOUT_COUNT=$((TIMEOUT_COUNT + 1))
|
||||
echo "TIMEOUT (>${TIMEOUT}s)"
|
||||
continue
|
||||
fi
|
||||
|
||||
if [[ $UPDATE -eq 1 ]]; then
|
||||
echo "$actual" > "$expected_file"
|
||||
echo "$actual_exit" > "$exit_file"
|
||||
echo " updated $name (exit=$actual_exit)"
|
||||
continue
|
||||
fi
|
||||
|
||||
expected=$(cat "$expected_file" | normalize)
|
||||
expected_exit=0
|
||||
if [[ -f "$exit_file" ]]; then
|
||||
expected_exit=$(cat "$exit_file")
|
||||
fi
|
||||
|
||||
output_ok=true
|
||||
exit_ok=true
|
||||
|
||||
if [[ "$actual" != "$expected" ]]; then
|
||||
output_ok=false
|
||||
fi
|
||||
if [[ "$actual_exit" != "$expected_exit" ]]; then
|
||||
exit_ok=false
|
||||
fi
|
||||
|
||||
if $output_ok && $exit_ok; then
|
||||
PASS=$((PASS + 1))
|
||||
echo "ok"
|
||||
else
|
||||
FAIL=$((FAIL + 1))
|
||||
echo "FAIL"
|
||||
if ! $output_ok; then
|
||||
diff <(echo "$expected") <(echo "$actual") || true
|
||||
fi
|
||||
if ! $exit_ok; then
|
||||
echo " exit code: expected=$expected_exit actual=$actual_exit"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ $UPDATE -eq 1 ]]; then
|
||||
echo "Updated all expected output files."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "$PASS passed, $FAIL failed, $SKIP skipped, $TIMEOUT_COUNT timed out"
|
||||
[[ $FAIL -eq 0 && $TIMEOUT_COUNT -eq 0 ]]
|
||||
Reference in New Issue
Block a user