#!/usr/bin/env bash # # test-check-deps.sh exercises scripts/check-deps.sh without a module proxy or # network access. It builds throwaway manifests in a temporary directory and # asserts the script's exit status and diagnostics. set -euo pipefail here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" script="$here/check-deps.sh" tmp="$(mktemp -d)" trap 'rm -rf "$tmp"' EXIT failures=0 expect_status() { local want="$1" got shift set +e "$@" >"$tmp/out" 2>&1 got=$? set -e if [ "$got" -ne "$want" ]; then echo "FAIL: expected exit $want, got $got: $*" >&2 cat "$tmp/out" >&2 failures=$((failures + 1)) fi } # fixture sets up $tmp/repo/go.mod and prints the go.mod path. fixture() { mkdir -p "$tmp/repo" cat >"$tmp/repo/go.mod" <<'EOF' module example.com/fixture go 1.27.1 require ( github.com/a/b v1.0.0 github.com/c/d v1.3.0 ) EOF } # facts writes a dependency-facts document to $tmp/facts.json. # Arguments: facts() { local freshness="$1" shift local items='' local item for item in "$@"; do [ -z "$items" ] || items+=',' items+="$item" done cat >"$tmp/facts.json" <&2; failures=$((failures + 1)); } # 2. A stale pin is reported and fails. fixture facts available '{"name":"github.com/a/b","current":"v1.0.0","latest":"v1.2.0","status":"observed"},{"name":"github.com/c/d","current":"v1.3.0","latest":"v1.3.0","status":"observed"}' expect_status 1 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" grep -q 'github.com/a/b v1.0.0 (latest observed: v1.2.0)' "$tmp/out" || { echo 'FAIL: stale module not named' >&2; failures=$((failures + 1)); } # 3. A pin that is newer than the observation cannot be verified. fixture facts available '{"name":"github.com/a/b","current":"v1.0.0","latest":"v1.0.0","status":"observed"}' expect_status 1 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" grep -q 'not covered by the dependency facts' "$tmp/out" || { echo 'FAIL: uncovered module not reported' >&2; failures=$((failures + 1)); } # 4. Without a proxy and without facts nothing is verified. fixture expect_status 2 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/missing.json" GOPROXY=off "$script" grep -q 'cannot verify dependency freshness' "$tmp/out" || { echo 'FAIL: unverified run did not explain itself' >&2; failures=$((failures + 1)); } # 5. Incomplete dependency facts are not trusted. fixture facts unavailable "$up_to_date" expect_status 2 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" # 6. Items that were not observed are not treated as current. fixture facts available '{"name":"github.com/a/b","current":"v1.0.0","latest":"v1.0.0","status":"not_found"},{"name":"github.com/c/d","current":"v1.3.0","latest":"v1.3.0","status":"observed"}' expect_status 1 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" # 7. A facts document from a different source is rejected. fixture sed 's#https://proxy.golang.org#https://example.invalid#' "$tmp/facts.json" >"$tmp/other.json" expect_status 2 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/other.json" GOPROXY=off "$script" if [ "$failures" -ne 0 ]; then echo "$failures check-deps test(s) failed" >&2 exit 1 fi echo 'ok: check-deps.sh behaviour is covered'