#!/usr/bin/env bash # # check-wai-quantum-names-selftest.sh — check-wai-quantum-names.py can fail. # # A check that cannot fail is decoration. This builds a scratch repository shaped like # this one (a registry export, the registry's table and page, a crate with a schema file # and a source that uses each kind of accounted string, a changelog, a binary file) and # asserts that the check passes it, and fails each way a string can go unaccounted, # naming the file and line. The namespace's prefix is spelled through $q here, so this # file holds none of the test's strings itself. set -uo pipefail here=$(cd "$(dirname "$0")" && pwd) check="$here/check-wai-quantum-names.py" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT q=wai.quantum mk() { rm -rf "$tmp/repo" mkdir -p "$tmp/repo/wai/registry" "$tmp/repo/wai/wai-rs/src/codecs" "$tmp/repo/wai/crate/src" \ "$tmp/repo/wai/crate/schemas" "$tmp/repo/wai/changes" cd "$tmp/repo" || exit 2 git init -q . cat > wai/registry/capabilities.json < wai/wai-rs/src/codecs/registry.rs < wai/REGISTRY.md cat > wai/crate/schemas/doc.v1.json < wai/crate/src/lib.rs <, "$q.". pub fn sentence() -> &'static str { "it reads $q.circuit." } R # The changelog keeps names as they were, and a binary file is not a source. printf '### A change\n\n- \x60%s\x60 was renamed.\n' "$q.renamed" > wai/CHANGELOG.md printf '### A change\n\n- \x60%s\x60 is new.\n' "$q.entry" > wai/changes/2026-01-01-a-change.md printf 'WQC1\0\0%s\0' "$q.circuit/9" > wai/crate/vector.bin git add -A cd "$here" || exit 2 } fails=0 expect() { # expect [text the output must hold] local want=$1 what=$2 needle=${3:-} out rc out=$(python3 -I "$check" --root "$tmp/repo" 2>&1); rc=$? if [ "$want" = pass ] && [ $rc -ne 0 ]; then echo "FAIL $what: the check failed"; printf '%s\n' "$out" | sed 's/^/ /'; fails=$((fails+1)); return; fi if [ "$want" = fail ] && [ $rc -eq 0 ]; then echo "FAIL $what: the check passed"; printf '%s\n' "$out" | sed 's/^/ /'; fails=$((fails+1)); return; fi if [ -n "$needle" ] && ! printf '%s\n' "$out" | grep -qF -- "$needle"; then echo "FAIL $what: the output does not say '$needle'"; printf '%s\n' "$out" | sed 's/^/ /'; fails=$((fails+1)); return fi echo "ok $what" } mk expect pass "every kind of accounted string, a placeholder, a prefix, the changelog and a binary file" mk printf 'pub const X: &str = "%s";\n' "$q.planted" >> "$tmp/repo/wai/crate/src/lib.rs" expect fail "an unlisted string in a tracked source" "wai/crate/src/lib.rs:8: $q.planted is not registered" mk printf '%s\n' "see $q.planted/1" > "$tmp/repo/wai/crate/notes.md" expect fail "an unlisted string in a file not yet added" "wai/crate/notes.md:1: $q.planted/1" mk printf '%s\n' "the family $q.nofam.*" >> "$tmp/repo/wai/crate/src/lib.rs" expect fail "a pattern that matches nothing" "$q.nofam.* matches no" mk sed -i.bak "s|$q.doc/1\"|$q.doc/2\"|" "$tmp/repo/wai/crate/schemas/doc.v1.json" && rm "$tmp/repo/wai/crate/schemas/doc.v1.json.bak" expect fail "a schema whose title is not its file's name" "doc.v1.json: its title is $q.doc/2" mk sed -i.bak "/FAM/d" "$tmp/repo/wai/crate/src/lib.rs" && rm "$tmp/repo/wai/crate/src/lib.rs.bak" sed -i.bak "s|$q.fam.\*|the family|" "$tmp/repo/wai/crate/src/lib.rs" && rm "$tmp/repo/wai/crate/src/lib.rs.bak" expect fail "a label nothing outside the registry uses" "the label $q.fam.one is used nowhere outside" mk sed -i.bak "s|\"name\": \"$q.wire/1\"|\"name\": \"$q.module\"|" "$tmp/repo/wai/registry/capabilities.json" && rm "$tmp/repo/wai/registry/capabilities.json.bak" expect fail "a string the registry holds twice" "$q.module is held twice" mk printf 'pub const X: &str = "%s";\n' "$q.planted" >> "$tmp/repo/wai/crate/src/lib.rs" sed -i.bak "s|{\"label\": \"$q.module\"|{\"label\": \"$q.planted\", \"reason\": \"r\"}, &|" "$tmp/repo/wai/registry/capabilities.json" && rm "$tmp/repo/wai/registry/capabilities.json.bak" expect pass "the planted string, once listed as a label" mk rm "$tmp/repo/wai/registry/capabilities.json" expect fail "no registry export (fails closed)" "cannot read wai/registry/capabilities.json" mk rm -rf "$tmp/repo/.git" expect fail "not a git checkout (fails closed)" "check-wai-quantum-names: git" if [ $fails -ne 0 ]; then echo "$fails self-test case(s) failed" exit 1 fi echo "check-wai-quantum-names: every self-test case behaves"