#!/usr/bin/env bash # check_the_checker for scripts/assemble-stamp.sh. The gate it holds was # deleted on PR #69 by injection and the assembly still exited 0 with both # failures printed -- a guard nobody had watched fail (improvise-w8ih). set -uo pipefail here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" RUN="$here/assemble-stamp.sh" work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT pass=0; fail=0 ok() { printf ' ok %s\n' "$1"; pass=$((pass+1)); } bad() { printf ' FAIL %s (exit %s, wanted %s)\n' "$1" "$2" "$3"; fail=$((fail+1)); } # $1 name, $2 expected exit, $3 expected stderr substring ("" for none), rest: args run_case() { local name="$1" want="$2" says="$3"; shift 3 local err err="$(sh "$RUN" "$@" 2>&1 >/dev/null)" local got=$? if [ "$got" != "$want" ]; then bad "$name" "$got" "$want" elif [ -n "$says" ] && [[ "$err" != *"$says"* ]]; then bad "$name: stderr did not name it" "$err" "$says" else ok "$name" fi } printf 'import "./pkg/improvise_wasm_client.js?v=abc";\n' > "$work/stamped.js" printf 'import "./worker-pkg/improvise_worker_server.js?v=abc";\n' > "$work/stamped2.js" printf 'import "./pkg/improvise_wasm_client.js";\n' > "$work/bare.js" printf 'import "./worker-pkg/improvise_worker_server.js";\n' > "$work/bare2.js" run_case "a stamped file passes" 0 "" \ "$work/stamped.js" "pkg/improvise_wasm_client.js?v=abc" run_case "an unstamped file is refused and named" 1 "bare.js" \ "$work/bare.js" "pkg/improvise_wasm_client.js?v=abc" # The bug the gate exists for: without the final `test -z`, the misses print # and the script exits 0. Both must be NAMED, which is what exiting on the # first one cost. run_case "two misses are both named, not just the first" 1 "bare2.js" \ "$work/bare.js" "pkg/improvise_wasm_client.js?v=abc" \ "$work/bare2.js" "worker-pkg/improvise_worker_server.js?v=abc" run_case "a miss among hits is still refused" 1 "bare2.js" \ "$work/stamped.js" "pkg/improvise_wasm_client.js?v=abc" \ "$work/bare2.js" "worker-pkg/improvise_worker_server.js?v=abc" run_case "all four stamped is the shipping case" 0 "" \ "$work/stamped.js" "pkg/improvise_wasm_client.js?v=abc" \ "$work/stamped2.js" "worker-pkg/improvise_worker_server.js?v=abc" # A file the assembly never copied is indistinguishable from an unstamped one # to `grep`, which errors rather than failing quietly -- but only because the # -f test runs first. Without it `set -e` would abort mid-list and the later # files would go unreported, which is the failure this gate was built to end. run_case "a file that does not exist is a miss, not an abort" 1 "vanished.js" \ "$work/vanished.js" "anything" \ "$work/bare2.js" "worker-pkg/improvise_worker_server.js?v=abc" # A caller that drops one argument would otherwise have the last FILE read as # a PATTERN over the file before it -- a check that passes by accident. run_case "an odd number of arguments is a usage error" 2 "pairs" \ "$work/stamped.js" "pkg/improvise_wasm_client.js?v=abc" "$work/bare.js" run_case "no arguments at all is a usage error, not a pass" 2 "usage" printf '\nassemble-stamp-test: %d passed, %d failed\n' "$pass" "$fail" [ "$fail" -eq 0 ]