interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / omega-reference / vendor / examples / gate.sh

154 lines · 5463 bytes Raw Blame History
1#!/usr/bin/env bash
2# gate.sh OMC: compile and run every worked example against its expected result.
3#
4# Covers examples/*.om and skills/*/examples/*.om. Each NAME.om needs NAME.expect:
5# compile N required; the compiler's exit status
6# stderr LINE required when N is not 0; the compiler's whole stderr is exactly LINE
7# exit N required when N is 0; the program's exit status
8# input dir optional; the compiler reads a directory instead of NAME.om (error 1)
9# output full optional; the compiler writes to /dev/full (error 2)
10# Optional fixtures: NAME.stdout (exact stdout bytes, absent means empty), NAME.stdin (piped in,
11# so reads can be partial), NAME.args (one argument per line), NAME.env (one KEY=VALUE per line).
12# Every program runs with `env -i` plus NAME.env, in an empty scratch directory, under a 20 s
13# timeout, and must write nothing to stderr. A compile that is not expected to succeed is never run.
14#
15# The compiler must match the pinned sha256 below; a different binary is refused, not tolerated.
16# Exit 0 only when every example passes; 1 on any failure; 2 on a usage or pin error.
17set -u
18
19PIN_REV=e5b32567a7aff3ce594e3254a622a846a6cd190b
20PIN_SHA=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f
21
22if [ "$#" -ne 1 ]; then
23 echo "usage: $0 /path/to/omc (omega-mv $PIN_REV seed/out/omc)" >&2
24 exit 2
25fi
26omc=$(cd "$(dirname "$1")" && pwd)/$(basename "$1")
27if [ ! -x "$omc" ]; then
28 echo "GATE FAIL $1 is not an executable file" >&2
29 exit 2
30fi
31got=$(sha256sum "$omc" | cut -d' ' -f1)
32if [ "$got" != "$PIN_SHA" ]; then
33 echo "GATE FAIL omc sha256 $got, pinned $PIN_SHA (omega-mv $PIN_REV)" >&2
34 exit 2
35fi
36
37here=$(cd "$(dirname "$0")" && pwd)
38root=$(dirname "$here")
39work=$(mktemp -d)
40trap 'rm -rf "$work"' EXIT
41
42pass=0
43fail=0
44total=0
45
46bad() {
47 echo "FAIL $name: $*"
48 fail=$((fail + 1))
49}
50
51# Expected files without a program are a silent loss of coverage, so they fail too.
52for f in "$root"/examples/*.expect "$root"/skills/*/examples/*.expect; do
53 [ -e "$f" ] || continue
54 if [ ! -e "${f%.expect}.om" ]; then
55 name=$(basename "${f%.expect}")
56 total=$((total + 1))
57 bad "expect file without $name.om"
58 fi
59done
60
61for src in "$root"/examples/*.om "$root"/skills/*/examples/*.om; do
62 [ -e "$src" ] || continue
63 base=${src%.om}
64 name=${base#"$root"/}
65 total=$((total + 1))
66 if [ ! -f "$base.expect" ]; then
67 bad "no $name.expect"
68 continue
69 fi
70 want_compile= want_stderr= want_exit= input=file output=file
71 while IFS= read -r line || [ -n "$line" ]; do
72 case $line in
73 "compile "*) want_compile=${line#compile } ;;
74 "stderr "*) want_stderr=${line#stderr } ;;
75 "exit "*) want_exit=${line#exit } ;;
76 "input dir") input=dir ;;
77 "output full") output=full ;;
78 "") ;;
79 *) want_compile=invalid ;;
80 esac
81 done < "$base.expect"
82 case $want_compile in
83 ''|invalid|*[!0-9]*) bad "malformed $name.expect"; continue ;;
84 esac
85 if [ "$want_compile" != 0 ] && [ -z "$want_stderr" ]; then
86 bad "$name.expect has no stderr line"; continue
87 fi
88 if [ "$want_compile" = 0 ]; then
89 case $want_exit in
90 ''|*[!0-9]*) bad "$name.expect has no exit line"; continue ;;
91 esac
92 fi
93
94 d=$work/$(echo "$name" | tr / _)
95 mkdir -p "$d/run"
96 in=$src
97 [ "$input" = dir ] && in=$d/run
98 out=$d/p
99 [ "$output" = full ] && out=/dev/full
100 "$omc" < "$in" > "$out" 2> "$d/cerr"
101 rc=$?
102 if [ "$rc" != "$want_compile" ]; then
103 bad "compile exit $rc, expected $want_compile: $(cat "$d/cerr")"
104 continue
105 fi
106 if [ "$want_compile" != 0 ]; then
107 if printf '%s\n' "$want_stderr" | cmp -s - "$d/cerr"; then
108 echo "PASS $name (compile $rc: $want_stderr)"
109 pass=$((pass + 1))
110 else
111 bad "compiler stderr '$(cat "$d/cerr")', expected '$want_stderr'"
112 fi
113 continue
114 fi
115 if [ -s "$d/cerr" ]; then
116 bad "compiler stderr not empty: $(cat "$d/cerr")"
117 continue
118 fi
119
120 chmod +x "$d/p"
121 args=()
122 envs=()
123 if [ -f "$base.args" ]; then mapfile -t args < "$base.args"; fi
124 if [ -f "$base.env" ]; then mapfile -t envs < "$base.env"; fi
125 if [ -f "$base.stdin" ]; then
126 (cd "$d/run" && cat "$base.stdin" | timeout 20 env -i "${envs[@]}" "$d/p" "${args[@]}" > "$d/out" 2> "$d/rerr")
127 else
128 (cd "$d/run" && timeout 20 env -i "${envs[@]}" "$d/p" "${args[@]}" < /dev/null > "$d/out" 2> "$d/rerr")
129 fi
130 rc=$?
131 want_out=/dev/null
132 [ -f "$base.stdout" ] && want_out=$base.stdout
133 if [ "$rc" != "$want_exit" ]; then
134 bad "exit $rc, expected $want_exit"
135 elif ! cmp -s "$want_out" "$d/out"; then
136 bad "stdout differs from $(basename "$want_out"): $(od -An -c "$d/out" | tr -s ' ' | tr -d '\n' | cut -c1-200)"
137 elif [ -s "$d/rerr" ]; then
138 bad "program stderr not empty: $(cat "$d/rerr")"
139 else
140 echo "PASS $name (exit $rc, $(wc -c < "$d/out" | tr -d ' ') stdout bytes)"
141 pass=$((pass + 1))
142 fi
143done
144
145if [ "$total" = 0 ]; then
146 echo "GATE FAIL no examples found under $root"
147 exit 1
148fi
149if [ "$fail" = 0 ] && [ "$pass" = "$total" ]; then
150 echo "GATE PASS examples=$total pass=$pass fail=0 omc=$PIN_SHA"
151 exit 0
152fi
153echo "GATE FAIL examples=$total pass=$pass fail=$fail omc=$got"
154exit 1