Oregami
Repositories/oxedyne/fe2o3

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

12.1 KiB, 25 runs, executable

created by r1870400018:69458, 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 the books' `./dev` script hands a tree to `austenite watch` when it should, and leaves the typst and
3# curated paths as they were.
4#
5# dev_dispatch.sh <dev> [<dev-orig>]
6#
7# <dev> the script under test
8# <dev-orig> the script it replaces; with it, the typst branch of `start_typst` is compared line for line
9#
10# Everything is a stub. `austenite` and `typst` are scripts that write their argument line to a log and make the
11# file or directory the real one would have made, then stay a moment as a watch does; `papers`, `evince` and `xdg-open` only note that they were
12# asked; and `python3` fails `import pikepdf`. The PATH holds the stubs and a symlink to each plain tool the
13# script uses, so `gs` is absent too: a hand-over with `--cmyk --scrub` that still runs proves the watch needs
14# neither. Trees are a few files under ~/.cache/austenite/dev_dispatch, made here and removed on exit. No book
15# is read or written, no real austenite or typst runs, and no viewer opens.
16#
17# Cases, each checked for the exit status, the austenite argument line, the typst argument line, whether a viewer
18# was asked for, and a message where one is due:
19# an austenite.jdat tree, with and without --cmyk, and with --scrub, which turns both metadata keys off over
20# whatever the settings file asks; USE_EVAL=1 with --cmyk, --grey, --scrub and --cmyk --scrub,
21# from the environment and from a dev.conf; USE_TYPST=1 in the environment and in a dev.conf, --typst, and
22# USE_TYPST=1 beside USE_EVAL=1; a binary whose usage has no watch command; a missing binary; the curated
23# path; and the Ghostscript and pikepdf refusals, which the curated path keeps. Then a book with its own
24# austenite.jdat below the tree root, reached through the relative dev symlink a book directory holds: run
25# in the book, run in a directory below it, with --cmyk, with USE_TYPST=1 in the environment, beside a
26# dev.conf that sets USE_TYPST=1 for every other directory of the tree, and in that other directory; and a
27# settings file above the tree root, which does not count.
28# Then the plant: the script with its USE_TYPST line removed, which the same cases must find red.
29set -u
30
31DEV="${1:-}"
32ORIG="${2:-}"
33[ -n "$DEV" ] && [ -f "$DEV" ] || { echo "usage: dev_dispatch.sh <dev> [<dev-orig>]"; exit 2; }
34DEV="$(cd "$(dirname "$DEV")" && pwd -P)/$(basename "$DEV")"
35if [ -n "$ORIG" ]; then
36 [ -f "$ORIG" ] || { echo "dev_dispatch: no such file: $ORIG"; exit 2; }
37 ORIG="$(cd "$(dirname "$ORIG")" && pwd -P)/$(basename "$ORIG")"
38fi
39
40BASE="$HOME/.cache/austenite/dev_dispatch"
41mkdir -p "$BASE" || exit 2
42S="$(mktemp -d "$BASE/run.XXXXXX")" || exit 2
43S="$(cd "$S" && pwd -P)"
44trap 'rm -rf "${S:?}"' EXIT
45
46BIN="$S/bin"; LOGS="$S/log"; TREES="$S/trees"
47mkdir -p "$BIN" "$LOGS" "$TREES" "$S/home"
48
49# A symlink to each plain tool the script uses, and nothing else.
50for t in bash dirname readlink grep sort timeout stat cp sleep md5sum cut head mkdir rm cat touch; do
51 p="$(type -P "$t")" || { echo "dev_dispatch: no $t on this machine"; exit 2; }
52 ln -s "$p" "$BIN/$t"
53done
54
55cat > "$BIN/austenite" <<'EOF'
56#!/bin/bash
57# No arguments: the usage, from the file the case names. Otherwise log the line and make what the real one makes.
58if [ $# -eq 0 ]; then cat "$STUB_USAGE"; exit 0; fi
59echo "$*" >> "$STUB_LOG/austenite.argv"
60if [ "$1" = "--watch" ]; then
61 out="${@: -1}"
62 mkdir -p "$out"
63 : > "$out/document.pdf"
64 sleep 3
65fi
66exit 0
67EOF
68cat > "$BIN/typst" <<'EOF'
69#!/bin/bash
70echo "$*" >> "$STUB_LOG/typst.argv"
71: > "${@: -1}"
72sleep 3
73exit 0
74EOF
75for v in papers evince xdg-open; do
76 printf '#!/bin/bash\necho %s >> "$STUB_LOG/viewer.log"\nexit 0\n' "$v" > "$BIN/$v"
77done
78printf '#!/bin/bash\nexit 1\n' > "$BIN/python3"
79chmod +x "$BIN"/austenite "$BIN"/typst "$BIN"/papers "$BIN"/evince "$BIN"/xdg-open "$BIN"/python3
80
81echo "Usage: austenite watch|build ... or austenite [--watch] [--eval] <SOURCE.typ> [OUTPUT_DIR]" > "$S/usage_new"
82echo "Usage: austenite [--watch] [--eval] <SOURCE.typ> [OUTPUT_DIR]" > "$S/usage_old"
83
84# mk_tree <name> [jdat] [conf=<text>]: a document and what stands beside it. `.root` marks the tree root,
85# where `go` puts the link to the script under test.
86mk_tree() {
87 local name="$1"; shift
88 mkdir -p "$TREES/$name"
89 : > "$TREES/$name/.root"
90 printf '= Title\n\nBody.\n' > "$TREES/$name/x.typ"
91 local a
92 for a in "$@"; do
93 case "$a" in
94 jdat) echo '{"document": "x.typ"}' > "$TREES/$name/austenite.jdat" ;;
95 conf=*) printf '%s\n' "${a#conf=}" > "$TREES/$name/dev.conf" ;;
96 esac
97 done
98}
99
100# mk_sub <tree> <sub> [jdat]: a directory below a tree root with a document, its own dev linked relatively
101# to the one above it as a book directory's is, and optionally its own settings file.
102mk_sub() {
103 local d="$TREES/$1/$2"
104 mkdir -p "$d"
105 printf '= Title\n\nBody.\n' > "$d/x.typ"
106 ln -sfn ../dev "$d/dev"
107 [ "${3:-}" = "jdat" ] && echo '{"document": "x.typ"}' > "$d/austenite.jdat"
108 return 0
109}
110
111FAILS=0
112CASES=0
113
114# go <dev> <name> <tree> <usage> [VAR=value...] -- [flags...]: run the script once, leaving $OUT and $RC.
115go() {
116 local dev="$1" name="$2" tree="$3" usage="$4"; shift 4
117 local envs=()
118 while [ $# -gt 0 ] && [ "$1" != "--" ]; do envs+=("$1"); shift; done
119 [ $# -gt 0 ] && shift
120 local log="$LOGS/$name"
121 rm -rf "$log"; mkdir -p "$log"
122 local t="$TREES/$tree"
123 # Each run starts from the tree as made, without the last run's outputs.
124 rm -rf "$t/.x.aus-out" "$t/x.pdf" "$t/.x.source.pdf" "$t/.x.raw.pdf"
125 local r="$t"
126 while [ ! -f "$r/.root" ]; do r="$(dirname "$r")"; done
127 ln -sfn "$dev" "$r/dev"
128 OUT="$(cd "$t" && timeout 30 /usr/bin/env -i PATH="$BIN" HOME="$S/home" \
129 AUSTENITE_BIN="$BIN/austenite" STUB_LOG="$log" STUB_USAGE="$S/$usage" \
130 "${envs[@]}" ./dev x.typ "$@" 2>&1)"
131 RC=$?
132}
133
134# expect <name> <rc> <aus-line|-> <typ-line|-> <viewer yes|no> <message|->: judge the last run.
135expect() {
136 local name="$1" rc="$2" aus="$3" typ="$4" view="$5" msg="$6"
137 local log="$LOGS/$name" why="" got
138 CASES=$((CASES + 1))
139 [ "$RC" = "$rc" ] || why="$why exit $RC not $rc;"
140 got="$(cat "$log/austenite.argv" 2>/dev/null)"; [ -f "$log/austenite.argv" ] || got="-"
141 [ "$got" = "$aus" ] || why="$why austenite got [$got] not [$aus];"
142 got="$(cat "$log/typst.argv" 2>/dev/null)"; [ -f "$log/typst.argv" ] || got="-"
143 [ "$got" = "$typ" ] || why="$why typst got [$got] not [$typ];"
144 if [ "$view" = "yes" ]; then
145 [ -f "$log/viewer.log" ] || why="$why no viewer asked for;"
146 else
147 [ ! -f "$log/viewer.log" ] || why="$why a viewer was asked for;"
148 fi
149 if [ "$msg" != "-" ]; then
150 case "$OUT" in *"$msg"*) ;; *) why="$why output lacks [$msg];" ;; esac
151 fi
152 if [ -z "$why" ]; then echo "ok $name"; else echo "FAIL $name:$why"; FAILS=$((FAILS + 1)); fi
153}
154
155# run_cases <dev>: every case against one copy of the script.
156run_cases() {
157 local dev="$1" T
158 rm -rf "$TREES"; mkdir -p "$TREES"
159 mk_tree jdat jdat
160 mk_tree evalbare
161 mk_tree evalconf conf='DEV_FLAGS="--cmyk --scrub"
162USE_EVAL=1'
163 mk_tree typstconf conf='USE_TYPST=1'
164 mk_tree plain
165 T="$(cd "$TREES" && pwd -P)"
166 local tf="watch --root $T/typstconf --font-path $T/typstconf/assets/fonts x.typ x.pdf"
167 local jf="watch --root $T/jdat --font-path $T/jdat/assets/fonts x.typ x.pdf"
168 local ef="watch --root $T/evalbare --font-path $T/evalbare/assets/fonts x.typ x.pdf"
169 local rf="--set root=$T/evalbare --set fonts=$T/evalbare/assets/fonts"
170 local sc="--set metadata.document=false --set metadata.engine=false"
171
172 go "$dev" jdat_plain jdat usage_new --
173 expect jdat_plain 0 "watch x.typ" - no "dev:"
174 go "$dev" jdat_cmyk jdat usage_new -- --cmyk
175 expect jdat_cmyk 0 "watch x.typ --set colour.space=cmyk" - no -
176 go "$dev" jdat_scrub jdat usage_new -- --scrub
177 expect jdat_scrub 0 "watch x.typ $sc" - no -
178 go "$dev" eval_cmyk evalbare usage_new USE_EVAL=1 -- --cmyk
179 expect eval_cmyk 0 "watch x.typ --set colour.space=cmyk $rf" - no -
180 go "$dev" eval_grey evalbare usage_new USE_EVAL=1 -- --grey
181 expect eval_grey 0 "watch x.typ --set colour.space=grey $rf" - no -
182 go "$dev" eval_cmyk_scrub evalbare usage_new USE_EVAL=1 -- --cmyk --scrub
183 expect eval_cmyk_scrub 0 "watch x.typ --set colour.space=cmyk $sc $rf" - no -
184 go "$dev" eval_scrub evalbare usage_new USE_EVAL=1 -- --scrub
185 expect eval_scrub 0 "watch x.typ $sc $rf" - no -
186 go "$dev" eval_conf evalconf usage_new --
187 expect eval_conf 0 "watch x.typ --set colour.space=cmyk $sc --set root=$T/evalconf --set fonts=$T/evalconf/assets/fonts" - no -
188 go "$dev" typst_env jdat usage_new USE_TYPST=1 --
189 expect typst_env 0 - "$jf" yes -
190 go "$dev" typst_conf typstconf usage_new --
191 expect typst_conf 0 - "$tf" yes -
192 go "$dev" typst_flag jdat usage_new -- --typst
193 expect typst_flag 0 - "$jf" yes -
194 go "$dev" typst_beats_eval evalbare usage_new USE_EVAL=1 USE_TYPST=1 --
195 expect typst_beats_eval 0 - "$ef" yes -
196 go "$dev" old_binary evalbare usage_old USE_EVAL=1 --
197 expect old_binary 0 - "$ef" yes "has no watch command"
198 go "$dev" no_binary jdat usage_new AUSTENITE_BIN="$S/none" --
199 expect no_binary 0 - "$jf" yes "austenite not found"
200 go "$dev" curated plain usage_new --
201 expect curated 0 "--watch x.typ .x.aus-out" - yes -
202 go "$dev" curated_cmyk plain usage_new -- --cmyk
203 expect curated_cmyk 1 - - no "needs Ghostscript"
204 go "$dev" curated_scrub plain usage_new -- --scrub
205 expect curated_scrub 1 - - no "needs pikepdf"
206
207 # A book with its own settings file below the tree root, as the Invitation has.
208 mk_tree shelf conf='case "$PWD" in
209 */inv|*/inv/*) ;;
210 *) USE_TYPST=1 ;;
211esac'
212 mk_sub shelf inv jdat
213 mk_sub shelf inv/part
214 mk_sub shelf other
215 mk_tree bare_shelf
216 mk_sub bare_shelf inv jdat
217 mkdir -p "$TREES/outer"
218 echo '{"document": "x.typ"}' > "$TREES/outer/austenite.jdat"
219 mk_tree outer/inner
220 local sf="watch --root $T/shelf --font-path $T/shelf/assets/fonts x.typ x.pdf"
221 local bf="watch --root $T/bare_shelf --font-path $T/bare_shelf/assets/fonts x.typ x.pdf"
222
223 go "$dev" book_jdat shelf/inv usage_new --
224 expect book_jdat 0 "watch x.typ" - no "dev:"
225 go "$dev" book_jdat_below shelf/inv/part usage_new --
226 expect book_jdat_below 0 "watch x.typ" - no -
227 go "$dev" book_jdat_cmyk bare_shelf/inv usage_new -- --cmyk
228 expect book_jdat_cmyk 0 "watch x.typ --set colour.space=cmyk" - no -
229 go "$dev" book_jdat_eval bare_shelf/inv usage_new USE_EVAL=1 --
230 expect book_jdat_eval 0 "watch x.typ" - no -
231 go "$dev" book_typst_env bare_shelf/inv usage_new USE_TYPST=1 --
232 expect book_typst_env 0 - "$bf" yes -
233 go "$dev" book_typst_flag bare_shelf/inv usage_new -- --typst
234 expect book_typst_flag 0 - "$bf" yes -
235 go "$dev" book_other_typst shelf/other usage_new --
236 expect book_other_typst 0 - "$sf" yes -
237 go "$dev" above_root outer/inner usage_new --
238 expect above_root 0 "--watch x.typ .x.aus-out" - yes -
239}
240
241# The typst branch of start_typst, from its opening line to the first four-space fi.
242typst_branch() {
243 awk '/^start_typst\(\) \{/ { f = 1 } f { print } f && /^ fi$/ { exit }' "$1"
244}
245
246echo "== the script"
247run_cases "$DEV"
248REAL_FAILS=$FAILS
249
250if [ -n "$ORIG" ]; then
251 CASES=$((CASES + 1))
252 a="$(typst_branch "$DEV")"
253 b="$(typst_branch "$ORIG")"
254 if [ -n "$a" ] && [ "$a" = "$b" ]; then
255 echo "ok typst_branch_unchanged ($(printf '%s\n' "$a" | wc -l) lines)"
256 else
257 echo "FAIL typst_branch_unchanged: the typst branch of start_typst differs from $ORIG"
258 REAL_FAILS=$((REAL_FAILS + 1))
259 fi
260fi
261
262echo "== the plant: the USE_TYPST line removed"
263PLANTED="$S/dev.planted"
264grep -vxF '[ "${USE_TYPST:-}" = "1" ] && engine="typst"' "$DEV" > "$PLANTED"
265if cmp -s "$DEV" "$PLANTED"; then
266 echo "FAIL plant: no USE_TYPST line found to remove"
267 exit 1
268fi
269chmod +x "$PLANTED"
270REAL_CASES=$CASES
271FAILS=0
272run_cases "$PLANTED" > "$S/plant.out"
273grep -v '^ok ' "$S/plant.out"
274PLANT_FAILS=$FAILS
275
276echo "== summary"
277echo "script: $REAL_FAILS failed of $REAL_CASES cases"
278echo "plant: $PLANT_FAILS cases red"
279if [ "$REAL_FAILS" -ne 0 ]; then echo "dev_dispatch: RED"; exit 1; fi
280if [ "$PLANT_FAILS" -eq 0 ]; then echo "dev_dispatch: the plant stayed green, so the test proves nothing"; exit 1; fi
281echo "dev_dispatch: green, and the plant is caught"