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. |
| 27 | set -u |
| 28 | |
| 29 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 30 | CRATE="$(cd "$HERE/../.." && pwd)" |
| 31 | # shellcheck source=gate_lib.sh |
| 32 | . "$HERE/gate_lib.sh" |
| 33 | |
| 34 | AUS="${1:-${AUSTENITE_BIN:-}}" |
| 35 | [ -n "$AUS" ] && [ -x "$AUS" ] || { echo "usage: selftest_devtrees.sh <austenite-bin>"; exit 2; } |
| 36 | AUS="$(cd "$(dirname "$AUS")" && pwd)/$(basename "$AUS")" |
| 37 | TYPST="${TYPST_BIN:-/home/jason/bin/typst}" |
| 38 | DT="$HERE/devtrees.sh" |
| 39 | PARENT="$HOME/.cache/austenite/devtrees" |
| 40 | mkdir -p "$PARENT" || exit 2 |
| 41 | S="$(mktemp -d "$PARENT/selftest.XXXXXX")" || exit 2 |
| 42 | trap 'rm -rf "${S:?}"' EXIT |
| 43 | GATE_SCRATCH="$S" |
| 44 | MARK=ZZLEAKMARKERZZ |
| 45 | |
| 46 | bad=0 |
| 47 | ok() { echo "selftest: ok $1"; } |
| 48 | fail() { echo "selftest: FAIL $1"; bad=$((bad + 1)); } |
| 49 | expect() { # 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 | } |
| 52 | expect_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 | } |
| 55 | lack() { # 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. |
| 60 | run() { |
| 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 ───────────────────────────────────────────────────── |
| 67 | mktree() { # 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' |
| 72 | import sys |
| 73 | from PIL import Image |
| 74 | Image.new('RGB', (8, 8), (200, 40, 40)).save(sys.argv[1]) |
| 75 | PY |
| 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" |
| 88 | TYP |
| 89 | cat > "$t/sub/page.typ" <<'TYP' |
| 90 | #set page(width: 200pt, height: 120pt, margin: 12pt) |
| 91 | #import "../chap/one.typ": one |
| 92 | #one |
| 93 | TYP |
| 94 | } |
| 95 | mktree "$S/clean" |
| 96 | |
| 97 | mkdir -p "$S/bad" |
| 98 | printf '#set page(width: 200pt, height: 120pt)\n= Broken\n#let x = (1, 2\n' > "$S/bad/main.typ" |
| 99 | |
| 100 | mkdir -p "$S/font" |
| 101 | printf '#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. |
| 104 | MK="$S/root-$MARK" |
| 105 | mkdir -p "$MK/dir-$MARK" "$MK/assets/fonts" |
| 106 | printf '#let broken = (1, 2\n// %s in a comment\n' "$MARK" > "$MK/dir-$MARK/lib-$MARK.typ" |
| 107 | cat > "$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": * |
| 111 | Text #$MARK more. |
| 112 | TYP |
| 113 | |
| 114 | # ── 1. a clean tree ───────────────────────────────────────────────────────── |
| 115 | r="$(run "$DT" 0 "$S/clean" main.typ sub/page.typ gone.typ)" |
| 116 | echo "$r" | sed 's/^/ /' |
| 117 | ALL="$r" |
| 118 | expect "clean: typst compiles main 1 in 2 pages" "$r" "tree 0 main 1 typst exit 0 pages 2 error 0 warning 0" |
| 119 | expect "clean: the evaluator compiles it in 2 pages, strict" "$r" "tree 0 main 1 eval exit 0 pages 2 error 0 warning 0" |
| 120 | expect_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]+" |
| 121 | expect "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" |
| 122 | expect "clean: and in the evaluator" "$r" "tree 0 main 2 eval exit 0 pages 1 error 0 warning 0" |
| 123 | expect "clean: a main that is not there is named" "$r" "tree 0 main 3 missing-main" |
| 124 | lack "clean: a strict run that passed has no lax run" "$r" "tree 0 main 1 eval-lax" |
| 125 | |
| 126 | # ── 2. a syntax error ─────────────────────────────────────────────────────── |
| 127 | r="$(run "$DT" 1 "$S/bad" main.typ)" |
| 128 | echo "$r" | sed 's/^/ /' |
| 129 | ALL="$ALL |
| 130 | $r" |
| 131 | expect_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]+" |
| 132 | expect_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]+" |
| 133 | expect_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 ───────────────────────────────────────────── |
| 136 | r="$(run "$DT" 2 "$S/font" main.typ)" |
| 137 | echo "$r" | sed 's/^/ /' |
| 138 | ALL="$ALL |
| 139 | $r" |
| 140 | expect "font: typst warns and exits 0" "$r" "tree 2 main 1 typst exit 0 pages 1 error 0 warning 1" |
| 141 | expect_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]+" |
| 142 | expect_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 ─────────────────────────────────────────────────────────── |
| 145 | r="$(run "$DT" 3 "$S/nowhere" main.typ)" |
| 146 | ALL="$ALL |
| 147 | $r" |
| 148 | expect "guard: a root that is not there" "$r" "tree 3 missing-root" |
| 149 | printf '#!/bin/sh\necho "typst 0.14.2 (stub)"\n' > "$S/typst-old" |
| 150 | chmod +x "$S/typst-old" |
| 151 | r="$(TYPST_BIN="$S/typst-old" DEVTREES_INDEX=0 AUSTENITE_BIN="$AUS" "$DT" "$S/clean" main.typ 2>&1)" |
| 152 | ALL="$ALL |
| 153 | $r" |
| 154 | expect "guard: a typst that is not 0.15.1" "$r" "devtrees: typst is not 0.15.1" |
| 155 | r="$(AUSTENITE_BIN="$S/none" TYPST_BIN="$TYPST" "$DT" "$S/clean" main.typ 2>&1)" |
| 156 | ALL="$ALL |
| 157 | $r" |
| 158 | expect "guard: no austenite binary" "$r" "devtrees: no austenite binary" |
| 159 | r="$("$DT" 2>&1)" |
| 160 | ALL="$ALL |
| 161 | $r" |
| 162 | expect "guard: no arguments" "$r" "devtrees: usage" |
| 163 | r="$(DEVTREES_INDEX=x AUSTENITE_BIN="$AUS" "$DT" "$S/clean" main.typ 2>&1)" |
| 164 | ALL="$ALL |
| 165 | $r" |
| 166 | expect "guard: a tree number that is not digits" "$r" "devtrees: usage" |
| 167 | printf '#!/bin/sh\n[ "$1" = --version ] && { echo "typst 0.15.1 (stub)"; exit 0; }\nexec sleep 30\n' > "$S/typst-slow" |
| 168 | chmod +x "$S/typst-slow" |
| 169 | r="$(TYPST_BIN="$S/typst-slow" DEVTREES_TIMEOUT=1 DEVTREES_INDEX=7 AUSTENITE_BIN="$AUS" "$DT" "$S/clean" sub/page.typ 2>&1)" |
| 170 | ALL="$ALL |
| 171 | $r" |
| 172 | expect "guard: an engine that runs past the timeout exits 124" "$r" "tree 7 main 1 typst exit 124 pages none error 0 warning 0" |
| 173 | expect_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 ─────────────────────────────────────────────────────── |
| 176 | WL='tree [0-9]+ main [0-9]+ (typst|curated|eval|eval-lax) exit [0-9]+ pages (none|[0-9]+) error [0-9]+ warning [0-9]+' |
| 177 | WL="$WL|tree [0-9]+ missing-root|tree [0-9]+ main [0-9]+ missing-main" |
| 178 | WL="$WL|devtrees: (typst is not 0\\.15\\.1|no austenite binary|usage|no scratch)" |
| 179 | n="$(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" |
| 181 | left="$(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 ─────────────────────────────────────────────────────────── |
| 185 | raw="$(cd "$MK/dir-$MARK" && gate_typst "$TYPST" compile --root "$MK" --font-path "$MK/assets/fonts" "main-$MARK.typ" "$S/raw.pdf" 2>&1)" |
| 186 | printf '%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" |
| 188 | raw="$(cd "$MK/dir-$MARK" && gate_hidden "$AUS" --eval --diag-summary --root "$MK" --font-path "$MK/assets/fonts" "main-$MARK.typ" "$S/rawout" 2>&1)" |
| 189 | printf '%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" |
| 191 | r="$(run "$DT" 4 "$MK" "dir-$MARK/main-$MARK.typ" "gone-$MARK.typ")" |
| 192 | echo "$r" | sed 's/^/ /' |
| 193 | lack "marker: nothing of a tree's names, text or errors is printed" "$r" "$MARK" |
| 194 | expect_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]+" |
| 195 | expect "marker: a missing main is a closed word" "$r" "tree 4 main 2 missing-main" |
| 196 | n="$(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. |
| 201 | mkdir -p "$S/home-$MARK/.cache/austenite/devtrees" |
| 202 | chmod 555 "$S/home-$MARK/.cache/austenite/devtrees" |
| 203 | stray() { # stray <script> |
| 204 | HOME="$S/home-$MARK" DEVTREES_INDEX=0 AUSTENITE_BIN="$AUS" TYPST_BIN="$TYPST" "$1" "$S/clean" main.typ 2>&1 |
| 205 | } |
| 206 | raw="$(mktemp -d "$S/home-$MARK/.cache/austenite/devtrees/run.XXXXXX" 2>&1)" |
| 207 | printf '%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" |
| 209 | r="$(stray "$DT")" |
| 210 | lack "stray: a tool's message that quotes a path is dropped" "$r" "$MARK" |
| 211 | expect "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. |
| 214 | prog="$(sed -n "/^WHITELIST='\$/,/'\$/p" "$DT" | sed "1d; \$s/'\$//")" |
| 215 | if [ -z "$prog" ]; then |
| 216 | fail "whitelist: the program was not found in devtrees.sh" |
| 217 | else |
| 218 | lookalikes="tree 0 main 1 typst exit 0 pages 1 error 0 warning 0 |
| 219 | tree 0 main 1 typst exit 0 pages 1 error 0 warning 0 $MARK |
| 220 | tree $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 |
| 222 | tree 0 main 1 typst exit 0 pages $MARK error 0 warning 0 |
| 223 | tree 0 main 1 pdf exit 0 pages 1 error 0 warning 0 |
| 224 | tree 0 main $MARK missing-main |
| 225 | tree 0 missing-root $MARK |
| 226 | devtrees: usage $MARK |
| 227 | devtrees: $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" |
| 233 | fi |
| 234 | |
| 235 | # ── 7. the plant ──────────────────────────────────────────────────────────── |
| 236 | mkdir -p "$S/plant" |
| 237 | cp "$HERE/gate_lib.sh" "$S/plant/" |
| 238 | sed 's/| awk "\$WHITELIST"$/| cat/' "$DT" > "$S/plant/devtrees.sh" |
| 239 | chmod +x "$S/plant/devtrees.sh" |
| 240 | if 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)" |
| 242 | else |
| 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 |
| 249 | fi |
| 250 | |
| 251 | echo "selftest: $bad failure(s)" |
| 252 | [ "$bad" -eq 0 ] |