|
| 1 | +#!/bin/bash |
| 2 | +set -ueo pipefail |
| 3 | + |
| 4 | +# A simple testcase runner that runs a command, captures all its command-line |
| 5 | +# outputs, and compares them against expected outputs. |
| 6 | + |
| 7 | +# Command-line parsing; this script is meant to be run from a higher-level |
| 8 | +# script, so don't do anything fancy. |
| 9 | +runwasi="$1" |
| 10 | +input="$2" |
| 11 | + |
| 12 | +# Compile names for generated files. |
| 13 | +wasm="$input" |
| 14 | +stdout_observed="$input.stdout.observed" |
| 15 | +stderr_observed="$input.stderr.observed" |
| 16 | +exit_status_observed="$input.exit_status.observed" |
| 17 | +tooldir=$(dirname $0) |
| 18 | + |
| 19 | +echo "Testing $input..." |
| 20 | + |
| 21 | +# Determine the input file to write to stdin. |
| 22 | +if [ -e "$input.stdin" ]; then |
| 23 | + stdin="$input.stdin" |
| 24 | +else |
| 25 | + stdin="/dev/null" |
| 26 | +fi |
| 27 | + |
| 28 | +# Determine any environment variables to set. |
| 29 | +if [ -e "$input.env" ]; then |
| 30 | + env=$(sed -e 's/^/--env /' < "$input.env") |
| 31 | +else |
| 32 | + env="" |
| 33 | +fi |
| 34 | + |
| 35 | +# Determine a preopened directory to provide. |
| 36 | +if [ -e "$input.dir" ]; then |
| 37 | + dir="--dir $input.dir" |
| 38 | + dirarg="$input.dir" |
| 39 | +else |
| 40 | + dir="" |
| 41 | + dirarg="" |
| 42 | +fi |
| 43 | + |
| 44 | +# Run the test, capturing stdout, stderr, and the exit status. |
| 45 | +exit_status=0 |
| 46 | +"$runwasi" $env $dir "$wasm" $dirarg \ |
| 47 | + < "$stdin" \ |
| 48 | + > "$stdout_observed" \ |
| 49 | + 2> "$stderr_observed" \ |
| 50 | + || exit_status=$? |
| 51 | +echo $exit_status > "$exit_status_observed" |
| 52 | + |
| 53 | +# Determine the reference files to compare with. |
| 54 | +if [ -e "$input.stdout.expected" ]; then |
| 55 | + stdout_expected="$input.stdout.expected" |
| 56 | + |
| 57 | + # Apply output filters. |
| 58 | + if [ -e "$input.stdout.expected.filter" ]; then |
| 59 | + cat "$stdout_observed" \ |
| 60 | + | "$input.stdout.expected.filter" \ |
| 61 | + > "${stdout_observed}.filtered" |
| 62 | + stdout_observed="${stdout_observed}.filtered" |
| 63 | + fi |
| 64 | +else |
| 65 | + stdout_expected="/dev/null" |
| 66 | +fi |
| 67 | +if [ -e "$input.stderr.expected" ]; then |
| 68 | + stderr_expected="$input.stderr.expected" |
| 69 | + |
| 70 | + # Apply output filters. |
| 71 | + if [ -e "$input.stderr.expected.filter" ]; then |
| 72 | + cat "$stderr_observed" \ |
| 73 | + | "./$input.stderr.expected.filter" \ |
| 74 | + > "${stderr_observed}.filtered" |
| 75 | + stderr_observed="${stderr_observed}.filtered" |
| 76 | + fi |
| 77 | +else |
| 78 | + stderr_expected="/dev/null" |
| 79 | +fi |
| 80 | +if [ -e "$input.exit_status.expected" ]; then |
| 81 | + exit_status_expected="$tooldir/$input.exit_status.expected" |
| 82 | +else |
| 83 | + exit_status_expected="$tooldir/exit_status_zero" |
| 84 | +fi |
| 85 | + |
| 86 | +# If there are any differences, diff will return a non-zero exit status, and |
| 87 | +# since this script uses "set -e", it will return a non-zero exit status too. |
| 88 | +diff -u "$stderr_expected" "$stderr_observed" |
| 89 | +diff -u "$stdout_expected" "$stdout_observed" |
| 90 | +diff -u "$exit_status_expected" "$exit_status_observed" |
0 commit comments