#!/usr/bin/env bash
#
# test-public-text-checks.sh
#
# Self-test of scripts/check-public-names.sh and scripts/check-comparative-prose.sh.
# Each case feeds a check a fixture file (or a pattern list) and states the exit
# status it must give:
# - a hit exits 1, a clean file exits 0;
# - an opt-out with a reason is honoured, and one with an empty reason (the
# comment form ``, whose `-->` must not count as the
# reason) is not;
# - a pattern grep cannot compile exits 2, never "clean";
# - the products of WAI's maintainers are hits beside any private list, and
# alone with --maintainer-products-only, which reads no private list.
# The fixtures name a made-up company, never a real one, and the maintainers'
# products, which check-public-names.sh lists itself.
#
# Exit status: 0 when every case gives its stated status, 1 otherwise.
set -uo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
names="$REPO_ROOT/scripts/check-public-names.sh"
prose="$REPO_ROOT/scripts/check-comparative-prose.sh"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
failures=0
in_dir() { # in_dir
: run a command from another directory
local d="$1"
shift
(cd "$d" && "$@")
}
names_file() { # names_file : the command exits 2 and its output names
local f="$1"
shift
"$@" >"$tmp/named" 2>&1
local got=$?
[ "$got" -eq 2 ] && grep -qF "$f" "$tmp/named"
}
expect() { # expect
local want="$1" label="$2"
shift 2
"$@" >"$tmp/out" 2>&1
local got=$?
if [ "$got" -eq "$want" ]; then
echo "ok $label (exit $got)"
else
echo "FAIL $label: exit $got, expected $want"
sed 's/^/ /' "$tmp/out"
failures=$((failures + 1))
fi
}
printf 'This line names Zyxwvut Holdings.\n' > "$tmp/bare.md"
printf 'Zyxwvut Holdings \n' > "$tmp/reasoned.md"
printf 'Zyxwvut Holdings \n' > "$tmp/empty-comment.md"
printf 'Zyxwvut Holdings // allow-name:\n' > "$tmp/empty-line.rs"
printf 'This line names nothing.\n' > "$tmp/clean.md"
list='Zyxwvut'
expect 1 "names: a bare name is a hit" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/bare.md"
expect 0 "names: an opt-out with a reason is honoured" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/reasoned.md"
expect 1 "names: an opt-out with an empty reason in a comment is not" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/empty-comment.md"
expect 1 "names: an opt-out with an empty reason at end of line is not" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/empty-line.rs"
expect 0 "names: a clean file is clean" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/clean.md"
expect 2 "names: a malformed pattern fails closed" env WAI_NAME_DENYLIST="$(printf 'Zyxwvut\n([unclosed')" bash "$names" "$tmp/clean.md"
expect 2 "names: an unset list fails closed" env -u WAI_NAME_DENYLIST bash "$names" "$tmp/clean.md"
expect 2 "names: a missing file fails closed" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/no-such-file.md"
mkdir -p "$tmp/sub"
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30; do
printf 'Clean line %s.\n' "$i" > "$tmp/sub/clean-$i.md"
done
many=()
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30; do many+=("sub/clean-$i.md"); done
expect 1 "names: a path relative to the caller's directory is read from there" in_dir "$tmp" env WAI_NAME_DENYLIST="$list" bash "$names" bare.md
expect 0 "names: many clean files given relative to the caller's directory are clean" in_dir "$tmp" env WAI_NAME_DENYLIST="$list" bash "$names" "${many[@]}"
expect 1 "names: a hit among many relative paths is a hit" in_dir "$tmp" env WAI_NAME_DENYLIST="$list" bash "$names" "${many[@]}" bare.md
expect 0 "names: a missing file among many fails closed and is named" names_file "gone.md" in_dir "$tmp" env WAI_NAME_DENYLIST="$list" bash "$names" "${many[@]}" sub/gone.md
expect 0 "names: a directory given as a file fails closed and is named" names_file "/sub" in_dir "$tmp" env WAI_NAME_DENYLIST="$list" bash "$names" "${many[@]}" sub
printf 'The failure cost waiTransport proposed.\n' > "$tmp/product-joined.md"
printf 'A sidecar under waitransport.provisional.route.\n' > "$tmp/product-lower.md"
printf 'A crate named wai_transport.\n' > "$tmp/product-underscore.md"
printf 'It runs on waiOS.\n' > "$tmp/product-short.md"
printf 'The WAI transport layer, WAI OS integration and xWAIOSy.\n' > "$tmp/product-generic.md"
printf 'waiTransport \n' > "$tmp/product-reasoned.md"
expect 1 "names: a maintainer's product is a hit beside an unrelated private list" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/product-joined.md"
expect 1 "names: a maintainer's product in lower case is a hit" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/product-lower.md"
expect 1 "names: a maintainer's product joined by an underscore is a hit" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/product-underscore.md"
expect 1 "names: the shorter product name standing alone is a hit" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/product-short.md"
expect 0 "names: the generic words, and the shorter name inside a word, are not hits" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/product-generic.md"
expect 0 "names: an opt-out with a reason is honoured for a product" env WAI_NAME_DENYLIST="$list" bash "$names" "$tmp/product-reasoned.md"
expect 2 "names: with the products listed, an unset private list still fails closed" env -u WAI_NAME_DENYLIST bash "$names" "$tmp/product-joined.md"
expect 1 "names: products only, a product is a hit with no private list" env -u WAI_NAME_DENYLIST bash "$names" --maintainer-products-only "$tmp/product-joined.md"
expect 0 "names: products only, a clean file is clean with no private list" env -u WAI_NAME_DENYLIST bash "$names" --maintainer-products-only "$tmp/clean.md"
expect 0 "names: products only, the private list is not read" env WAI_NAME_DENYLIST="$list" bash "$names" --maintainer-products-only "$tmp/bare.md"
expect 2 "names: products only, a missing file fails closed" env -u WAI_NAME_DENYLIST bash "$names" --maintainer-products-only "$tmp/no-such-file.md"
printf 'Its reproducibility is the superiority claim.\n' > "$tmp/superiority.md"
printf 'About 8 kbps, well below Xcodec at the same quality.\n' > "$tmp/well-below.md"
printf 'Its token rate is among the lowest of any codec.\n' > "$tmp/among.md"
printf 'One property nothing else in the field offers.\n' > "$tmp/nothing-else.md"
printf 'State-feeds are the biggest commercial wedge.\n' > "$tmp/wedge.md"
printf 'Joules-per-decode, the unclaimed metric.\n' > "$tmp/unclaimed.md"
printf 'Haptics is a fast win.\n' > "$tmp/fast-win.md"
printf 'Carriage is not a differentiator any more.\n' > "$tmp/differentiator.md"
printf 'It is not a rate-competitive codec.\n' > "$tmp/rate-competitive.md"
printf 'This is the moat \n' > "$tmp/prose-empty.md"
printf 'WAI_MOAT_DIR \n' > "$tmp/prose-reasoned.md"
printf 'Neither, in what they publish, meters the callout or signs what ran.\n' > "$tmp/neither-signs.md"
printf 'Neither engine appears in a receipt.\n' > "$tmp/neither-clean.md"
printf 'The decoder is integer all the way down.\n' > "$tmp/prose-clean.md"
expect 1 "prose: superiority" bash "$prose" "$tmp/superiority.md"
expect 1 "prose: well below " bash "$prose" "$tmp/well-below.md"
expect 1 "prose: among the lowest" bash "$prose" "$tmp/among.md"
expect 1 "prose: nothing else ... offers" bash "$prose" "$tmp/nothing-else.md"
expect 1 "prose: commercial wedge" bash "$prose" "$tmp/wedge.md"
expect 1 "prose: unclaimed" bash "$prose" "$tmp/unclaimed.md"
expect 1 "prose: fast win" bash "$prose" "$tmp/fast-win.md"
expect 1 "prose: differentiator" bash "$prose" "$tmp/differentiator.md"
expect 1 "prose: rate-competitive" bash "$prose" "$tmp/rate-competitive.md"
expect 1 "prose: neither ... signs" bash "$prose" "$tmp/neither-signs.md"
expect 0 "prose: neither, of our own two things" bash "$prose" "$tmp/neither-clean.md"
expect 1 "prose: an opt-out with an empty reason is not honoured" bash "$prose" "$tmp/prose-empty.md"
expect 0 "prose: an opt-out with a reason is honoured" bash "$prose" "$tmp/prose-reasoned.md"
expect 0 "prose: a clean file is clean" bash "$prose" "$tmp/prose-clean.md"
expect 2 "prose: a missing file fails closed" bash "$prose" "$tmp/no-such-file.md"
if [ $failures -gt 0 ]; then
echo "$failures case(s) gave the wrong status"
exit 1
fi
echo "public-text check self-test: every case gave its stated status"