Files
Holocron/tests/test-apm-hooks-git-env.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

139 lines
4.6 KiB
Bash

#!/usr/bin/env bash
# Regression test for issue #204: the apm pre-push hooks must not hand a
# hook-exported GIT_DIR to apm.
#
# `git push` from a LINKED worktree exports GIT_DIR=<main>/.git/worktrees/<name>
# (absolute) to its hooks; from the main checkout it exports none. apm 0.28.0's
# drift replay runs `git init` / `git checkout` in a temp dir with an env copied
# from os.environ, so the inherited GIT_DIR wins: `git init` wrote
# core.bare=true into the shared .git/config, and a later checkout moved the
# worktree's HEAD to the lock-pinned commit.
#
# The stub `apm` below reproduces that pattern -- record GIT_DIR, then `git init`
# and `git checkout` inside a fresh `mktemp -d` -- and each hook script is run
# from a linked worktree of a throwaway repo with GIT_DIR exported the way git
# exports it. Nothing here touches the repo this suite lives in.
#
# The hooks run apm as `uv run --project <repo root> --frozen apm` (ADR-0034,
# scripts/lib/apm.sh), so a fake `uv` sits beside the stub and hands everything
# after `apm` to it. tests/test-apm-gate-invocation.sh pins that command shape;
# this suite only needs the call to land on the stub.
set -euo pipefail
# This suite builds its own repos; it must never inherit the caller's.
unset APM GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
FIXTURES=()
cleanup() { [[ ${#FIXTURES[@]} -eq 0 ]] || rm -rf "${FIXTURES[@]}"; }
trap cleanup EXIT
g() { git -c user.name=fixture -c user.email=[email protected] "$@"; }
# Builds <dir>/main (two commits on `main`) and <dir>/wt, a linked worktree on
# branch `feature`, with a plugins/demo/ package dir so the audit script's
# per-package loop has something to iterate. Also writes <dir>/bin/apm, the stub,
# and <dir>/bin/uv, the fake that reaches it.
make_fixture() {
local dir="$1"
mkdir -p "$dir/bin"
g init -q -b main "$dir/main"
echo one > "$dir/main/f"
g -C "$dir/main" add f
g -C "$dir/main" commit -q -m one
echo two > "$dir/main/f"
g -C "$dir/main" commit -q -am two
g -C "$dir/main" worktree add -q -b feature "$dir/wt"
mkdir -p "$dir/wt/plugins/demo"
# The "locked sha" the replay checks out: the first commit.
local locked
locked="$(g -C "$dir/main" rev-parse HEAD~1)"
cat > "$dir/bin/uv" <<EOF
#!/usr/bin/env bash
[[ "\${1:-}" == run && "\${5:-}" == apm ]] || { echo "fake uv: unexpected argv: \$*" >&2; exit 97; }
shift 5
exec "$dir/bin/apm" "\$@"
EOF
chmod +x "$dir/bin/uv"
# --version answers above the gates' 0.39.2 floor and records nothing.
cat > "$dir/bin/apm" <<EOF
#!/usr/bin/env bash
if [[ "\${1:-}" == --version ]]; then echo "apm version 0.39.3"; exit 0; fi
printf '%s\n' "\${GIT_DIR-<unset>}" >> "$dir/apm-saw-git-dir"
tmp="\$(mktemp -d)"
( cd "\$tmp" && git init -q . >/dev/null 2>&1; git checkout -q --detach $locked >/dev/null 2>&1 ) || true
rm -rf "\$tmp"
exit 0
EOF
chmod +x "$dir/bin/apm"
}
# check_hook <label> <script>
check_hook() {
local label="$1" script="$2" dir rc=0
dir="$(mktemp -d)"
FIXTURES+=("$dir")
make_fixture "$dir"
echo ""
echo "--- $label: run from a linked worktree with GIT_DIR exported ---"
if [[ ! -f "$script" ]]; then
fail "$label: $script does not exist"
return
fi
(
cd "$dir/wt"
export GIT_DIR="$dir/main/.git/worktrees/wt"
export PATH="$dir/bin:$PATH"
bash "$script"
) > "$dir/out" 2>&1 || rc=$?
if [[ "$rc" -eq 0 ]]; then
pass "$label: exits 0 when apm passes"
else
fail "$label: exited $rc; output: $(cat "$dir/out")"
fi
local bare
bare="$(git -C "$dir/main" config --get core.bare || true)"
if [[ "$bare" == "false" ]]; then
pass "$label: main repo core.bare is still false"
else
fail "$label: main repo core.bare is '$bare', expected false"
fi
local branch
branch="$(git -C "$dir/wt" symbolic-ref -q --short HEAD || echo '<detached>')"
if [[ "$branch" == "feature" ]]; then
pass "$label: worktree still on its branch"
else
fail "$label: worktree HEAD is '$branch', expected feature"
fi
if [[ ! -s "$dir/apm-saw-git-dir" ]]; then
fail "$label: stub apm never ran"
elif grep -qvxF '<unset>' "$dir/apm-saw-git-dir"; then
fail "$label: apm inherited GIT_DIR: $(sort -u "$dir/apm-saw-git-dir" | tr '\n' ' ')"
else
pass "$label: apm saw no GIT_DIR"
fi
}
check_hook apm-audit-ci "$REPO_ROOT/scripts/apm-audit-ci.sh"
check_hook apm-pack-check-clean "$REPO_ROOT/scripts/apm-pack-check-clean.sh"
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ "$FAIL" -eq 0 ]]