AtlatestRepositorysigil-doc

sigil-doc / tree / testgate-sd-p2-rename.sh

1#!/usr/bin/env bash
2# SD-P2 / D13 rename-completeness arm, standalone so it can be sabotaged and
3# re-run on its own without a half-hour compile.
4#
5# This is an ABSENCE assertion, which is the cheapest kind of gate to get wrong:
6# it passes for free when the search is pointed at nothing, when the pattern is
7# subtly wrong, or when the tool is missing. So the arm proves three things
8# before it is allowed to mean anything:
9#
10# 1. the searcher exists at all;
11# 2. the file population it searches is non-empty and roughly the expected size;
12# 3. the searcher CAN see the old spelling, demonstrated against a planted
13# occurrence, immediately before asserting the old spelling is absent.
15# Without (3) this arm would report the rename complete on a machine where the
16# pattern had rotted, which is precisely how a partial rename ships looking fine.
17set -euo pipefail
19repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
20cd "$repo_dir"
21command -v rg >/dev/null || { echo "gate-sd-p2-rename: rg is missing" >&2; exit 1; }
22work="$(mktemp -d)"
23trap 'rm -rf "$work"' EXIT
25roots=(src test book docs tools)
26# This script and the sabotage harness both CONTAIN the old spelling, in their
27# own search pattern and their own planted-occurrence fixture. They are the only
28# three entries excluded, each of which MUST contain the old spelling by
29# construction, named here rather than matched by a wildcard, with the count
30# asserted so the list cannot be quietly widened into a hiding place:
32# - this script, which carries the old spelling in its own search pattern;
33# - the sabotage harness, which carries it in its planted defects;
34# - test/transcripts, the committed sabotage evidence, whose whole purpose is
35# to record runs in which the old spelling WAS present.
37# Excluding a directory is a bigger claim than excluding a file, so it is
38# audited below rather than trusted: everything under it must be a .txt
39# evidence record, so it can never become a hiding place for live source.
40excluded=(test/gate-sd-p2-rename.sh test/sabotage-sd-p2.sh test/transcripts)
41test "${#excluded[@]}" -eq 3 \
42 || { echo "gate-sd-p2-rename: exclusion list has grown; that is how a rename hides" >&2; exit 1; }
43non_evidence="$(rg --files test/transcripts | rg -v '\.txt$' || true)"
44test -z "$non_evidence" \
45 || { echo "gate-sd-p2-rename: non-evidence files under the excluded transcripts directory:" >&2
46 echo "$non_evidence" >&2
47 echo "the exclusion would hide live source; refusing" >&2; exit 1; }
48globs=()
49for path in "${excluded[@]}"; do globs+=(--glob "!$path" --glob "!$path/**"); done
51population="$(rg --files "${globs[@]}" "${roots[@]}" | wc -l | tr -d ' ')"
52test "$population" -gt 30 \
53 || { echo "gate-sd-p2-rename: search population is $population; refusing to assert an absence" >&2; exit 1; }
55# WHAT THIS ARM ACTUALLY CLAIMS: the ADMONITION is no longer spelled `note`.
56# NOT that the token `note` is absent - D13 renamed the admonition precisely so
57# that `@note` could become the FOLIO ROOT CONTAINER, so `@note` reappearing as a
58# root is the rename SUCCEEDING. SD-P1 and SD-P4 landed exactly that on master
59# (test/fixtures/sd-p1-folio-roots.sgd, test/fixtures/sd-p4-note-root.sgd,
60# test/test-root-profile.sgl, docs/root-profiles.md), and an arm asserting the
61# bare token would go red on 28 legitimate occurrences.
63# So the patterns below discriminate the admonition from the container:
65# - `make-admonition 'note` the admonition kind, unambiguous
66# - `(memq kind '(note` the model's kind allowlist. The bare `'(note`
67# pattern is NOT usable: master legitimately
68# carries `'(note project)` for root profiles,
69# which is a different vocabulary
70# - `(define (note ` the author binding
72# and in manuscripts only, `@note` NOT preceded by `@` (which would be an escaped
73# `@@note` inside `@code`) and NOT followed by `[` (a folio root always takes
74# ["id" ...]; an admonition never does).
76# `@end note` is deliberately NOT forbidden: the folio root and the admonition
77# share that delimiter, so it stopped being a discriminator the moment the
78# container landed.
79code_spelling="make-admonition 'note|\(memq kind '\(note|\(define \(note "
80manuscript_spelling='(^|[^@])@note([^\[]|$)'
82# The searcher must be shown to SEE each spelling before its absence means
83# anything, and each planted line must be seen by the pattern that owns it.
84printf '%s\n' "(make-admonition 'note #f children loc)" \
85 "(memq kind '(note tip warning))" \
86 "(define (note . body) ...)" >"$work/planted-code.txt"
87printf '%s\n' '@note' '@note{body}' >"$work/planted-manuscript.sgd"
88code_hits="$(rg -c -e "$code_spelling" "$work/planted-code.txt" || true)"
89test "$code_hits" = "3" \
90 || { echo "gate-sd-p2-rename: code searcher saw $code_hits/3 planted occurrences; it cannot be trusted to report an absence" >&2; exit 1; }
91manuscript_hits="$(rg -c -e "$manuscript_spelling" "$work/planted-manuscript.sgd" || true)"
92test "$manuscript_hits" = "2" \
93 || { echo "gate-sd-p2-rename: manuscript searcher saw $manuscript_hits/2 planted occurrences; it cannot be trusted to report an absence" >&2; exit 1; }
94# and it must NOT see the folio container or an escaped mention, or it would
95# report a false positive on legitimate SD-P1/SD-P4 vocabulary
96printf '%s\n' '@note["topics/alpha" title: "Alpha"]' '@code{@@note}' '@end note' \
97 >"$work/legitimate.sgd"
98if rg -q -e "$manuscript_spelling" "$work/legitimate.sgd"; then
99 echo "gate-sd-p2-rename: the manuscript pattern matches the folio root container; it would forbid what D13 was for" >&2
100 rg -n -e "$manuscript_spelling" "$work/legitimate.sgd" >&2
101 exit 1
102fi
104# THE ABSENCE ASSERTIONS THEMSELVES. Everything above only establishes that the
105# searchers work; these two are what actually inspect the tree. An earlier
106# revision of this script lost these blocks in an edit and still printed
107# RENAME_COMPLETE - green on its own branch AND green on the merged tree, both
108# meaningless, because the arm was asserting nothing. The rename sabotage legs
109# are what caught it.
110if rg -n "${globs[@]}" -e "$code_spelling" "${roots[@]}"; then
111 echo 'gate-sd-p2-rename: the admonition kind or binding is still spelled note above' >&2
112 exit 1
113fi
114if rg -n "${globs[@]}" --glob '*.sgd' -e "$manuscript_spelling" "${roots[@]}"; then
115 echo 'gate-sd-p2-rename: a manuscript still uses @note as an admonition above' >&2
116 exit 1
117fi
119# The renamed binding and kind must be PRESENT, not merely the old ones absent.
120rg -q "\(define \(aside " src/sigil/doc/author.sgl \
121 || { echo "gate-sd-p2-rename: author.sgl does not define aside" >&2; exit 1; }
122rg -q "memq kind '\(aside" src/sigil/doc/model.sgl \
123 || { echo "gate-sd-p2-rename: model.sgl kind allowlist does not carry aside" >&2; exit 1; }
125# The new spelling must actually be present, or an empty tree would pass.
126aside_files="$(rg -l "${globs[@]}" -e '@aside|@end aside|'"'"'aside' "${roots[@]}" | wc -l | tr -d ' ')"
127test "$aside_files" -ge 3 \
128 || { echo "gate-sd-p2-rename: expected the new spelling in at least 3 files, found $aside_files" >&2; exit 1; }
130echo "SD_P2_04_RENAME_COMPLETE population=$population planted=code-3/3+manuscript-2/2+container-not-matched aside_files=$aside_files excluded=${excluded[*]} transcripts-audited=evidence-only"