Files
Holocron/tests/test-check-executables-allow-sync.sh
T
Defame1297andClaude Opus 5.5 e1a99a7291 fix(apm): key executables.allow on the full 0.39 package identity
apm 0.39.3 matches executable grants on the package identity
<host>/<owner>/<repo>/<path>. The bare kyberforge#<version> and
delivery#<version> keys that 0.28.0 matched no longer match, so the first
0.39 install parks both hooks and rewrites .claude/settings.json without
them, while root apm audit --ci stays green (spike #269, on #268).

Rename both keys to git.rkdr.net/Mortis/Holocron/plugins/<name>#<version>
with type-level grants, as `apm approve --list` names them on 0.39.3. Keep
the version suffix convention; apm strips it before matching. Do not use
`apm approve`, which would content-pin, strip the block's comments and
park the hook on every script edit.

check-executables-allow-sync now derives each expected key from root
apm.yml's git + path dependency entry for the plugin, fails a bare key with
a message saying why, and fails a shipping plugin with no such entry. Both
readers carry the change, and the test covers URL and path spellings.

Refs: #271

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

436 lines
19 KiB
Bash

#!/usr/bin/env bash
# Tests for scripts/check-executables-allow-sync.sh — the pre-push gate that
# keeps root apm.yml's executables.allow keys level with the version of every
# plugin that ships executables (plugins/<name>/.apm/hooks/ or bin/).
#
# Fixtures are skeletons (root apm.yml + plugins/<name>/apm.yml + an empty
# hooks/ or bin/ dir) rather than copies of the real repo: the gate reads
# exactly those files, and a hand-built fixture is the only way to construct
# the drift it exists to catch without editing the real manifests.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SCRIPT="$REPO_ROOT/scripts/check-executables-allow-sync.sh"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
# Same `exit 77` (automake convention; run-tests.sh renders it as SKIPPED) guard
# the vale suites use. The script itself runs fine without python3 — it falls
# back to a shape scan — but this suite asserts BOTH readers agree, and the
# PyYAML path cannot be exercised at all on a machine without it. Reporting
# those cases as failures would say "a regression landed" when the truth is
# "this machine is missing a dev dependency".
command -v python3 > /dev/null 2>&1 || { echo "SKIP: python3 is required to exercise the PyYAML reader"; exit 77; }
python3 -c 'import yaml' > /dev/null 2>&1 || { echo "SKIP: PyYAML is required to exercise the PyYAML reader"; exit 77; }
# One root per run: fixture helpers run inside $(...) subshells, where an
# array append never reaches the parent's trap, so register the root instead.
RUN_TMP="$(mktemp -d)"
trap 'rm -rf "$RUN_TMP"' EXIT
# make_fixture <plugin-version-line> <root-executables-block>
# The executables block is passed verbatim (may be empty) so a fixture can omit
# it entirely, which is one of the failure modes under test.
make_fixture() {
local version_line="$1" executables_block="$2" dir
dir="$(mktemp -d "$RUN_TMP/fx.XXXXXX")"
# kyberforge ships both hooks and bin, which is what makes the gate check it.
mkdir -p "$dir/plugins/kyberforge/.apm/hooks" "$dir/plugins/kyberforge/bin"
{
echo "name: kyberforge"
echo "$version_line"
echo "description: fixture"
} > "$dir/plugins/kyberforge/apm.yml"
# Both plugins are declared the way the real root apm.yml declares them, as
# git + path entries: the gate derives each key's identity from these.
{
echo "name: ai-development"
echo "version: 0.0.1"
echo "dependencies:"
echo " apm:"
echo " - git: https://git.example.invalid/Owner/Repo.git"
echo " path: plugins/kyberforge"
echo " - git: https://git.example.invalid/Owner/Repo.git"
echo " path: plugins/delivery"
echo " mcp: []"
[[ -n "$executables_block" ]] && printf '%s\n' "$executables_block"
# A top-level key after the block: the fallback reader must stop collecting
# allow keys here rather than reading on into the next section.
echo "marketplace:"
echo " owner:"
echo " name: fixture"
} > "$dir/apm.yml"
printf '%s\n' "$dir"
}
# add_plugin <fixture> <dir> <version-line> [<name>] — a second plugin that
# ships hooks only (like delivery). <name> defaults to <dir>.
add_plugin() {
local dir="$1/plugins/$2"
mkdir -p "$dir/.apm/hooks"
{
echo "name: ${4:-$2}"
echo "$3"
} > "$dir/apm.yml"
}
# The identity apm 0.39 matches on: <host>/<owner>/<repo>/<path>.
ID=git.example.invalid/Owner/Repo/plugins
MATCHING_BLOCK='executables:
allow:
git.example.invalid/Owner/Repo/plugins/kyberforge#1.5.0:
hooks: true
bin: true'
STALE_BLOCK='executables:
allow:
git.example.invalid/Owner/Repo/plugins/kyberforge#1.4.0:
hooks: true
bin: true'
TWO_MATCHING_BLOCK='executables:
allow:
git.example.invalid/Owner/Repo/plugins/kyberforge#1.5.0:
hooks: true
bin: true
# delivery ships a guard hook; an indented comment must not read as a key.
git.example.invalid/Owner/Repo/plugins/delivery#0.1.0:
hooks: true'
TWO_STALE_BLOCK='executables:
allow:
git.example.invalid/Owner/Repo/plugins/kyberforge#1.4.0:
hooks: true
bin: true
git.example.invalid/Owner/Repo/plugins/delivery#0.0.9:
hooks: true'
OTHER_PACKAGE_BLOCK='executables:
allow:
git.example.invalid/Owner/Repo/plugins/git#1.0.0:
hooks: true'
# The bare '<name>#<version>' form apm 0.28 matched and 0.39 does not.
BARE_BLOCK='executables:
allow:
kyberforge#1.5.0:
hooks: true
bin: true'
# run_gate <fixture> — echoes combined output, sets GATE_RC.
GATE_RC=0
run_gate() {
GATE_RC=0
bash "$SCRIPT" "$1" > /dev/null 2>&1 || GATE_RC=$?
}
# ---------------------------------------------------------------------------
echo "--- the real repo passes ---"
# ---------------------------------------------------------------------------
# The gate's whole value is that it is green on a correct tree and red on drift;
# a version bump landing in only one of the two real manifests must show up here.
run_gate "$REPO_ROOT"
[[ $GATE_RC -eq 0 ]] && pass "current repo state passes" \
|| fail "current repo state should pass — the gate said: $(bash "$SCRIPT" "$REPO_ROOT" 2>&1 | head -3)"
# ---------------------------------------------------------------------------
echo ""
echo "--- matching version ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "exits 0 when the allow key names the plugin's version" \
|| fail "should pass when key and version agree, got rc=$GATE_RC"
F="$(make_fixture 'version: "1.5.0"' "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "exits 0 when the version is quoted" \
|| fail "a quoted version must compare the same as an unquoted one, got rc=$GATE_RC"
# ---------------------------------------------------------------------------
echo ""
echo "--- mismatched version ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$STALE_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when the allow key names a different version" \
|| fail "a stale allow key must fail the push"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && pass "names the key that should be there" \
|| fail "the failure must state the expected key"
grep -q "kyberforge#1.4.0" <<< "$OUT" && pass "names the stale key it found instead" \
|| fail "the failure must quote back the stale key"
grep -q "Why:" <<< "$OUT" && grep -q "Fix:" <<< "$OUT" && pass "uses the FAIL/Why/Fix message block" \
|| fail "message must carry Why: and Fix: lines"
# ---------------------------------------------------------------------------
echo ""
echo "--- missing executables.allow ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when there is no executables block at all" \
|| fail "a missing executables.allow must fail — apm deploys no hooks without it"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "executables:" <<< "$OUT" && grep -q "kyberforge#1.5.0" <<< "$OUT" \
&& pass "shows the block to add" || fail "the failure must show the block to add"
# An `executables:` key that is not a mapping is the same hole as no key at all.
F="$(make_fixture "version: 1.5.0" "executables:")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when executables: exists but allow: does not" \
|| fail "an empty executables: block grants nothing and must fail"
# ---------------------------------------------------------------------------
echo ""
echo "--- allow present, kyberforge key missing ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$OTHER_PACKAGE_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when allow: exists but names no kyberforge key" \
|| fail "an allow block covering only other packages must still fail"
# ---------------------------------------------------------------------------
echo ""
echo "--- degenerate inputs fail loudly rather than passing silently ---"
# ---------------------------------------------------------------------------
run_gate "$REPO_ROOT/definitely-not-a-directory"
[[ $GATE_RC -ne 0 ]] && pass "fails on a nonexistent REPO_ROOT" \
|| fail "a bad path must not exit 0 — that reads as 'checked, in sync'"
# No executable-shipping plugin at all is the one legitimate no-op: nothing to pin.
NO_PLUGIN="$(mktemp -d "$RUN_TMP/fx.XXXXXX")"
echo "name: someone-else" > "$NO_PLUGIN/apm.yml"
run_gate "$NO_PLUGIN"
[[ $GATE_RC -eq 0 ]] && pass "no-ops in a repo with no executable-shipping plugin" \
|| fail "a repo without any plugins/*/.apm/hooks or bin has nothing to check"
# A plugin dir that ships nothing executable is not this gate's business.
mkdir -p "$NO_PLUGIN/plugins/kyberforge"
run_gate "$NO_PLUGIN"
[[ $GATE_RC -eq 0 ]] && pass "no-ops when plugins/kyberforge/ ships no hooks/ or bin/" \
|| fail "a plugin dir without hooks/ or bin/ has nothing to check"
# ...but one that ships executables with no manifest is drift, not a no-op.
mkdir -p "$NO_PLUGIN/plugins/kyberforge/.apm/hooks"
run_gate "$NO_PLUGIN"
[[ $GATE_RC -ne 0 ]] && pass "fails when plugins/kyberforge/ ships hooks but has no apm.yml" \
|| fail "a plugin dir with no manifest must not silently pass"
F="$(make_fixture "description: no version here" "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when the plugin manifest declares no version" \
|| fail "no version means nothing to compare — must fail, not pass"
# ---------------------------------------------------------------------------
echo ""
echo "--- several executable-shipping plugins ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "exits 0 when both shipping plugins have matching keys" \
|| fail "two in-sync packages should pass, got rc=$GATE_RC"
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when the second package's key is missing" \
|| fail "a shipping plugin with no allow key must fail"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "delivery#0.1.0" <<< "$OUT" && pass "names the second package's expected key" \
|| fail "the failure must state delivery#0.1.0"
grep -q "kyberforge#" <<< "$OUT" && fail "an in-sync kyberforge must not be reported" \
|| pass "does not report the package that is in sync"
F="$(make_fixture "version: 1.5.0" "$TWO_STALE_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && grep -q "delivery#0.1.0" <<< "$OUT" \
&& pass "reports every failing package in one run" \
|| fail "both stale packages must be named, got: $OUT"
grep -q "kyberforge#1.4.0" <<< "$OUT" && grep -q "delivery#0.0.9" <<< "$OUT" \
&& pass "quotes back each package's stale key" || fail "each stale key must be quoted back"
F="$(make_fixture "version: 1.5.0" "")"
add_plugin "$F" delivery "version: 0.1.0"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && grep -q "delivery#0.1.0" <<< "$OUT" \
&& pass "missing executables.allow lists every package's key to add" \
|| fail "the add-this block must name every shipping package"
# A plugin with no hooks/ or bin/ needs no key, whatever its version.
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
mkdir -p "$F/plugins/docs-only/.apm/skills"
printf 'name: docs-only\nversion: 9.9.9\n' > "$F/plugins/docs-only/apm.yml"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "ignores a plugin that ships no hooks/ or bin/" \
|| fail "a plugin without executables must not need an allow key"
# Keys use the package name, so the dir name and name: must agree.
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0" "delivery-renamed"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when a plugin's name: differs from its directory" \
|| fail "a dir/name mismatch must fail"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "delivery-renamed" <<< "$OUT" && pass "names the mismatched name:" \
|| fail "the failure must quote the mismatched name:"
# A 2-component version is a YAML float; the PyYAML reader refuses to guess.
F="$(make_fixture "version: 1.5" "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "refuses an unquoted 2-component version" \
|| fail "a float version must fail rather than be compared in reader-invented form"
# ---------------------------------------------------------------------------
echo ""
echo "--- the key is the full package identity apm 0.39 matches on ---"
# ---------------------------------------------------------------------------
# The 0.28-era bare key: apm 0.39.3 parks the hooks under it while root
# `apm audit --ci` stays green (spike #269), so this gate is what catches it.
F="$(make_fixture "version: 1.5.0" "$BARE_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails on a bare '<name>#<version>' key" \
|| fail "a bare key no longer matches under apm 0.39 and must fail"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -qF "$ID/kyberforge#1.5.0" <<< "$OUT" && pass "names the full-identity key to use" \
|| fail "the failure must state the full-identity key, got: $OUT"
grep -qF "full package identity" <<< "$OUT" && pass "says why a bare key no longer matches" \
|| fail "the failure must explain the bare key, got: $OUT"
# No git+path dependency entry for the plugin means no identity to derive.
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
sed -i '/path: plugins\/kyberforge/d' "$F/apm.yml"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when root apm.yml has no git+path entry for the plugin" \
|| fail "with no dependency entry the identity cannot be derived; must fail"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -qF "cannot be derived" <<< "$OUT" && pass "says the identity cannot be derived" \
|| fail "the failure must say why, got: $OUT"
# ---------------------------------------------------------------------------
echo ""
echo "--- the fallback reader agrees with the PyYAML reader ---"
# ---------------------------------------------------------------------------
# PyYAML is deliberately not a hard dependency of this gate (no other pre-push
# hook needs it), so the shape-scan fallback carries the same verdicts. Masking
# is done with a python3 stub whose `import yaml` fails, which is the exact
# condition on a machine that has python3 without PyYAML.
NO_YAML_BIN="$(mktemp -d "$RUN_TMP/fx.XXXXXX")"
printf '#!/usr/bin/env bash\nexit 1\n' > "$NO_YAML_BIN/python3"
chmod +x "$NO_YAML_BIN/python3"
run_fallback() {
GATE_RC=0
PATH="$NO_YAML_BIN:$PATH" bash "$SCRIPT" "$1" > /dev/null 2>&1 || GATE_RC=$?
}
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -eq 0 ]] && pass "fallback passes a matching fixture" || fail "fallback should pass when in sync"
F="$(make_fixture 'version: "1.5.0"' "$MATCHING_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -eq 0 ]] && pass "fallback strips quotes from the version" || fail "fallback mishandled a quoted version"
F="$(make_fixture "version: 1.5.0" "$STALE_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a stale key" || fail "fallback missed a stale key"
F="$(make_fixture "version: 1.5.0" "")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a missing executables block" || fail "fallback missed a missing block"
F="$(make_fixture "version: 1.5.0" "$OTHER_PACKAGE_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails when no kyberforge key is present" || fail "fallback missed an absent key"
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
run_fallback "$F"
[[ $GATE_RC -eq 0 ]] && pass "fallback passes two in-sync packages" || fail "fallback should pass two in-sync packages"
F="$(make_fixture "version: 1.5.0" "$TWO_STALE_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
OUT="$(PATH="$NO_YAML_BIN:$PATH" bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && grep -q "delivery#0.1.0" <<< "$OUT" \
&& pass "fallback reports both stale packages" || fail "fallback must name both stale packages"
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0" "delivery-renamed"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a dir/name mismatch" || fail "fallback missed a dir/name mismatch"
run_fallback "$REPO_ROOT"
[[ $GATE_RC -eq 0 ]] && pass "fallback passes the real repo" || fail "fallback disagrees with PyYAML on the real repo"
# Dependency entry shapes that name the same identity: no .git suffix, path
# first, quoted values, ./ prefix and trailing slash, and an scp-like URL. Each
# must pass under both readers.
identity_variant() {
local label="$1" entry="$2" f
f="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
python3 - "$f/apm.yml" "$entry" << 'PY2'
import sys
path, entry = sys.argv[1], sys.argv[2]
text = open(path).read()
old = " - git: https://git.example.invalid/Owner/Repo.git\n path: plugins/kyberforge\n"
assert old in text
open(path, "w").write(text.replace(old, entry + "\n"))
PY2
run_gate "$f"
[[ $GATE_RC -eq 0 ]] && pass "PyYAML reader derives the identity: $label" \
|| fail "PyYAML reader: $label should pass, got: $(bash "$SCRIPT" "$f" 2>&1 | head -2 || true)"
run_fallback "$f"
[[ $GATE_RC -eq 0 ]] && pass "fallback reader derives the identity: $label" \
|| fail "fallback reader: $label should pass"
}
identity_variant "no .git suffix" " - git: https://git.example.invalid/Owner/Repo
path: plugins/kyberforge"
identity_variant "path before git" " - path: plugins/kyberforge
git: https://git.example.invalid/Owner/Repo.git"
identity_variant "quoted values, ./ and trailing /" " - git: \"https://git.example.invalid/Owner/Repo.git\"
path: './plugins/kyberforge/'"
identity_variant "scp-like URL" " - git: [email protected]:Owner/Repo.git
path: plugins/kyberforge"
F="$(make_fixture "version: 1.5.0" "$BARE_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a bare key" || fail "fallback missed a bare key"
# ---------------------------------------------------------------------------
echo ""
echo "--- wired into the pre-push gate ---"
# ---------------------------------------------------------------------------
# A gate nobody runs is not a gate; this is the only assertion that the script
# is actually reachable from `git push`.
CONFIG="$REPO_ROOT/.pre-commit-config.yaml"
grep -q "id: check-executables-allow-sync" "$CONFIG" \
&& pass ".pre-commit-config.yaml declares the hook" || fail "hook is not declared in .pre-commit-config.yaml"
grep -q "scripts/check-executables-allow-sync.sh" "$CONFIG" \
&& pass ".pre-commit-config.yaml points at the script" || fail "hook does not reference the script path"
# ---------------------------------------------------------------------------
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ $FAIL -eq 0 ]]