Files
Holocron/tests/test-apm-gate-invocation.sh
T
Defame1297andClaude Opus 5.5 7291b46271 feat(scripts): run the apm gates through the uv-pinned apm
Add scripts/lib/apm.sh, the one place the repo spells its apm command:
`uv run --project <repo root> --frozen apm`. --project comes from the
helper's own location, so it names the repo root even when apm runs inside
plugins/<name>/. Sourcing it keeps the git-env scrub (#204), and
require_apm_version fails with a clear message, naming `uv sync --frozen`,
when the resolved apm is below 0.39.2, unreadable, or uv is missing. APM
overrides the command, as ADR-0034 provides.

apm-audit-ci.sh and apm-pack-check-clean.sh use it. The audit script's
matching logic is unchanged; only its verified-against note now records
0.39.3.

tests/test-apm-gate-invocation.sh is seam B: a fake uv and a stub apm
first on a PATH without the real ones. It proves the command shape,
--project from a plugin directory, the scrub, and the version floor.
test-apm-hooks-git-env.sh reaches its stub through a fake uv, and the
real-apm probe in test-apm-current-hook.sh now runs the pinned apm.

Refs: #271

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
2026-12-02 08:47:12 +00:00

197 lines
7.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Tests how the apm pre-push gates invoke apm (ADR-0034, issue #271).
#
# Seam B: a fake `uv` and a stub `apm` sit first on a PATH that excludes the
# machine's real uv and any global apm. The fake uv records its cwd, its argv
# and the GIT_DIR it inherited, accepts only the `run --project <root> --frozen
# apm ...` shape, and hands the rest to the stub. The stub answers --version
# with $STUB_APM_VERSION and records every other call. Nothing here runs a real
# apm or touches the repo this suite lives in.
#
# It proves, for both scripts/apm-audit-ci.sh and scripts/apm-pack-check-clean.sh:
# * the command is `uv run --project <repo root> --frozen apm`, and --project
# names the repo root even when apm runs from a plugin directory;
# * git's repo-locating env is still scrubbed before apm runs (issue #204);
# * an apm below 0.39.2 fails with a clear message before any gate work runs.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
AUDIT="$REPO_ROOT/scripts/apm-audit-ci.sh"
PACK="$REPO_ROOT/scripts/apm-pack-check-clean.sh"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
# The test must never inherit an override or the caller's repo.
unset APM GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
BIN="$TMP/bin"
mkdir -p "$BIN"
UV_LOG="$TMP/uv.log"
APM_LOG="$TMP/apm.log"
cat > "$BIN/uv" << EOF
#!/usr/bin/env bash
{
printf 'cwd=%s\n' "\$PWD"
printf 'git_dir=%s\n' "\${GIT_DIR-<unset>}"
printf 'git_work_tree=%s\n' "\${GIT_WORK_TREE-<unset>}"
printf 'argv=%s\n' "\$*"
} >> "$UV_LOG"
if [[ "\${1:-}" != run || "\${2:-}" != --project || "\${4:-}" != --frozen || "\${5:-}" != apm ]]; then
echo "fake uv: unexpected argv: \$*" >&2
exit 97
fi
shift 5
exec "$BIN/apm" "\$@"
EOF
cat > "$BIN/apm" << EOF
#!/usr/bin/env bash
if [[ "\${1:-}" == --version ]]; then
printf '%s\n' "\${STUB_APM_OUTPUT:-Agent Package Manager (APM) CLI version \${STUB_APM_VERSION:-0.39.3}}"
exit 0
fi
printf 'cwd=%s args=%s\n' "\$PWD" "\$*" >> "$APM_LOG"
exit 0
EOF
chmod +x "$BIN/uv" "$BIN/apm"
# A PATH with the fakes first and only system directories after them: no
# /usr/local/bin (the real uv) and no ~/.local/bin (the global pipx apm).
FAKE_PATH="$BIN:/usr/bin:/bin"
NO_UV_PATH="/usr/bin:/bin"
for tool in uv apm; do
if PATH="$NO_UV_PATH" command -v "$tool" > /dev/null 2>&1; then
echo "SKIP: a real $tool is on $NO_UV_PATH, so the fake cannot be isolated"
exit 77
fi
done
# A fixture tree for apm-audit-ci.sh's per-manifest loop: a root and one
# plugin package. The script iterates `. plugins/*/` relative to its cwd.
FIXTURE="$TMP/fixture"
mkdir -p "$FIXTURE/plugins/demo"
reset_logs() { : > "$UV_LOG"; : > "$APM_LOG"; }
# run_gate <cwd> <script> [VAR=value ...] — sets RC and OUT (stdout+stderr).
RC=0
OUT=""
run_gate() {
local cwd="$1" script="$2"
shift 2
RC=0
OUT="$(cd "$cwd" && env PATH="$FAKE_PATH" "$@" bash "$script" 2>&1)" || RC=$?
}
# Every uv call names the repo root as --project and uses --frozen.
check_project_root() {
local label="$1" n bad
n="$(grep -c '^argv=' "$UV_LOG" || true)"
if [[ "$n" -eq 0 ]]; then
fail "$label: uv was never called"
return
fi
bad="$(grep '^argv=' "$UV_LOG" | grep -vF "argv=run --project $REPO_ROOT --frozen apm " || true)"
if [[ -z "$bad" ]]; then
pass "$label: all $n uv call(s) are 'run --project <repo root> --frozen apm'"
else
fail "$label: uv called with another shape: $bad"
fi
}
echo "--- apm-pack-check-clean: the command, run from a plugin directory ---"
reset_logs
run_gate "$REPO_ROOT/plugins/kyberforge" "$PACK"
[[ $RC -eq 0 ]] && pass "exits 0 when the pinned apm passes" || fail "exited $RC: $OUT"
check_project_root "apm-pack-check-clean from plugins/kyberforge"
grep -qxF "cwd=$REPO_ROOT/plugins/kyberforge args=pack --check-versions --check-clean --dry-run" "$APM_LOG" \
&& pass "runs 'pack --check-versions --check-clean --dry-run' in the caller's cwd" \
|| fail "unexpected apm calls: $(cat "$APM_LOG")"
echo ""
echo "--- apm-audit-ci: --project stays the repo root inside each plugin dir ---"
reset_logs
run_gate "$FIXTURE" "$AUDIT"
[[ $RC -eq 0 ]] && pass "exits 0 when every audit passes" || fail "exited $RC: $OUT"
check_project_root "apm-audit-ci"
grep -qxF "cwd=$FIXTURE/plugins/demo args=audit --ci" "$APM_LOG" \
&& pass "audits plugins/demo from inside that directory" \
|| fail "no audit ran in plugins/demo: $(cat "$APM_LOG")"
grep -qxF "cwd=$FIXTURE args=audit --ci" "$APM_LOG" \
&& pass "audits the root manifest" || fail "no root audit: $(cat "$APM_LOG")"
echo ""
echo "--- the git-env scrub still applies ---"
for pair in "apm-audit-ci:$AUDIT" "apm-pack-check-clean:$PACK"; do
label="${pair%%:*}"
script="${pair#*:}"
reset_logs
run_gate "$FIXTURE" "$script" GIT_DIR="$TMP/elsewhere/.git" GIT_WORK_TREE="$TMP/elsewhere"
[[ $RC -eq 0 ]] || fail "$label: exited $RC with GIT_DIR exported: $OUT"
if grep -q '^git_dir=' "$UV_LOG" && ! grep -q '^git_dir=[^<]' "$UV_LOG" \
&& ! grep -q '^git_work_tree=[^<]' "$UV_LOG"; then
pass "$label: uv (and so apm) saw no GIT_DIR or GIT_WORK_TREE"
else
fail "$label: git env leaked: $(grep '^git_' "$UV_LOG" | sort -u | tr '\n' ' ')"
fi
done
echo ""
echo "--- the minimum-version check ---"
for pair in "apm-audit-ci:$AUDIT" "apm-pack-check-clean:$PACK"; do
label="${pair%%:*}"
script="${pair#*:}"
for old in 0.39.1 0.28.0; do
reset_logs
run_gate "$FIXTURE" "$script" STUB_APM_VERSION="$old"
[[ $RC -ne 0 ]] && pass "$label: fails on apm $old" || fail "$label: apm $old must fail"
grep -qF "apm $old is below 0.39.2" <<< "$OUT" && grep -qF "uv sync --frozen" <<< "$OUT" \
&& pass "$label: says $old is below 0.39.2 and names the fix" \
|| fail "$label: unclear message for $old: $OUT"
[[ ! -s "$APM_LOG" ]] && pass "$label: ran no gate work on apm $old" \
|| fail "$label: ran apm past the version check: $(cat "$APM_LOG")"
done
for ok in 0.39.2 0.39.10 1.0.0; do
reset_logs
run_gate "$FIXTURE" "$script" STUB_APM_VERSION="$ok"
[[ $RC -eq 0 ]] && pass "$label: accepts apm $ok" || fail "$label: apm $ok must pass: $OUT"
done
reset_logs
run_gate "$FIXTURE" "$script" STUB_APM_OUTPUT="apm, version unknown"
[[ $RC -ne 0 ]] && grep -qF "printed no version" <<< "$OUT" \
&& pass "$label: fails when the version cannot be read" \
|| fail "$label: an unreadable version must fail clearly (rc=$RC): $OUT"
done
echo ""
echo "--- no uv, and the APM override ---"
RC=0
OUT="$(cd "$FIXTURE" && env PATH="$NO_UV_PATH" bash "$PACK" 2>&1)" || RC=$?
[[ $RC -ne 0 ]] && grep -qF "uv is not on PATH" <<< "$OUT" \
&& pass "fails clearly when uv is missing" || fail "missing uv must fail clearly (rc=$RC): $OUT"
reset_logs
RC=0
OUT="$(cd "$FIXTURE" && env PATH="$NO_UV_PATH" APM="$BIN/apm" bash "$PACK" 2>&1)" || RC=$?
[[ $RC -eq 0 && ! -s "$UV_LOG" ]] && grep -qF "args=pack" "$APM_LOG" \
&& pass "APM overrides the command and bypasses uv" \
|| fail "APM override not honoured (rc=$RC): $OUT"
reset_logs
RC=0
OUT="$(cd "$FIXTURE" && env PATH="$NO_UV_PATH" APM="$BIN/apm" STUB_APM_VERSION=0.28.0 bash "$PACK" 2>&1)" || RC=$?
[[ $RC -ne 0 ]] && grep -qF "below 0.39.2" <<< "$OUT" \
&& pass "the version check applies to an APM override too" \
|| fail "an old apm via APM must fail (rc=$RC): $OUT"
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ "$FAIL" -eq 0 ]]