AtlatestRepositorysigil-doc

sigil-doc / tree / testgate-d2.sh

1#!/usr/bin/env bash
2set -euo pipefail
3repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
4cd "$repo_dir"
5mkdir -p "$repo_dir/build"
6sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
7redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"
8cache_dir=$(mktemp -d "$repo_dir/build/d2-cache.XXXXXX")
9export SIGIL_REDIRECTS="$redirects"
11mkdir -p "$cache_dir/real"
12# Subject-existence pins: `if rg` reads a missing file (exit 2) the same as a
13# clean one (exit 1); a renamed semantic core would disarm this sweep silently.
14test -f src/sigil/doc/model.sgl
15test -f src/sigil/doc/traversal.sgl
16if rg -n -i '(html|sxml|css|typst|latex|slate|class=|tag:)' src/sigil/doc/model.sgl src/sigil/doc/traversal.sgl; then
17 echo "backend vocabulary leaked into semantic core" >&2; exit 1
18fi
19future="$(find src -type f | rg '/(slate|pdf)\.sgl$' || true)"
20test -z "$future"
21test "$(find src -type f | rg '/html\.sgl$' | wc -l)" -eq 1
22test "$(find src -type f | rg '/typst\.sgl$' | wc -l)" -eq 1
23export XDG_CACHE_HOME="$cache_dir/real"
25# Compile the package and run the focused contract first. The real-language
26# controls below then exercise the public #lang path against those exact bytes.
27setsid -w "$sigil" -L src -L test test test/test-reader.sgl --report
29observed=$(setsid -w "$sigil" -L src -L test eval '(begin (import (sigil doc)) (load "test/fixtures/practical-sigil-chapter.sgd") (display (document-title (eval (quote document)))) (display "|") (display (eval (quote tip-calls))) (display "|") (display (document->plain (eval (quote document)))))')
30case "$observed" in
31 *"A Useful First Script|2|"*"macro-expanded phrase"*"computed inline expression"*"Computed Section 2"*) ;;
32 *) echo "D2 authoring positive control failed" >&2; exit 1 ;;
33esac
34reader_sgb=$(find "$cache_dir/real" -path '*/sigil/doc/reader.sgb' -print -quit)
35test -n "$reader_sgb"
36# POSITIVE CONTROLS FOR THE ABSENCE ASSERTION BELOW -- RUN FIRST, DELIBERATELY.
37# Until they pass, the assertion they guard means nothing, so they do not run
38# after it.
40# `test "$before" = "$after"` below claims that loading the manuscript through
41# the public #lang path did NOT recompile the reader. That is an assertion that
42# NOTHING happened, and it is satisfied for free by anything that stops the
43# `eval` from running at all.
45# It was reachable, not hypothetical. Until 2026-08-22 that line was a bare
46# `setsid`, which forks and returns 0 at once whenever the setsid process is a
47# process-group leader -- which is what bash makes it at an interactive prompt,
48# and by hand at a prompt is the only way anything in this repo is run.
50# WHAT WAS MEASURED, STATED PRECISELY SO NOBODY READS IT AS "THIS GATE IS RED":
51# the measurement was of this ARM'S SHAPE -- stat, run a child that rewrites the
52# artifact, stat again -- with a synthetic 3-second child, under `bash -i` on a
53# pty. The bare form reported GREEN for an artifact the child demonstrably HAD
54# rewritten; `-w` reported RED. It says the shape can produce a false green. It
55# says nothing about whether the real manuscript recompiles the real reader, and
56# the assertion below is expected to hold.
58# `-w` closes the race. These two controls close the other half, and they assert
59# DIFFERENT things:
61# (a) A REBUILD LANDS AT THIS EXACT PATH. Without it, a recompile that wrote
62# somewhere else would leave `before = after` true while the very thing the
63# arm rules out had happened.
64# (b) THE `stat` PROBE SEES A REWRITE IN PLACE. That is the actual failure mode
65# -- an existing artifact overwritten, not a deleted one recreated -- and
66# (a) alone cannot show it, because a file that was removed and recreated
67# changes its stat triple by construction the moment it exists again.
69# Both work in a THROWAWAY COPY of the cache, so the working tree is untouched
70# and no restore step has to be reached. The copy is removed afterwards: it is
71# under `build/` and therefore invisible to git, which is not the same as free.
72control_cache="$cache_dir/control"
73cp -a "$cache_dir/real" "$control_cache"
74control_sgb=$(find "$control_cache" -path '*/sigil/doc/reader.sgb' -print -quit)
75test -n "$control_sgb" || { echo "positive control: no reader.sgb in the copied cache" >&2; exit 1; }
77# (b) first, because it needs no compiler and a failure here invalidates (a) too.
78inplace_before=$(stat -c '%y:%s:%i' "$control_sgb")
79printf '\0' >>"$control_sgb"
80inplace_after=$(stat -c '%y:%s:%i' "$control_sgb")
81test "$inplace_before" != "$inplace_after" || {
82 echo "POSITIVE CONTROL (b) FAILED: the stat probe cannot see an in-place rewrite." >&2
83 echo " before: $inplace_before" >&2
84 echo " after: $inplace_after" >&2
85 echo " The 'before = after' assertion below therefore proves nothing." >&2; exit 1; }
87# (a) now: delete the compiled reader from the copy and re-run the same
88# manuscript against that cache. It must be written back to the same path.
89rm -f "$control_sgb"
90XDG_CACHE_HOME="$control_cache" setsid -w "$sigil" -L src -L test \
91 eval -f test/fixtures/practical-sigil-chapter.sgd >"$cache_dir/control.log" 2>&1 || {
92 echo "positive control (a): the forced-recompile run failed" >&2
93 tail -30 "$cache_dir/control.log" >&2; exit 1; }
94test -f "$control_sgb" || {
95 echo "POSITIVE CONTROL (a) FAILED: a recompile did not land at the path this arm watches." >&2
96 echo " expected: $control_sgb" >&2
97 echo " A rebuild could therefore happen without moving 'before'/'after' at all." >&2; exit 1; }
98rm -rf "$control_cache"
99echo "D2 cache-reuse probe armed: in-place rewrite visible to stat ($inplace_before -> $inplace_after); a rebuild lands at the watched path"
101# THE ASSERTION ITSELF, now that both halves of the probe have been shown to fire.
102before=$(stat -c '%y:%s:%i' "$reader_sgb")
103setsid -w "$sigil" -L src -L test eval -f test/fixtures/practical-sigil-chapter.sgd
104after=$(stat -c '%y:%s:%i' "$reader_sgb")
105test "$before" = "$after"
107echo "D2_REAL_LANGUAGE_GREEN"