#!/usr/bin/env bash # # standards-touched-selftest.sh — the plan builds what a change can affect, and fails # toward building. # # Builds a scratch repository shaped like this one (a workflow with three workspaces, # one depending on another by path, one reading a file outside its area) and asserts # what standards-touched.py decides for each kind of change. set -uo pipefail here=$(cd "$(dirname "$0")" && pwd) tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT cd "$tmp" git init -q && git config user.email t@example.invalid && git config user.name t mkdir -p .github/workflows scripts a/a-rs/src b/b-rs/src c/c-rs/src shared cp "$here/standards-touched.py" scripts/ cat > .github/workflows/standards-build.yml <<'W' jobs: plan: env: WORKSPACES: | - { name: a-rs, dir: a/a-rs, runner: linux } - { name: b-rs, dir: b/b-rs, runner: linux-arm64, z3: true } - { name: c-rs, dir: c/c-rs, runner: macos, feature_tests: "-p x --features y,z" } W printf '[package]\nname="a"\n[dependencies]\nb = { path = "../../b/b-rs" }\n' > a/a-rs/Cargo.toml printf '[package]\nname="b"\n' > b/b-rs/Cargo.toml printf '[package]\nname="c"\n' > c/c-rs/Cargo.toml printf 'const D: &str = include_str!("../../../shared/data.txt");\n' > c/c-rs/src/lib.rs : > a/a-rs/src/lib.rs; : > b/b-rs/src/lib.rs; echo one > shared/data.txt; echo x > README.md git add -A && git commit -qm base && base=$(git rev-parse HEAD) fails=0 expect() { # expect local what=$1 want=$2 got head=$(git rev-parse HEAD) got=$(python3 -I scripts/standards-touched.py "$base" "$head" | python3 -c 'import json,sys; print(",".join(sorted(json.load(sys.stdin)["run"])))') if [ "$got" = "$want" ]; then echo "ok $what: runs [$got]"; else echo "FAIL $what: runs [$got], expected [$want]"; fails=$((fails+1)); fi git reset -q --hard "$base" } echo y >> README.md && git commit -qam readme; expect "a file in no area" "" echo y >> b/b-rs/src/lib.rs && git commit -qam b; expect "b's area, which a depends on by path" "a-rs,b-rs" echo y >> a/a-rs/src/lib.rs && git commit -qam a; expect "a's own area" "a-rs" echo y >> shared/data.txt && git commit -qam shared; expect "a file c's sources include" "c-rs" echo y >> .github/workflows/standards-build.yml && git commit -qam wf; expect "the workflow itself" "a-rs,b-rs,c-rs" echo y >> scripts/standards-touched.py && git commit -qam script; expect "the planning script" "a-rs,b-rs,c-rs" got=$(python3 -I scripts/standards-touched.py "" "" | python3 -c 'import json,sys; print(",".join(sorted(json.load(sys.stdin)["run"])))') [ "$got" = "a-rs,b-rs,c-rs" ] && echo "ok no pull request (a push): runs everything" || { echo "FAIL a push ran [$got]"; fails=$((fails+1)); } got=$(python3 -I scripts/standards-touched.py "$base" "0000000000000000000000000000000000000bad" 2>/dev/null | python3 -c 'import json,sys; print(",".join(sorted(json.load(sys.stdin)["run"])))') [ "$got" = "a-rs,b-rs,c-rs" ] && echo "ok a diff it cannot compute: runs everything" || { echo "FAIL an unknown head ran [$got]"; fails=$((fails+1)); } out=$(GITHUB_OUTPUT="$tmp/out" python3 -I scripts/standards-touched.py --github-output "" "" >/dev/null; cat "$tmp/out") grep -q '"z3":true' <<<"$out" && grep -q '"feature_tests":"-p x --features y,z"' <<<"$out" && grep -q '^energy=false$' <<<"$out" \ && echo "ok the matrix entries keep their booleans and quoted values" || { echo "FAIL the emitted matrix: $out"; fails=$((fails+1)); } if [ $fails -ne 0 ]; then echo "standards-touched-selftest: $fails failure(s)"; exit 1; fi echo "standards-touched-selftest: the plan builds what a change can affect, and everything when unsure."