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

74 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Fails in seconds when harness/opfs-contract/Cargo.lock no longer matches the
# manifests it resolves against. Reasoning: context/code/tooling.md, "The
# harness lock gate" (improvise-zs2r).
set -uo pipefail
usage() {
cat >&2 <<'EOF'
usage: check-harness-lock.sh [--allow-network] [manifest]
manifest the Cargo.toml whose lock is checked; defaults to
harness/opfs-contract/Cargo.toml beside this script
--allow-network resolve without --offline (CI); the default never
touches the network
exit 0 the lock is current
exit 1 the lock is stale: a manifest it resolves against changed
exit 2 the gate did not run: usage error, no cargo, or cargo could not
resolve at all (offline, with a dependency missing from the local
cache). NO verdict was produced. Never collapse 2 into 1.
EOF
}
main() {
local network=0 manifest=""
while [ $# -gt 0 ]; do
case "$1" in
--allow-network) network=1 ;;
-h|--help) usage; return 2 ;;
-*) printf 'check-harness-lock: unknown option %s\n' "$1" >&2; usage; return 2 ;;
*)
if [ -n "$manifest" ]; then usage; return 2; fi
manifest="$1" ;;
esac
shift
done
if [ -z "$manifest" ]; then
manifest="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/harness/opfs-contract/Cargo.toml"
fi
if [ ! -f "$manifest" ]; then
printf 'check-harness-lock: no manifest at %s\n' "$manifest" >&2
return 2
fi
if ! command -v cargo >/dev/null 2>&1; then
echo 'check-harness-lock: no cargo on PATH' >&2
return 2
fi
local -a args=(metadata --locked --format-version 1 --manifest-path "$manifest")
if [ "$network" = 0 ]; then args+=(--offline); fi
local err rc
err="$(cargo "${args[@]}" 2>&1 >/dev/null)"
rc=$?
if [ "$rc" = 0 ]; then
printf 'check-harness-lock: %s is current\n' "${manifest%Cargo.toml}Cargo.lock"
return 0
fi
# The one message that means "resolution succeeded and disagrees with the lock".
if printf '%s' "$err" | grep -q 'cannot update the lock file .* because --locked was passed'; then
printf 'check-harness-lock: STALE: %s\n' "${manifest%Cargo.toml}Cargo.lock" >&2
printf ' Run: cargo update -w --manifest-path %s, and commit the lock.\n' "$manifest" >&2
return 1
fi
printf 'check-harness-lock: the gate did not run; cargo could not resolve:\n%s\n' "$err" >&2
if [ "$network" = 0 ]; then
echo ' Offline, this is usually a dependency missing from the local cache.' >&2
echo ' Retry with --allow-network, or fetch once and rerun.' >&2
fi
return 2
}
main "$@"