PR #95's review of the issue #90 apm-conversion work found several defects in scripts/sync-plugin-content.sh and the gate wired to it: - --check claimed never to mutate the plugin root, but apm pack still wrote .claude-plugin/plugin.json and .github/plugin/plugin.json into the real plugin_dir on first-time creation. --check now packs a throwaway copy instead. - check-plugin-content-sync hardcoded the six plugin directories instead of deriving them the way check-manifests.sh already does. Added an --all flag that parses .claude-plugin/marketplace.json, and simplified the pre-commit hook to use it. - A missing plugin_dir and one that legitimately has no .apm/ yet both reported SKIP/success; a missing directory now FAILs. - The dispatch loop backgrounded every plugin with no concurrency cap, unlike the JOBS-bounded pattern this same PR added to tests/run-bats.sh and tests/run-tests.sh. Added the same bash-3.2-safe getconf + batched-wait cap here for consistency. - Per-plugin scratch/log/status files were keyed only by basename, with no collision guard across arguments; added a fail-fast check. - sync_hooks_json()'s trailing-newline normalization was duplicated between its --check and write branches; factored into one helper. - tests/test-sync-plugin-content.sh set two competing `trap ... EXIT` statements, so the first (cleaning up $FIXTURE) was silently replaced by the second and its tmp dir leaked every run. Adopted the track()/CLEANUP_DIRS pattern already used in tests/test-check-release-needed.sh. Separately: apm's Copilot-ecosystem plugin.json builder unconditionally strips mcpServers, citing (in its own docstring) that the field is out of schema for Copilot -- a claim this repo's own researched Copilot plugin schema docs contradict. reinject_mcp_servers() narrowly restores it from the plugin's .mcp.json on real syncs only, regenerating plugins/bin/.github/plugin/plugin.json (the only plugin that currently declares any MCP servers). Documented as an amendment to ADR-0017, since it's a deliberate, narrow exception to that ADR's rejection of patching apm's compiled output -- apm's premise for stripping skills/agents/commands/hooks pointers is still accurate; its premise for stripping mcpServers is not. All 12 assertions in tests/test-sync-plugin-content.sh pass individually, plus 5 new regression tests added for this round; the full bats and shell-script suites are green; shellcheck is clean. Refs: #95 ADR: 0017
242 lines
7.6 KiB
Bash
Executable File
242 lines
7.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
SCRIPT="$REPO_ROOT/scripts/sync-plugin-content.sh"
|
|
PASS=0
|
|
FAIL=0
|
|
|
|
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
|
|
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
|
|
|
|
if ! command -v apm &>/dev/null; then
|
|
echo "apm not installed -- skipping (see kyberforge:apm-install)" >&2
|
|
exit 77
|
|
fi
|
|
|
|
# Minimal fixture: one skill (with a tests/ fixture that must NOT be mirrored), one
|
|
# agent, one hooks.json -- enough to exercise every mirrored category
|
|
# (scripts/sync-plugin-content.sh's MIRROR_DIRS plus hooks.json) without needing
|
|
# network access (no apm.yml dependencies).
|
|
make_fixture() {
|
|
local dir
|
|
dir="$(mktemp -d)"
|
|
mkdir -p "$dir/.apm/skills/hello/tests" "$dir/.apm/agents" "$dir/.apm/hooks"
|
|
cat > "$dir/apm.yml" <<'YAML'
|
|
name: fixture
|
|
version: 0.0.1
|
|
description: fixture
|
|
license: MIT
|
|
type: hybrid
|
|
targets:
|
|
- claude
|
|
dependencies:
|
|
apm: []
|
|
mcp: []
|
|
includes: auto
|
|
devDependencies:
|
|
apm: []
|
|
scripts: {}
|
|
YAML
|
|
cat > "$dir/.apm/skills/hello/SKILL.md" <<'EOF'
|
|
---
|
|
name: hello
|
|
description: hello
|
|
---
|
|
Hello.
|
|
EOF
|
|
cat > "$dir/.apm/skills/hello/tests/sample.bats" <<'EOF'
|
|
@test "dummy" { true; }
|
|
EOF
|
|
cat > "$dir/.apm/agents/foo.agent.md" <<'EOF'
|
|
---
|
|
name: foo
|
|
description: foo
|
|
---
|
|
Foo.
|
|
EOF
|
|
cat > "$dir/.apm/hooks/hooks.json" <<'EOF'
|
|
{"hooks": {"PreToolUse": []}}
|
|
EOF
|
|
echo "$dir"
|
|
}
|
|
|
|
# Same base fixture, but with a copilot target (so apm pack produces
|
|
# .github/plugin/plugin.json) and a caller-supplied .mcp.json -- for exercising
|
|
# reinject_mcp_servers().
|
|
make_fixture_with_mcp() {
|
|
local mcp_json="$1" dir
|
|
dir="$(mktemp -d)"
|
|
mkdir -p "$dir/.apm/skills/hello" "$dir/.apm/agents"
|
|
cat > "$dir/apm.yml" <<'YAML'
|
|
name: fixture
|
|
version: 0.0.1
|
|
description: fixture
|
|
license: MIT
|
|
type: hybrid
|
|
targets:
|
|
- claude
|
|
- copilot
|
|
dependencies:
|
|
apm: []
|
|
mcp: []
|
|
includes: auto
|
|
devDependencies:
|
|
apm: []
|
|
scripts: {}
|
|
YAML
|
|
cat > "$dir/.apm/skills/hello/SKILL.md" <<'EOF'
|
|
---
|
|
name: hello
|
|
description: hello
|
|
---
|
|
Hello.
|
|
EOF
|
|
cat > "$dir/.apm/agents/foo.agent.md" <<'EOF'
|
|
---
|
|
name: foo
|
|
description: foo
|
|
---
|
|
Foo.
|
|
EOF
|
|
printf '%s' "$mcp_json" > "$dir/.mcp.json"
|
|
echo "$dir"
|
|
}
|
|
|
|
CLEANUP_DIRS=()
|
|
trap 'rm -rf "${CLEANUP_DIRS[@]}"' EXIT
|
|
track() { CLEANUP_DIRS+=("$1"); }
|
|
|
|
# --- 1. --check reports drift before any sync has run ---
|
|
echo ""
|
|
echo "--- --check reports drift on an unsynced fixture ---"
|
|
FIXTURE="$(make_fixture)"; track "$FIXTURE"
|
|
if bash "$SCRIPT" --check "$FIXTURE" > /dev/null 2>&1; then
|
|
fail "exited 0 on an unsynced fixture — expected drift (exit 1)"
|
|
else
|
|
pass "exits non-zero (drift) before syncing"
|
|
fi
|
|
|
|
# --- 2. A real sync creates the flat mirror and exits 0 ---
|
|
echo ""
|
|
echo "--- real sync creates skills/, agents/, hooks.json ---"
|
|
if bash "$SCRIPT" "$FIXTURE" > /dev/null 2>&1 \
|
|
&& [[ -f "$FIXTURE/skills/hello/SKILL.md" ]] \
|
|
&& [[ -f "$FIXTURE/agents/foo.agent.md" ]] \
|
|
&& [[ -f "$FIXTURE/hooks.json" ]]; then
|
|
pass "sync creates the expected flat mirror"
|
|
else
|
|
fail "sync did not create the expected flat mirror"
|
|
fi
|
|
|
|
# --- 3. tests/ fixtures are excluded from the mirror ---
|
|
echo ""
|
|
echo "--- tests/ subdirectories are not mirrored ---"
|
|
if [[ ! -e "$FIXTURE/skills/hello/tests" ]]; then
|
|
pass "skills/hello/tests/ was not copied into the mirror"
|
|
else
|
|
fail "skills/hello/tests/ was copied into the mirror — should be excluded"
|
|
fi
|
|
|
|
# --- 4. --check is clean immediately after a real sync ---
|
|
echo ""
|
|
echo "--- --check is clean right after syncing ---"
|
|
if bash "$SCRIPT" --check "$FIXTURE" > /dev/null 2>&1; then
|
|
pass "no drift reported immediately after syncing"
|
|
else
|
|
fail "drift reported right after syncing — sync and check disagree"
|
|
fi
|
|
|
|
# --- 5. New .apm/ content is detected as drift, and a re-sync clears it ---
|
|
echo ""
|
|
echo "--- new .apm/ content is detected as drift and cleared by re-sync ---"
|
|
mkdir -p "$FIXTURE/.apm/skills/second"
|
|
cat > "$FIXTURE/.apm/skills/second/SKILL.md" <<'EOF'
|
|
---
|
|
name: second
|
|
description: second
|
|
---
|
|
Second.
|
|
EOF
|
|
if bash "$SCRIPT" --check "$FIXTURE" > /dev/null 2>&1; then
|
|
fail "no drift reported after adding a new skill under .apm/ — expected drift"
|
|
else
|
|
pass "new .apm/ content is detected as drift"
|
|
bash "$SCRIPT" "$FIXTURE" > /dev/null 2>&1
|
|
if bash "$SCRIPT" --check "$FIXTURE" > /dev/null 2>&1; then
|
|
pass "re-sync clears the drift"
|
|
else
|
|
fail "re-sync did not clear the drift"
|
|
fi
|
|
fi
|
|
|
|
# --- 6. A plugin dir with no .apm/ is skipped cleanly, not treated as an error ---
|
|
echo ""
|
|
echo "--- a plugin dir with no .apm/ is skipped, not failed ---"
|
|
NO_APM="$(mktemp -d)"; track "$NO_APM"
|
|
if bash "$SCRIPT" "$NO_APM" > /dev/null 2>&1 && bash "$SCRIPT" --check "$NO_APM" > /dev/null 2>&1; then
|
|
pass "a plugin dir with no .apm/ exits 0 in both real and --check mode"
|
|
else
|
|
fail "a plugin dir with no .apm/ should exit 0 (skip), not fail"
|
|
fi
|
|
|
|
# --- 7. A plugin dir that doesn't exist at all is a hard failure, not a skip ---
|
|
echo ""
|
|
echo "--- a plugin dir that does not exist fails, distinct from an existing-but-empty one ---"
|
|
MISSING_ROOT="$(mktemp -d)"; track "$MISSING_ROOT"
|
|
MISSING="$MISSING_ROOT/does-not-exist"
|
|
if bash "$SCRIPT" "$MISSING" > /dev/null 2>&1; then
|
|
fail "exited 0 for a plugin dir that does not exist — expected a hard failure"
|
|
else
|
|
pass "a nonexistent plugin dir fails instead of silently skipping"
|
|
fi
|
|
|
|
# --- 8. --check never mutates the real plugin root, even on first-time manifest creation ---
|
|
echo ""
|
|
echo "--- --check does not create .claude-plugin/plugin.json or .github/plugin/plugin.json ---"
|
|
FIXTURE8="$(make_fixture)"; track "$FIXTURE8"
|
|
bash "$SCRIPT" --check "$FIXTURE8" > /dev/null 2>&1 || true
|
|
if [[ ! -e "$FIXTURE8/.claude-plugin/plugin.json" ]] && [[ ! -e "$FIXTURE8/.github/plugin/plugin.json" ]]; then
|
|
pass "--check leaves the real plugin root without a first-write plugin.json"
|
|
else
|
|
fail "--check created plugin.json in the real plugin root — it must never mutate it"
|
|
fi
|
|
|
|
# --- 9. A duplicate plugin-dir basename among arguments fails fast, not silently ---
|
|
echo ""
|
|
echo "--- duplicate plugin dir basenames among arguments are rejected ---"
|
|
DUP_PARENT_A="$(mktemp -d)"; track "$DUP_PARENT_A"
|
|
DUP_PARENT_B="$(mktemp -d)"; track "$DUP_PARENT_B"
|
|
mkdir -p "$DUP_PARENT_A/dup" "$DUP_PARENT_B/dup"
|
|
if bash "$SCRIPT" "$DUP_PARENT_A/dup" "$DUP_PARENT_B/dup" > /dev/null 2>&1; then
|
|
fail "exited 0 with two plugin-dir arguments sharing a basename — expected a collision error"
|
|
else
|
|
pass "rejects two plugin-dir arguments that share a basename"
|
|
fi
|
|
|
|
# --- 10. Real sync re-injects mcpServers that apm's Copilot builder strips ---
|
|
echo ""
|
|
echo "--- real sync re-injects mcpServers into .github/plugin/plugin.json ---"
|
|
FIXTURE10="$(make_fixture_with_mcp '{"mcpServers":{"demo":{"command":"demo-server","type":"stdio"}}}')"; track "$FIXTURE10"
|
|
bash "$SCRIPT" "$FIXTURE10" > /dev/null 2>&1
|
|
if jq -e '.mcpServers.demo.command == "demo-server"' "$FIXTURE10/.github/plugin/plugin.json" > /dev/null 2>&1; then
|
|
pass "mcpServers from .mcp.json is present in .github/plugin/plugin.json after a real sync"
|
|
else
|
|
fail "mcpServers was not re-injected into .github/plugin/plugin.json"
|
|
fi
|
|
|
|
# --- 11. An empty .mcp.json does not add a redundant mcpServers: {} ---
|
|
echo ""
|
|
echo "--- an empty .mcp.json does not add mcpServers: {} ---"
|
|
FIXTURE11="$(make_fixture_with_mcp '{"mcpServers":{}}')"; track "$FIXTURE11"
|
|
bash "$SCRIPT" "$FIXTURE11" > /dev/null 2>&1
|
|
if jq -e 'has("mcpServers") | not' "$FIXTURE11/.github/plugin/plugin.json" > /dev/null 2>&1; then
|
|
pass "an empty .mcp.json does not add mcpServers to .github/plugin/plugin.json"
|
|
else
|
|
fail "an empty .mcp.json still added mcpServers -- should match apm's own omit-when-empty convention"
|
|
fi
|
|
|
|
echo ""
|
|
echo "Results: $PASS passed, $FAIL failed"
|
|
[[ $FAIL -eq 0 ]]
|