#!/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 --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 --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-}" printf 'git_work_tree=%s\n' "\${GIT_WORK_TREE-}" 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