|
| 1 | +set -euo pipefail |
| 2 | + |
| 3 | +ulimit -s ${MAIN_STACK_SIZE:-8192} |
| 4 | +DIFF=diff |
| 5 | +if diff --color --help >/dev/null 2>&1; then |
| 6 | + DIFF="diff --color"; |
| 7 | +fi |
| 8 | + |
| 9 | +function fail { |
| 10 | + echo $1 |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +INTERACTIVE=no |
| 15 | +if [ $1 == "-i" ]; then |
| 16 | + INTERACTIVE=yes |
| 17 | + shift |
| 18 | +fi |
| 19 | +f="$1" |
| 20 | +shift |
| 21 | +[ $# -eq 0 ] || fail "Usage: test_single.sh [-i] test-file.lean" |
| 22 | + |
| 23 | +function lean_has_llvm_support { |
| 24 | + lean --features | grep -q "LLVM" |
| 25 | +} |
| 26 | + |
| 27 | +function compile_lean_c_backend { |
| 28 | + lean --c="$f.c" "$f" || fail "Failed to compile $f into C file" |
| 29 | + leanc ${LEANC_OPTS-} -O3 -DNDEBUG -o "$f.out" "$@" "$f.c" || fail "Failed to compile C file $f.c" |
| 30 | +} |
| 31 | + |
| 32 | +function compile_lean_llvm_backend { |
| 33 | + set -o xtrace |
| 34 | + rm "*.ll" || true # remove debugging files. |
| 35 | + rm "*.bc" || true # remove bitcode files |
| 36 | + rm "*.o" || true # remove object files |
| 37 | + lean --bc="$f.linked.bc" "$f" || fail "Failed to compile $f into bitcode file" |
| 38 | + leanc ${LEANC_OPTS-} -O3 -DNDEBUG -o "$f.out" "$@" "$f.linked.bc" || fail "Failed to link object file '$f.linked.bc'" |
| 39 | + set +o xtrace |
| 40 | +} |
| 41 | + |
| 42 | +function exec_capture_raw { |
| 43 | + # backtraces are system-specific, strip them (might be captured in `#guard_msgs`) |
| 44 | + LEAN_BACKTRACE=0 "$@" 2>&1 > "$f.produced.out" |
| 45 | +} |
| 46 | + |
| 47 | +# produces filtered output intended for usage with `diff_produced` |
| 48 | +function exec_capture { |
| 49 | + # backtraces are system-specific, strip them |
| 50 | + # mvar suffixes like in `?m.123` are deterministic but prone to change on minor changes, so strip them |
| 51 | + # similarly, links to the language reference may have URL components depending on the toolchain, so normalize those |
| 52 | + LEAN_BACKTRACE=0 "$@" 2>&1 \ |
| 53 | + | perl -pe 's/(\?(\w|_\w+))\.[0-9]+/\1/g' \ |
| 54 | + | perl -pe 's/https:\/\/lean-lang\.org\/doc\/reference\/(v?[0-9.]+(-rc[0-9]+)?|latest)/REFERENCE/g' > "$f.produced.out" |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +# Remark: `${var+x}` is a parameter expansion which evaluates to nothing if `var` is unset, and substitutes the string `x` otherwise. |
| 59 | +function check_ret { |
| 60 | + [ -n "${expected_ret+x}" ] || expected_ret=0 |
| 61 | + [ -f "$f.expected.ret" ] && expected_ret=$(< "$f.expected.ret") |
| 62 | + if [ -n "$expected_ret" ] && [ $ret -ne $expected_ret ]; then |
| 63 | + echo "Unexpected return code $ret executing '$@'; expected $expected_ret. Output:" |
| 64 | + cat "$f.produced.out" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +function exec_check_raw { |
| 70 | + ret=0 |
| 71 | + exec_capture_raw "$@" || ret=$? |
| 72 | + check_ret "$@" |
| 73 | +} |
| 74 | + |
| 75 | +# produces filtered output intended for usage with `diff_produced` |
| 76 | +function exec_check { |
| 77 | + ret=0 |
| 78 | + exec_capture "$@" || ret=$? |
| 79 | + check_ret "$@" |
| 80 | +} |
| 81 | + |
| 82 | +function diff_produced { |
| 83 | + if test -f "$f.expected.out"; then |
| 84 | + if $DIFF -au --strip-trailing-cr -I "executing external script" "$f.expected.out" "$f.produced.out"; then |
| 85 | + : |
| 86 | + else |
| 87 | + echo "ERROR: file $f.produced.out does not match $f.expected.out" |
| 88 | + if [ $INTERACTIVE == "yes" ]; then |
| 89 | + meld "$f.produced.out" "$f.expected.out" |
| 90 | + if diff -I "executing external script" "$f.expected.out" "$f.produced.out"; then |
| 91 | + echo "-- mismatch was fixed" |
| 92 | + fi |
| 93 | + fi |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + else |
| 97 | + echo "ERROR: file $f.expected.out does not exist" |
| 98 | + if [ $INTERACTIVE == "yes" ]; then |
| 99 | + read -p "copy $f.produced.out (y/n)? " |
| 100 | + if [ $REPLY == "y" ]; then |
| 101 | + cp -- "$f.produced.out" "$f.expected.out" |
| 102 | + echo "-- copied $f.produced.out --> $f.expected.out" |
| 103 | + fi |
| 104 | + fi |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | +} |
0 commit comments