Skip to content

Commit 713727f

Browse files
authored
Initial checkin of test scripts, copied from wasi-sdk. (#397)
* Initial checkin of test scripts, copied from wasi-sdk. * Add a minimal command test. * Adjust the scripts so that they run the minimal-command.wasm test.
1 parent 78b7367 commit 713727f

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

Diff for: test/minimal-command.wasm

36 Bytes
Binary file not shown.

Diff for: test/sources/generate-wasm.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -ueo pipefail
3+
4+
for wat in wat/*.wat; do
5+
wat2wasm "$wat"
6+
done
7+
8+
mv *.wasm ..

Diff for: test/sources/wat/minimal-command.wat

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(module
2+
(func (export "_start"))
3+
)

Diff for: tools/test-harness/exit_status_zero

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

Diff for: tools/test-harness/run.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -ueo pipefail
3+
4+
# Top-level test runner. Usage is "run.sh" to run tests in compile-only mode,
5+
# or "run.sh <runwasi>" where <runwasi> is a WASI-capable runtime to run the
6+
# tests in full compile and execute mode.
7+
8+
# Determine the wasm runtime to use.
9+
runwasi="$1"
10+
11+
tooldir=$(dirname $0)
12+
13+
echo "===== Testing ====="
14+
for file in *.wasm; do
15+
"$tooldir/testcase.sh" "$runwasi" "$file"
16+
done
17+
cd - >/dev/null

Diff for: tools/test-harness/testcase.sh

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)