raz / test / compiler-host.sh

86 lines · 2.5 KiB · executable · raw · history · last change b6a29ae3

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
8raz=$(cd "$(dirname "$1")" && pwd)/$(basename "$1")
9top=$(mktemp -d "${TMPDIR:-/tmp}/raz-host-XXXXXX") || exit 1
10trap 'rm -rf "$top"' EXIT
11pass=0
12fail=0
13unset RAZ_PATH RAZ_PROFILE RAZ_TEST_WRITE
14NO_COLOR=1
15export NO_COLOR
16
17# check NAME COMMAND... passes if the command succeeds.
18check() {
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.
25says() { grep -q -F -- "$2" "$1"; }
26
27# lacks FILE TEXT: FILE doesn't hold TEXT.
28lacks() { ! grep -q -F -- "$2" "$1"; }
29
30# The staged sources and the compiler built from them.
31mkdir "$top/src" "$top/work"
32cp src/*.raz "$top/src"
33# staged as src/cross.sh does: the plain name takes the place of Linux's
34rm "$top"/src/Host.*.raz
35cp 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.
43cp -r lib "$top/library"
44echo "module HostValue; const value* = 42; end HostValue." >"$top/library/HostValue.raz"
45RAZ_TEST_LIBRARY=$top/library
46export RAZ_TEST_LIBRARY
47
48cd "$top/work" || exit 1
49cat >Main.raz <<'END'
50module Main;
51import Out, HostValue;
52proc main*;
53begin Out.int(HostValue.value); Out.ln; end;
54end Main.
55END
56cat >UsesC.raz <<'END'
57module UsesC;
58import SYSTEM;
59extern proc getpid: I32;
60proc main*;
61begin _ := getpid(); end;
62end UsesC.
63END
64
65# the library comes from the host, and the file is written
66"$top/compiler" -Werror -o program Main.raz 2>err
67check "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
71check "run is refused" says err "this test host cannot run programs"
72check "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
76check "C is refused" says err "cannot make a program that uses C"
77check "C leaves no file" test ! -e cprogram
78
79# a write the host refuses
80rm -f program
81RAZ_TEST_WRITE=refuse "$top/compiler" -o program Main.raz 2>err
82check "a refused write is an error" says err "can't write"
83check "a refused write leaves no file" test ! -e program
84
85echo "$pass passed, $fail failed"
86[ $fail = 0 ]