| 1 | #!/bin/sh |
| 2 | # Builds the compiler with another host, test/host/Host.raz in place of |
| 3 | # src/Host.linux.raz (the staging doc/compiler.md describes), and checks what |
| 4 | # that host changes: the library it names, a refused raz run, no C, and |
| 5 | # a refused write. |
| 6 | # Usage: test/compiler-host.sh path/to/raz |
| 7 | |
| 8 | raz=$(cd "$(dirname "$1")" && pwd)/$(basename "$1") |
| 9 | top=$(mktemp -d "${TMPDIR:-/tmp}/raz-host-XXXXXX") || exit 1 |
| 10 | trap 'rm -rf "$top"' EXIT |
| 11 | pass=0 |
| 12 | fail=0 |
| 13 | unset RAZ_PATH RAZ_PROFILE RAZ_TEST_WRITE |
| 14 | NO_COLOR=1 |
| 15 | export NO_COLOR |
| 16 | |
| 17 | # check NAME COMMAND... passes if the command succeeds. |
| 18 | check() { |
| 19 | name=$1 |
| 20 | shift |
| 21 | if "$@"; then pass=$((pass + 1)); else echo "FAIL $name"; fail=$((fail + 1)); fi |
| 22 | } |
| 23 | |
| 24 | # says FILE TEXT: FILE holds TEXT. |
| 25 | says() { grep -q -F -- "$2" "$1"; } |
| 26 | |
| 27 | # lacks FILE TEXT: FILE doesn't hold TEXT. |
| 28 | lacks() { ! grep -q -F -- "$2" "$1"; } |
| 29 | |
| 30 | # The staged sources and the compiler built from them. |
| 31 | mkdir "$top/src" "$top/work" |
| 32 | cp src/*.raz "$top/src" |
| 33 | # staged as src/cross.sh does: the plain name takes the place of Linux's |
| 34 | rm "$top"/src/Host.*.raz |
| 35 | cp test/host/Host.raz "$top/src/Host.raz" |
| 36 | "$raz" -Werror -I "$top/src" -I build/src -o "$top/compiler" "$top/src/Raz.raz" 2>"$top/build.err" || { |
| 37 | cat "$top/build.err" |
| 38 | echo "FAIL the compiler with the test host doesn't build" |
| 39 | exit 1 |
| 40 | } |
| 41 | |
| 42 | # A library of its own, which only this host's libraryDir finds. |
| 43 | cp -r lib "$top/library" |
| 44 | echo "module HostValue; const value* = 42; end HostValue." >"$top/library/HostValue.raz" |
| 45 | RAZ_TEST_LIBRARY=$top/library |
| 46 | export RAZ_TEST_LIBRARY |
| 47 | |
| 48 | cd "$top/work" || exit 1 |
| 49 | cat >Main.raz <<'END' |
| 50 | module Main; |
| 51 | import Out, HostValue; |
| 52 | proc main*; |
| 53 | begin Out.int(HostValue.value); Out.ln; end; |
| 54 | end Main. |
| 55 | END |
| 56 | cat >UsesC.raz <<'END' |
| 57 | module UsesC; |
| 58 | import SYSTEM; |
| 59 | extern proc getpid: I32; |
| 60 | proc main*; |
| 61 | begin _ := getpid(); end; |
| 62 | end UsesC. |
| 63 | END |
| 64 | |
| 65 | # the library comes from the host, and the file is written |
| 66 | "$top/compiler" -Werror -o program Main.raz 2>err |
| 67 | check "builds with the host's library" test "$(./program)" = 42 |
| 68 | |
| 69 | # raz run is refused, before the source is read |
| 70 | "$top/compiler" run missing.raz 2>err |
| 71 | check "run is refused" says err "this test host cannot run programs" |
| 72 | check "run is refused before the source is read" lacks err "can't open" |
| 73 | |
| 74 | # no C |
| 75 | "$top/compiler" -o cprogram UsesC.raz 2>err |
| 76 | check "C is refused" says err "cannot make a program that uses C" |
| 77 | check "C leaves no file" test ! -e cprogram |
| 78 | |
| 79 | # a write the host refuses |
| 80 | rm -f program |
| 81 | RAZ_TEST_WRITE=refuse "$top/compiler" -o program Main.raz 2>err |
| 82 | check "a refused write is an error" says err "can't write" |
| 83 | check "a refused write leaves no file" test ! -e program |
| 84 | |
| 85 | echo "$pass passed, $fail failed" |
| 86 | [ $fail = 0 ] |