Files
improvise/scripts/check-harness-lock-test.sh
T

78 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# check_the_checker for scripts/check-harness-lock.sh (improvise-zs2r). The
# fixtures are path-only crates, so every case runs offline with no cache.
set -uo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUN="$here/check-harness-lock.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="$("$RUN" "$@" 2>&1 >/dev/null)"
local got=$?
if [ "$got" != "$want" ]; then
bad "$name" "$got" "$want"
printf '%s\n' "$err" | sed 's/^/ /'
elif [ -n "$says" ] && [[ "$err" != *"$says"* ]]; then
bad "$name: stderr did not say $says" "$got" "$want"
else
ok "$name"
fi
}
# $1 dir, $2 crate name, $3 extra [dependencies] lines, $4 "workspace" or ""
crate() {
mkdir -p "$1/src"
: > "$1/src/lib.rs"
{
printf '[package]\nname = "%s"\nversion = "0.0.0"\nedition = "2021"\n' "$2"
[ "${4:-}" = workspace ] && printf '\n[workspace]\n'
printf '\n[dependencies]\n%s' "$3"
} > "$1/Cargo.toml"
}
# A two-crate workspace shaped like the harness: `app` is its own root and
# reaches `lib` by path, so `lib`'s manifest is one the lock resolves against.
fixture() {
local root="$1"
crate "$root/lib" lib ""
crate "$root/extra" extra ""
crate "$root/app" app 'lib = { path = "../lib" }
' workspace
cargo generate-lockfile --offline --manifest-path "$root/app/Cargo.toml" 2>/dev/null
}
fixture "$work/current"
run_case "a current lock passes" 0 "" "$work/current/app/Cargo.toml"
# The shape improvise-zs2r names: only a PATH DEPENDENCY's manifest changed.
fixture "$work/stale"
crate "$work/stale/lib" lib 'extra = { path = "../extra" }
'
run_case "a manifest-only change to a path dependency is stale" 1 "STALE" \
"$work/stale/app/Cargo.toml"
# Offline and unresolvable is NOT a stale lock: the gate did not run.
fixture "$work/cold"
crate "$work/cold/lib" lib 'zs2r-no-such-crate-anywhere = "1"
'
run_case "an offline resolution failure is not a verdict" 2 "did not run" \
"$work/cold/app/Cargo.toml"
run_case "a missing manifest is a usage error" 2 "no manifest" \
"$work/nowhere/Cargo.toml"
run_case "two manifests is a usage error" 2 "" \
"$work/current/app/Cargo.toml" "$work/stale/app/Cargo.toml"
run_case "an unknown option is a usage error" 2 "unknown option" --bogus
printf '\ncheck-harness-lock: %d passed, %d failed\n' "$pass" "$fail"
[ "$fail" = 0 ]