Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tools/gate/selftest_devtrees.sh

13.6 KiB, 1 run, executable

created by r1870400018:66918, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#!/usr/bin/env bash
2# Proves devtrees.sh on synthetic trees, and proves the leak test can fail.
3#
4# selftest_devtrees.sh <austenite-bin>
5#
6# <austenite-bin> is the austenite binary under test (or AUSTENITE_BIN). Scratch is a child of
7# ~/.cache/austenite/devtrees, made here and removed on exit. Everything is synthetic: no path to a real tree
8# is taken or needed.
9#
10# It checks, in order:
11# 1. a clean tree (a relative import, an image and an include by a root-absolute path, a supplied font), two
12# mains, the second in a subdirectory, and a third that is not there: typst and the evaluator both exit 0
13# and agree on the pages, the curated reader gives a line, and the missing main is named;
14# 2. a syntax error: typst and the evaluator exit non-zero, and the strict run's failure is followed by the
15# lax run;
16# 3. a font family nobody has: typst warns and exits 0; the evaluator's strict run exits non-zero and its lax
17# run exits 0 with a page;
18# 4. a missing root, a binary that is not typst 0.15.1, no austenite binary, no arguments, an engine that
19# times out (124), and the tree's number;
20# 5. every line printed anywhere above is one of the whitelist's shapes, and no scratch is left behind;
21# 6. the marker word, ZZLEAKMARKERZZ, in a tree's directory and file names, its contents, its errors and the
22# family a font is asked for: the engines' own stderr quotes it (the premise), and nothing of it is
23# printed; a tool of the script's own that fails and quotes a path (mktemp, with a HOME that cannot hold the
24# scratch) is dropped as well; and ten lookalike lines go through the script's own whitelist, one result
25# among them, which is the only one to pass;
26# 7. the plant: the script's final whitelist made `cat`, and the stray test goes red.
27set -u
28
29HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
30CRATE="$(cd "$HERE/../.." && pwd)"
31# shellcheck source=gate_lib.sh
32. "$HERE/gate_lib.sh"
33
34AUS="${1:-${AUSTENITE_BIN:-}}"
35[ -n "$AUS" ] && [ -x "$AUS" ] || { echo "usage: selftest_devtrees.sh <austenite-bin>"; exit 2; }
36AUS="$(cd "$(dirname "$AUS")" && pwd)/$(basename "$AUS")"
37TYPST="${TYPST_BIN:-/home/jason/bin/typst}"
38DT="$HERE/devtrees.sh"
39PARENT="$HOME/.cache/austenite/devtrees"
40mkdir -p "$PARENT" || exit 2
41S="$(mktemp -d "$PARENT/selftest.XXXXXX")" || exit 2
42trap 'rm -rf "${S:?}"' EXIT
43GATE_SCRATCH="$S"
44MARK=ZZLEAKMARKERZZ
45
46bad=0
47ok() { echo "selftest: ok $1"; }
48fail() { echo "selftest: FAIL $1"; bad=$((bad + 1)); }
49expect() { # expect <label> <text> <fixed line the text must contain>
50 if printf '%s\n' "$2" | grep -qxF -- "$3"; then ok "$1"; else fail "$1 (wanted: $3)"; fi
51}
52expect_re() { # expect_re <label> <text> <extended regex a line must match whole>
53 if printf '%s\n' "$2" | grep -qE -- "^($3)\$"; then ok "$1"; else fail "$1 (wanted a line like: $3)"; fi
54}
55lack() { # lack <label> <text> <fixed string the text must not contain>
56 if printf '%s\n' "$2" | grep -qF -- "$3"; then fail "$1 (found: $3)"; else ok "$1"; fi
57}
58
59# run <script> <index> <args>...: the survey with both streams, as a caller sees them.
60run() {
61 local script="$1" idx="$2"
62 shift 2
63 DEVTREES_INDEX="$idx" AUSTENITE_BIN="$AUS" TYPST_BIN="$TYPST" "$script" "$@" 2>&1
64}
65
66# ── the synthetic trees ─────────────────────────────────────────────────────
67mktree() { # mktree <dir>
68 local t="$1"
69 mkdir -p "$t/chap" "$t/img" "$t/sub" "$t/assets/fonts"
70 cp "$CRATE/fonts/LibertinusMono-Regular.otf" "$t/assets/fonts/"
71 python3 - "$t/img/mark.png" <<'PY'
72import sys
73from PIL import Image
74Image.new('RGB', (8, 8), (200, 40, 40)).save(sys.argv[1])
75PY
76 printf '#let one = [One: some words on the first page.]\n' > "$t/chap/one.typ"
77 printf 'Two: the included page.\n' > "$t/chap/two.typ"
78 cat > "$t/main.typ" <<'TYP'
79#set page(width: 200pt, height: 120pt, margin: 12pt)
80#set text(size: 10pt)
81#import "chap/one.typ": one
82= Clean
83#image("/img/mark.png", width: 24pt)
84#text(font: "Libertinus Mono")[mono face]
85#one
86#pagebreak()
87#include "/chap/two.typ"
88TYP
89 cat > "$t/sub/page.typ" <<'TYP'
90#set page(width: 200pt, height: 120pt, margin: 12pt)
91#import "../chap/one.typ": one
92#one
93TYP
94}
95mktree "$S/clean"
96
97mkdir -p "$S/bad"
98printf '#set page(width: 200pt, height: 120pt)\n= Broken\n#let x = (1, 2\n' > "$S/bad/main.typ"
99
100mkdir -p "$S/font"
101printf '#set page(width: 200pt, height: 120pt)\n#set text(font: "ZZNoSuchFamilyZZ")\nA family nobody has.\n' > "$S/font/main.typ"
102
103# The marker in a directory name, a file name, a line of source, an error and a font family.
104MK="$S/root-$MARK"
105mkdir -p "$MK/dir-$MARK" "$MK/assets/fonts"
106printf '#let broken = (1, 2\n// %s in a comment\n' "$MARK" > "$MK/dir-$MARK/lib-$MARK.typ"
107cat > "$MK/dir-$MARK/main-$MARK.typ" <<TYP
108#set page(width: 200pt, height: 120pt)
109#set text(font: "family-$MARK")
110#import "lib-$MARK.typ": *
111Text #$MARK more.
112TYP
113
114# ── 1. a clean tree ─────────────────────────────────────────────────────────
115r="$(run "$DT" 0 "$S/clean" main.typ sub/page.typ gone.typ)"
116echo "$r" | sed 's/^/ /'
117ALL="$r"
118expect "clean: typst compiles main 1 in 2 pages" "$r" "tree 0 main 1 typst exit 0 pages 2 error 0 warning 0"
119expect "clean: the evaluator compiles it in 2 pages, strict" "$r" "tree 0 main 1 eval exit 0 pages 2 error 0 warning 0"
120expect_re "clean: the curated reader gives its line" "$r" "tree 0 main 1 curated exit [0-9]+ pages (none|[0-9]+) error [0-9]+ warning [0-9]+"
121expect "clean: main 2 in a subdirectory, run from there, reaches ../chap in typst" "$r" "tree 0 main 2 typst exit 0 pages 1 error 0 warning 0"
122expect "clean: and in the evaluator" "$r" "tree 0 main 2 eval exit 0 pages 1 error 0 warning 0"
123expect "clean: a main that is not there is named" "$r" "tree 0 main 3 missing-main"
124lack "clean: a strict run that passed has no lax run" "$r" "tree 0 main 1 eval-lax"
125
126# ── 2. a syntax error ───────────────────────────────────────────────────────
127r="$(run "$DT" 1 "$S/bad" main.typ)"
128echo "$r" | sed 's/^/ /'
129ALL="$ALL
130$r"
131expect_re "syntax error: typst exits non-zero, with no pages and an error" "$r" "tree 1 main 1 typst exit [1-9][0-9]* pages none error [1-9][0-9]* warning [0-9]+"
132expect_re "syntax error: the evaluator exits non-zero, with no pages" "$r" "tree 1 main 1 eval exit [1-9][0-9]* pages none error [0-9]+ warning [0-9]+"
133expect_re "syntax error: the strict failure is followed by the lax run" "$r" "tree 1 main 1 eval-lax exit [1-9][0-9]* pages none error [0-9]+ warning [0-9]+"
134
135# ── 3. a font family nobody has ─────────────────────────────────────────────
136r="$(run "$DT" 2 "$S/font" main.typ)"
137echo "$r" | sed 's/^/ /'
138ALL="$ALL
139$r"
140expect "font: typst warns and exits 0" "$r" "tree 2 main 1 typst exit 0 pages 1 error 0 warning 1"
141expect_re "font: the evaluator, strict, exits non-zero" "$r" "tree 2 main 1 eval exit [1-9][0-9]* pages none error [0-9]+ warning [0-9]+"
142expect_re "font: and lax exits 0 in 1 page" "$r" "tree 2 main 1 eval-lax exit 0 pages 1 error 0 warning [0-9]+"
143
144# ── 4. the guards ───────────────────────────────────────────────────────────
145r="$(run "$DT" 3 "$S/nowhere" main.typ)"
146ALL="$ALL
147$r"
148expect "guard: a root that is not there" "$r" "tree 3 missing-root"
149printf '#!/bin/sh\necho "typst 0.14.2 (stub)"\n' > "$S/typst-old"
150chmod +x "$S/typst-old"
151r="$(TYPST_BIN="$S/typst-old" DEVTREES_INDEX=0 AUSTENITE_BIN="$AUS" "$DT" "$S/clean" main.typ 2>&1)"
152ALL="$ALL
153$r"
154expect "guard: a typst that is not 0.15.1" "$r" "devtrees: typst is not 0.15.1"
155r="$(AUSTENITE_BIN="$S/none" TYPST_BIN="$TYPST" "$DT" "$S/clean" main.typ 2>&1)"
156ALL="$ALL
157$r"
158expect "guard: no austenite binary" "$r" "devtrees: no austenite binary"
159r="$("$DT" 2>&1)"
160ALL="$ALL
161$r"
162expect "guard: no arguments" "$r" "devtrees: usage"
163r="$(DEVTREES_INDEX=x AUSTENITE_BIN="$AUS" "$DT" "$S/clean" main.typ 2>&1)"
164ALL="$ALL
165$r"
166expect "guard: a tree number that is not digits" "$r" "devtrees: usage"
167printf '#!/bin/sh\n[ "$1" = --version ] && { echo "typst 0.15.1 (stub)"; exit 0; }\nexec sleep 30\n' > "$S/typst-slow"
168chmod +x "$S/typst-slow"
169r="$(TYPST_BIN="$S/typst-slow" DEVTREES_TIMEOUT=1 DEVTREES_INDEX=7 AUSTENITE_BIN="$AUS" "$DT" "$S/clean" sub/page.typ 2>&1)"
170ALL="$ALL
171$r"
172expect "guard: an engine that runs past the timeout exits 124" "$r" "tree 7 main 1 typst exit 124 pages none error 0 warning 0"
173expect_re "guard: the tree's number is the caller's" "$r" "tree 7 main 1 curated exit [0-9]+ pages (none|[0-9]+) error [0-9]+ warning [0-9]+"
174
175# ── 5. the vocabulary ───────────────────────────────────────────────────────
176WL='tree [0-9]+ main [0-9]+ (typst|curated|eval|eval-lax) exit [0-9]+ pages (none|[0-9]+) error [0-9]+ warning [0-9]+'
177WL="$WL|tree [0-9]+ missing-root|tree [0-9]+ main [0-9]+ missing-main"
178WL="$WL|devtrees: (typst is not 0\\.15\\.1|no austenite binary|usage|no scratch)"
179n="$(printf '%s\n' "$ALL" | grep -vcE "^($WL)\$")"
180[ "$n" -eq 0 ] && ok "whitelist: every line printed so far is one of the closed shapes" || fail "whitelist: $n line(s) outside the closed shapes"
181left="$(ls -d "$PARENT"/run.* 2>/dev/null | wc -l)"
182[ "$left" -eq 0 ] && ok "scratch: no run directory is left behind" || fail "scratch: $left run directory(ies) left under $PARENT"
183
184# ── 6. the marker ───────────────────────────────────────────────────────────
185raw="$(cd "$MK/dir-$MARK" && gate_typst "$TYPST" compile --root "$MK" --font-path "$MK/assets/fonts" "main-$MARK.typ" "$S/raw.pdf" 2>&1)"
186printf '%s\n' "$raw" | grep -qF "$MARK" && ok "leak premise: typst's own stderr quotes the tree's text" \
187 || fail "leak premise: typst did not quote the marker, so the next check proves nothing"
188raw="$(cd "$MK/dir-$MARK" && gate_hidden "$AUS" --eval --diag-summary --root "$MK" --font-path "$MK/assets/fonts" "main-$MARK.typ" "$S/rawout" 2>&1)"
189printf '%s\n' "$raw" | grep -qF "$MARK" && ok "leak premise: the evaluator's own stderr quotes it too" \
190 || fail "leak premise: the evaluator did not quote the marker"
191r="$(run "$DT" 4 "$MK" "dir-$MARK/main-$MARK.typ" "gone-$MARK.typ")"
192echo "$r" | sed 's/^/ /'
193lack "marker: nothing of a tree's names, text or errors is printed" "$r" "$MARK"
194expect_re "marker: typst's failure is counted" "$r" "tree 4 main 1 typst exit [1-9][0-9]* pages none error [1-9][0-9]* warning [0-9]+"
195expect "marker: a missing main is a closed word" "$r" "tree 4 main 2 missing-main"
196n="$(printf '%s\n' "$r" | grep -vcE "^($WL)\$")"
197[ "$n" -eq 0 ] && ok "marker: and every line is one of the closed shapes" || fail "marker: $n stray line(s)"
198
199# A stray from the script's own tools: HOME names a directory with the marker in its name, where the scratch
200# directory is made and cannot be, so that mktemp's own message quotes the path. The whitelist must drop it.
201mkdir -p "$S/home-$MARK/.cache/austenite/devtrees"
202chmod 555 "$S/home-$MARK/.cache/austenite/devtrees"
203stray() { # stray <script>
204 HOME="$S/home-$MARK" DEVTREES_INDEX=0 AUSTENITE_BIN="$AUS" TYPST_BIN="$TYPST" "$1" "$S/clean" main.typ 2>&1
205}
206raw="$(mktemp -d "$S/home-$MARK/.cache/austenite/devtrees/run.XXXXXX" 2>&1)"
207printf '%s\n' "$raw" | grep -qF "$MARK" && ok "leak premise: mktemp's own message quotes the path" \
208 || fail "leak premise: mktemp did not quote the marker, so the stray check proves nothing"
209r="$(stray "$DT")"
210lack "stray: a tool's message that quotes a path is dropped" "$r" "$MARK"
211expect "stray: and the script says so in a closed word" "$r" "devtrees: no scratch"
212
213# Lines that look like results but are not, through the script's own whitelist.
214prog="$(sed -n "/^WHITELIST='\$/,/'\$/p" "$DT" | sed "1d; \$s/'\$//")"
215if [ -z "$prog" ]; then
216 fail "whitelist: the program was not found in devtrees.sh"
217else
218 lookalikes="tree 0 main 1 typst exit 0 pages 1 error 0 warning 0
219tree 0 main 1 typst exit 0 pages 1 error 0 warning 0 $MARK
220tree $MARK main 1 typst exit 0 pages 1 error 0 warning 0
221 tree 0 main 1 typst exit 0 pages 1 error 0 warning 0
222tree 0 main 1 typst exit 0 pages $MARK error 0 warning 0
223tree 0 main 1 pdf exit 0 pages 1 error 0 warning 0
224tree 0 main $MARK missing-main
225tree 0 missing-root $MARK
226devtrees: usage $MARK
227devtrees: $MARK
228$MARK"
229 out="$(printf '%s\n' "$lookalikes" | awk "$prog")"
230 [ "$out" = "tree 0 main 1 typst exit 0 pages 1 error 0 warning 0" ] \
231 && ok "whitelist: of ten lines only the one result passes; each lookalike is dropped" \
232 || fail "whitelist: the lookalikes were not all dropped"
233fi
234
235# ── 7. the plant ────────────────────────────────────────────────────────────
236mkdir -p "$S/plant"
237cp "$HERE/gate_lib.sh" "$S/plant/"
238sed 's/| awk "\$WHITELIST"$/| cat/' "$DT" > "$S/plant/devtrees.sh"
239chmod +x "$S/plant/devtrees.sh"
240if cmp -s "$DT" "$S/plant/devtrees.sh" || ! grep -q '2>&1 | cat$' "$S/plant/devtrees.sh"; then
241 fail "plant: the sed did not change devtrees.sh (its final filter moved)"
242else
243 r="$(stray "$S/plant/devtrees.sh")"
244 if printf '%s\n' "$r" | grep -qF "$MARK"; then
245 ok "plant: with the final whitelist dropped the marker is printed, so the stray test is red"
246 else
247 fail "plant: the stray test stayed green with the final whitelist dropped"
248 fi
249fi
250
251echo "selftest: $bad failure(s)"
252[ "$bad" -eq 0 ]