omtex
aien-dev/omtex
Brought by drakestapleton
| 1 | #!/bin/sh |
| 2 | # tests/run.sh -- omtex test suite. Every refusal case is paired with a minimally different accepted |
| 3 | # case, so a refusal cannot pass by the library refusing everything. Prints one line per case and a |
| 4 | # summary; exits 1 on any FAIL. Cases that cannot run on this host print NOT_RUN (never PASS). |
| 5 | # Env: OMC (pinned omc; fetched by tools/pin.sh when absent), OMTEX_NICE as in tools/build.sh. |
| 6 | set -u |
| 7 | here=$(cd "$(dirname "$0")/.." && pwd) |
| 8 | . "$here/tools/pin.sh" |
| 9 | nice=${OMTEX_NICE-nice -n 19 ionice -c3} |
| 10 | ( omtex_check_omc ) || { echo "FAIL pin: omc missing or not the pinned build"; exit 1; } |
| 11 | T=$(mktemp -d "${TMPDIR:-/tmp}/omtex-test.XXXXXX") |
| 12 | trap '[ -n "${OMTEX_KEEP:-}" ] || rm -rf "$T"' EXIT |
| 13 | pass=0 fail=0 notrun=0 |
| 14 | ok() { pass=$((pass + 1)); echo "PASS $1"; } |
| 15 | bad() { fail=$((fail + 1)); echo "FAIL $1: $2"; } |
| 16 | nr() { notrun=$((notrun + 1)); echo "NOT_RUN $1: $2"; } |
| 17 | |
| 18 | # doc NAME: stdin holds "text" lines, a line "---", then body words; wrapped in a minimal document. |
| 19 | doc() { |
| 20 | { echo 'text zzcls "article"'; echo 'text zzopt "11pt"' |
| 21 | sed '/^---$/,$d' |
| 22 | echo 'fn main to int'; echo 'docclass zzopt zzcls'; echo 'begindoc' |
| 23 | sed -n '/^---$/,$p' "$T/$1.in" | sed 1d |
| 24 | echo 'enddoc'; echo 'ret 0'; echo 'end'; } < "$T/$1.in" > "$T/$1.om" |
| 25 | } |
| 26 | # gen NAME [ARGS]: compile lib+case, run the generator; sets rc, output in $T/NAME.out / .err. |
| 27 | gen() { |
| 28 | n=$1; shift |
| 29 | if ! cat "$here/lib/omtex.om" "$T/$n.om" | $nice "$OMC" > "$T/$n.gen" 2> "$T/$n.cerr"; then |
| 30 | rc=compile; return |
| 31 | fi |
| 32 | chmod +x "$T/$n.gen" |
| 33 | "$T/$n.gen" "$@" > "$T/$n.out" 2> "$T/$n.err"; rc=$? |
| 34 | } |
| 35 | # expect NAME RC [FIXED-STRING...]: case built by doc(); generator exit must be RC and stdout must |
| 36 | # contain each string. |
| 37 | expect() { |
| 38 | n=$1 want=$2; shift 2 |
| 39 | doc "$n"; gen "$n" |
| 40 | if [ "$rc" != "$want" ]; then bad "$n" "rc=$rc want=$want $(cat "$T/$n.err" "$T/$n.cerr" 2>/dev/null | head -2)"; return; fi |
| 41 | for s in "$@"; do |
| 42 | grep -qF -- "$s" "$T/$n.out" || { bad "$n" "output lacks: $s"; return; } |
| 43 | done |
| 44 | if [ "$want" = 111 ]; then |
| 45 | grep -q '^omtex: refused element [0-9]*: ' "$T/$n.err" || { bad "$n" "no refusal message"; return; } |
| 46 | fi |
| 47 | ok "$n" |
| 48 | } |
| 49 | case_() { cat > "$T/$1.in"; } |
| 50 | |
| 51 | # ---- escaping (prose) -------------------------------------------------------------------------- |
| 52 | case_ esc <<'EOF' |
| 53 | text p "50% & a_b #1 {x} ^ < > | tie~here, tilde \\~ and backslash \\\\ done" |
| 54 | --- |
| 55 | para p |
| 56 | EOF |
| 57 | expect esc 0 '50\% \& a\_b \#1 \{x\} \textasciicircum{} \textless{} \textgreater{} \textbar{} tie~here, tilde \textasciitilde{} and backslash \textbackslash{} done' |
| 58 | |
| 59 | # Leak detector: a TeX special preceded by anything but a backslash. Negative control: the detector |
| 60 | # must fire on the raw prose, otherwise it proves nothing about the escaped output. |
| 61 | leak() { grep -E '(^|[^\\])[%&_#]' "$1" > /dev/null; } |
| 62 | grep '^text p ' "$T/esc.in" > "$T/esc.raw" |
| 63 | if ! leak "$T/esc.raw"; then bad esc-leak-control "detector does not fire on raw % & _ #" |
| 64 | elif sed -n '/begin{document}/,/end{document}/p' "$T/esc.out" > "$T/esc.body" && leak "$T/esc.body"; then |
| 65 | bad esc-leak "unescaped special in output: $(grep -E '(^|[^\\])[%&_#]' "$T/esc.body" | head -1)" |
| 66 | else ok esc-leak; fi |
| 67 | |
| 68 | case_ markup <<'EOF' |
| 69 | text p "*em* **bold** `a_b{c}` $x^2$ \"quoted\" [@k1,k2] [#sec:a] \\* \\$ \\[x\\]" |
| 70 | --- |
| 71 | para p |
| 72 | EOF |
| 73 | expect markup 0 '\emph{em} \textbf{bold} \texttt{a\_b\{c\}} $x^2$ ``quoted'"''"' ~\cite{k1,k2} ~\ref{sec:a} * \$ {[}x{]}' |
| 74 | |
| 75 | case_ utf8 <<'EOF' |
| 76 | text p "Gödel – π" |
| 77 | --- |
| 78 | para p |
| 79 | EOF |
| 80 | expect utf8 0 'Gödel – π' |
| 81 | |
| 82 | # ---- refusal pairs: (accepted, refused) ---------------------------------------------------------- |
| 83 | pair() { # pair NAME BODY-WORD GOOD-TEXT BAD-TEXT |
| 84 | printf 'text v "%s"\n---\n%s v\n' "$3" "$2" > "$T/$1-ok.in" |
| 85 | printf 'text v "%s"\n---\n%s v\n' "$4" "$2" > "$T/$1-refused.in" |
| 86 | expect "$1-ok" 0; expect "$1-refused" 111 |
| 87 | } |
| 88 | pair backslash para 'C:\\\\temp' 'C:\\temp' |
| 89 | pair emph para 'F\\* and *x*' 'F* and x' |
| 90 | pair emph-nest para '**a *b* c**' '**a *b** c*' |
| 91 | pair quote para '\"hi\" she said' '\"hi she said' |
| 92 | pair code para '`x`' '`x' |
| 93 | pair math-pct para '$50\\%$' '$50%$' |
| 94 | pair math-dollar para '$a$ and $b$' '$a$ and $b' |
| 95 | pair math-brace para '$x^{2}$' '$x^{2$' |
| 96 | pair math-input para '$\\mathrm{input}$' '$\\input{/etc/passwd}$' |
| 97 | pair math-csname para '$\\alpha$' '$\\csname foo\\endcsname$' |
| 98 | pair disp-def equation 'e^{i\\pi}' '\\def\\x{1}\\x' |
| 99 | pair disp-write displaymath 'a = b' '\\immediate\\write18{id}' |
| 100 | pair amp-outside para '$a \\le b$' '$a & b$' |
| 101 | pair cite-key para '[@shannon48]' '[@bad key]' |
| 102 | pair ref-key para '[#sec:a]' '[#sec a]' |
| 103 | # labels go through sectionl, which takes two texts |
| 104 | printf 'text s "Intro"\ntext v "sec:intro"\n---\nsectionl s v\n' > "$T/label-ok.in" |
| 105 | printf 'text s "Intro"\ntext v "sec:intro}\\\\input{x}"\n---\nsectionl s v\n' > "$T/label-refused.in" |
| 106 | expect label-ok 0 '\section{Intro}\label{sec:intro}'; expect label-refused 111 |
| 107 | printf 'text o "leftmargin=*"\ntext i "a"\n---\nitemizeo o\nitem i\nendlist\n' > "$T/opt-ok.in" |
| 108 | printf 'text o "leftmargin=*]\\\\input{x}"\ntext i "a"\n---\nitemizeo o\nitem i\nendlist\n' > "$T/opt-refused.in" |
| 109 | expect opt-ok 0 '\begin{itemize}[leftmargin=*]'; expect opt-refused 111 |
| 110 | printf 'text i "a"\n---\nitemize\nitem i\nendlist\n' > "$T/list-ok.in" |
| 111 | printf 'text i "a"\n---\nitemize\nitem i\n' > "$T/list-refused.in" |
| 112 | expect list-ok 0; expect list-refused 111 |
| 113 | printf 'text i "a"\n---\nitemize\nitem i\nendlist\n' > "$T/item-ok.in" |
| 114 | printf 'text i "a"\n---\nitem i\n' > "$T/item-refused.in" |
| 115 | expect item-ok 0; expect item-refused 111 |
| 116 | printf 'text sh "sh"\ntext c "c"\ntext l "lst:x"\ntext b "echo end{lstlisting}\\n"\n---\ncodelang sh\nlisting c l b\n' > "$T/lst-ok.in" |
| 117 | printf 'text sh "sh"\ntext c "c"\ntext l "lst:x"\ntext b "echo \\\\end{lstlisting}\\n"\n---\ncodelang sh\nlisting c l b\n' > "$T/lst-refused.in" |
| 118 | expect lst-ok 0; expect lst-refused 111 |
| 119 | printf 'text c "c"\ntext l "lst:x"\ntext b "x\\n"\ntext sh "sh"\n---\ncodelang sh\nlisting c l b\n' > "$T/lang-ok.in" |
| 120 | printf 'text c "c"\ntext l "lst:x"\ntext b "x\\n"\n---\nlisting c l b\n' > "$T/lang-refused.in" |
| 121 | expect lang-ok 0; expect lang-refused 111 |
| 122 | |
| 123 | # phase order: a body word before begindoc, a preamble word after it |
| 124 | cat > "$T/order-ok.om" <<'EOF' |
| 125 | text c "article" |
| 126 | text o "11pt" |
| 127 | text p "amsmath" |
| 128 | text v "x" |
| 129 | fn main to int |
| 130 | docclass o c |
| 131 | usepkg p |
| 132 | begindoc |
| 133 | para v |
| 134 | enddoc |
| 135 | ret 0 |
| 136 | end |
| 137 | EOF |
| 138 | sed 's/^usepkg p$/para v/' "$T/order-ok.om" > "$T/order-refused.om" |
| 139 | for n in order-ok order-refused; do |
| 140 | gen "$n"; want=0; [ "$n" = order-refused ] && want=111 |
| 141 | [ "$rc" = "$want" ] && ok "$n" || bad "$n" "rc=$rc want=$want" |
| 142 | done |
| 143 | cp "$T/order-ok.om" "$T/order-late.om" |
| 144 | sed -i 's/^para v$/usepkg p/' "$T/order-late.om" |
| 145 | gen order-late; [ "$rc" = 111 ] && ok order-late-preamble || bad order-late-preamble "rc=$rc want=111" |
| 146 | |
| 147 | # ---- listings check mode ------------------------------------------------------------------------ |
| 148 | lcase() { # lcase NAME REFUSE-RC|- CODE (CODE uses \n, the Omega string escape) |
| 149 | { printf 'text c "c"\ntext l "lst:t"\ntext b "%s"\n---\n' "$3" |
| 150 | [ "$2" = - ] || echo "omrefuse $2" |
| 151 | echo 'omlisting c l b'; } > "$T/$1.in" |
| 152 | doc "$1" |
| 153 | # omlang belongs to the preamble |
| 154 | sed -i 's/^begindoc$/omlang\nbegindoc/' "$T/$1.om" |
| 155 | } |
| 156 | lcheck() { # lcheck NAME WANT-RC WANT-LINE |
| 157 | mkdir -p "$T/$1.d"; gen "$1" check "$OMC" "$T/$1.d" |
| 158 | if [ "$rc" != "$2" ]; then bad "$1" "rc=$rc want=$2 $(cat "$T/$1.err" | head -1)"; return; fi |
| 159 | grep -qF -- "$3" "$T/$1.out" || { bad "$1" "no line: $3"; return; } |
| 160 | ok "$1" |
| 161 | } |
| 162 | lcase lst-compiles - 'fn main to int\nret 0\nend\n' |
| 163 | lcheck lst-compiles 0 'LISTING lst:t want=0 got=0 PASS' |
| 164 | lcase lst-broken - 'fn main to int\nret 0\nret 1\nend\n' |
| 165 | lcheck lst-broken 113 'LISTING lst:t want=0 got=17 FAIL' |
| 166 | lcase lst-refusal-ok 17 'fn main to int\nret 0\nret 1\nend\n' |
| 167 | lcheck lst-refusal-ok 0 'LISTING lst:t want=17 got=17 PASS' |
| 168 | lcase lst-refusal-wrong 16 'fn main to int\nret 0\nret 1\nend\n' |
| 169 | lcheck lst-refusal-wrong 113 'LISTING lst:t want=16 got=17 FAIL' |
| 170 | lcase lst-refusal-compiles 17 'fn main to int\nret 0\nend\n' |
| 171 | lcheck lst-refusal-compiles 113 'LISTING lst:t want=17 got=0 FAIL' |
| 172 | |
| 173 | # ---- determinism -------------------------------------------------------------------------------- |
| 174 | gen esc; cp "$T/esc.out" "$T/esc.1"; gen esc |
| 175 | cmp -s "$T/esc.1" "$T/esc.out" && ok determinism-tex || bad determinism-tex "two runs differ" |
| 176 | if command -v pdflatex > /dev/null; then |
| 177 | ex=$here/examples/listings/paper.om |
| 178 | ( cd "$here" && SOURCE_DATE_EPOCH=1700000000 tools/build.sh "$ex" "$T/b1" ) > "$T/b1.log" 2>&1 \ |
| 179 | && ( cd "$here" && SOURCE_DATE_EPOCH=1700000000 tools/build.sh "$ex" "$T/b2" ) > "$T/b2.log" 2>&1 |
| 180 | if [ $? -ne 0 ]; then bad determinism-pdf "build failed: $(tail -2 "$T/b1.log" "$T/b2.log")" |
| 181 | elif cmp -s "$T/b1/paper.tex" "$T/b2/paper.tex" && cmp -s "$T/b1/paper.pdf" "$T/b2/paper.pdf"; then ok determinism-pdf |
| 182 | else bad determinism-pdf "tex or pdf differ between two builds"; fi |
| 183 | else nr determinism-pdf "pdflatex not installed"; fi |
| 184 | |
| 185 | # ---- keyword parity with the pinned compiler ---------------------------------------------------- |
| 186 | kw=$(dirname "$OMC")/../omc/genkw.awk |
| 187 | om2='rotr rotl unroll inline fresh abstract seal open holds layout field at little big part out ops export requires ensures invariant result old om2 ir' |
| 188 | if [ -r "$kw" ]; then |
| 189 | # note is the line-comment word: it is highlighted as a comment (morecomment), not a keyword |
| 190 | sed -n 's/.*n=split("\([^"]*\)".*/\1/p' "$kw" | tr ' ' '\n' | cut -d: -f1 | grep -E '^[a-z0-9]+$' | grep -vx note > "$T/kw.pin" |
| 191 | grep -qF 'morecomment=[l]{note}' "$here/lib/omtex.om" || bad keyword-note "note is not the listings comment" |
| 192 | for w in $om2; do echo "$w"; done >> "$T/kw.pin" |
| 193 | sort -u -o "$T/kw.pin" "$T/kw.pin" |
| 194 | grep '^text zlang[2-5] ' "$here/lib/omtex.om" | sed 's/.*{\([^}]*\)}.*/\1/' | tr ',' '\n' | sort -u > "$T/kw.lib" |
| 195 | if cmp -s "$T/kw.pin" "$T/kw.lib"; then ok keyword-parity |
| 196 | else bad keyword-parity "$(diff "$T/kw.pin" "$T/kw.lib" | grep '^[<>]' | tr '\n' ' ')"; fi |
| 197 | else nr keyword-parity "no genkw.awk next to $OMC"; fi |
| 198 | |
| 199 | # ---- pin refusal -------------------------------------------------------------------------------- |
| 200 | cp "$OMC" "$T/omc.other"; printf 'x' >> "$T/omc.other" |
| 201 | if ( OMC=$T/omc.other; omtex_check_omc ) 2> /dev/null; then bad pin-refusal "a modified omc was accepted" |
| 202 | else ok pin-refusal; fi |
| 203 | |
| 204 | # ---- zenodo-draft never publishes (offline: the refusal happens before any request) ------------ |
| 205 | printf '%%PDF-1.4\n' > "$T/z.pdf" |
| 206 | ZENODO_TOKEN=offline-test sh "$here/tools/zenodo-draft.sh" newversion '1/actions/publish#' "$T/z.pdf" > /dev/null 2> "$T/z.err" |
| 207 | zrc=$? |
| 208 | if [ "$zrc" = 3 ] && grep -q 'never publishes' "$T/z.err"; then ok zenodo-publish-refusal |
| 209 | else bad zenodo-publish-refusal "rc=$zrc $(head -1 "$T/z.err")"; fi |
| 210 | |
| 211 | echo "TESTS pass=$pass fail=$fail not_run=$notrun" |
| 212 | [ "$fail" = 0 ] |