#!/usr/bin/env bash set -euo pipefail # Tests for scripts/tag-plugin-releases.sh (ADR-0033). Every case builds its # own throwaway repo; none touches this repo's tags. REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" SCRIPT="$REPO_ROOT/scripts/tag-plugin-releases.sh" PASS=0 FAIL=0 pass() { echo " PASS: $1"; PASS=$((PASS + 1)); } fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); } if ! python3 -c 'import yaml' 2>/dev/null; then echo "SKIP: python3 with PyYAML is required" >&2 exit 77 fi # Fixtures must not inherit the caller's repo (see tests/run-tests.sh, #204). unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR # Output assertions use here-strings, never `echo | grep -q` (pipefail race; # see docs/spec/gates.md, Tests). CLEANUP_DIRS=() trap 'rm -rf ${CLEANUP_DIRS[@]+"${CLEANUP_DIRS[@]}"}' EXIT new_repo() { local dir dir="$(mktemp -d)" CLEANUP_DIRS+=("$dir") (cd "$dir" && git init -q -b main && git config user.email t@t.t && git config user.name t \ && git config tag.gpgSign false && git config commit.gpgSign false) echo "$dir" } # write_apm [name] [description] write_apm() { local repo="$1" dir="$2" ver="$3" name="${4:-$2}" desc="${5:-A plugin.}" mkdir -p "$repo/plugins/$dir" printf 'name: %s\nversion: %s\ndescription: %s\n' "$name" "$ver" "$desc" > "$repo/plugins/$dir/apm.yml" } commit() { (cd "$1" && git add -A && git commit -q -m "${2:-change}"); } head_sha() { git -C "$1" rev-parse HEAD; } # run [args...]: stdout+stderr, exit code in RC. run() { local dir="$1"; shift RC=0 OUT="$(cd "$dir" && bash "$SCRIPT" "$@" 2>&1)" || RC=$? } expect_rc() { local desc="$1" want="$2" if [[ "$RC" -eq "$want" ]]; then pass "$desc (exit $want)"; else fail "$desc: exit $RC, want $want"; echo "$OUT" | sed 's/^/ /'; fi } # expect_row expect_row() { local desc="$1" re re="^$2 +$3 +${4:0:7} +$5\$" if grep -Eq "$re" <<< "$OUT"; then pass "$desc"; else fail "$desc: no row '$2 $3 ${4:0:7} $5'"; echo "$OUT" | sed 's/^/ /'; fi } # expect_rows expect_rows() { local n n="$(grep -Ec "^$2 " <<< "$OUT" || true)" if [[ "$n" -eq "$3" ]]; then pass "$1"; else fail "$1: $n rows for $2, want $3"; echo "$OUT" | sed 's/^/ /'; fi } echo "Case 1: merge commit vs squash -- target is the commit on main's first-parent line" R="$(new_repo)" write_apm "$R" alpha 1.0.0; write_apm "$R" beta 1.0.0; commit "$R" init (cd "$R" && git checkout -q -b feat-a) write_apm "$R" alpha 1.1.0; commit "$R" "bump alpha" BRANCH_A="$(head_sha "$R")" (cd "$R" && git checkout -q main && git merge -q --no-ff -m "merge a" feat-a) MERGE_A="$(head_sha "$R")" (cd "$R" && git checkout -q -b feat-b) write_apm "$R" beta 1.1.0; commit "$R" "bump beta" (cd "$R" && git checkout -q main && git merge -q --squash feat-b > /dev/null && git commit -q -m "squash b") SQUASH_B="$(head_sha "$R")" run "$R" expect_rc "dry run with missing tags" 1 expect_row "merge: alpha 1.1.0 targets the merge commit" alpha 1.1.0 "$MERGE_A" MISSING expect_row "squash: beta 1.1.0 targets the squash commit" beta 1.1.0 "$SQUASH_B" MISSING if ! grep -q "${BRANCH_A:0:7}" <<< "$OUT"; then pass "feature-branch commit is never a target"; else fail "feature-branch commit named"; fi if grep -q "reading local main" <<< "$OUT"; then pass "falls back to local main with a staleness note"; else fail "no staleness note"; fi (cd "$R" && git checkout -q -b elsewhere && git checkout -q main) write_apm "$R" alpha 9.9.9 > /dev/null; # working-tree edit must not count run "$R" --ref main expect_row "reads the ref, not the working tree" alpha 1.1.0 "$MERGE_A" MISSING (cd "$R" && git checkout -q -- .) echo "Case 2: version goes X -> Y -> X -- only the current run is backfilled" R="$(new_repo)" write_apm "$R" gamma 1.0.0; commit "$R" a; FIRST="$(head_sha "$R")" write_apm "$R" gamma 1.1.0; commit "$R" b write_apm "$R" gamma 1.0.0; commit "$R" c; THIRD="$(head_sha "$R")" run "$R" --ref main expect_rc "X->Y->X dry run" 1 expect_row "target is the third run's commit" gamma 1.0.0 "$THIRD" MISSING expect_rows "exactly one wanted row" gamma 1 if ! grep -q "${FIRST:0:7}" <<< "$OUT"; then pass "first X run not named"; else fail "first X run named"; fi echo "Case 3: non-bump edit to apm.yml does not start a new run" R="$(new_repo)" write_apm "$R" delta 2.0.0; commit "$R" a; BUMP="$(head_sha "$R")" write_apm "$R" delta 2.0.0 delta "Reworded."; commit "$R" b; EDIT="$(head_sha "$R")" run "$R" --ref main expect_row "target stays on the bump commit" delta 2.0.0 "$BUMP" MISSING expect_rows "one row only" delta 1 if ! grep -q "${EDIT:0:7}" <<< "$OUT"; then pass "edit commit not a target"; else fail "edit commit named"; fi echo "Case 4: existing lightweight tag is an ERROR" (cd "$R" && git tag delta-v2.0.0 "$BUMP") run "$R" --ref main expect_rc "lightweight tag" 2 expect_row "row is ERROR" delta 2.0.0 "$BUMP" ERROR if grep -q "lightweight" <<< "$OUT"; then pass "message names lightweight"; else fail "no lightweight message"; fi echo "Case 5: annotated tag on the wrong commit is an ERROR" (cd "$R" && git tag -d delta-v2.0.0 > /dev/null && git tag -a delta-v2.0.0 -m x "$EDIT") run "$R" --ref main expect_rc "tag on a later commit" 2 expect_row "row is ERROR" delta 2.0.0 "$BUMP" ERROR if grep -q "peels to ${EDIT:0:7}" <<< "$OUT"; then pass "message names where it peels"; else fail "no peel message"; fi (cd "$R" && git tag -d delta-v2.0.0 > /dev/null && git tag -a delta-v2.0.0 -m x "$BUMP") run "$R" --ref main expect_rc "same tag on the right commit" 0 expect_row "row is OK" delta 2.0.0 "$BUMP" OK echo "Case 6: shallow clone exits 2" S="$(mktemp -d)"; CLEANUP_DIRS+=("$S") git clone -q --depth 1 "file://$R" "$S/c" 2>/dev/null run "$S/c" expect_rc "shallow clone" 2 if grep -q "shallow" <<< "$OUT"; then pass "message names shallow"; else fail "no shallow message"; fi echo "Case 7: apm.yml name differs from directory exits 2" R="$(new_repo)" write_apm "$R" eps 1.0.0 epsilon; commit "$R" a run "$R" --ref main expect_rc "name != dir" 2 if grep -q "must equal the directory" <<< "$OUT"; then pass "message names the mismatch"; else fail "no mismatch message"; fi echo "Case 7b: non-semver current version exits 2" R="$(new_repo)" write_apm "$R" zeta 1.0; commit "$R" a run "$R" --ref main expect_rc "version 1.0" 2 echo "Case 8: prefix-sharing names (git, gitea) never claim each other's tags" R="$(new_repo)" write_apm "$R" git 0.1.0; write_apm "$R" gitea 1.0.0; commit "$R" a; C1="$(head_sha "$R")" write_apm "$R" git 0.2.0; write_apm "$R" gitea 1.1.0; commit "$R" b; C2="$(head_sha "$R")" (cd "$R" && git tag -a gitea-v1.0.0 -m x "$C1") run "$R" --ref main expect_rows "git: gitea's tag is not its anchor -- current run only" git 1 expect_row "git 0.2.0 MISSING" git 0.2.0 "$C2" MISSING expect_rows "gitea: anchored at its own tag" gitea 2 expect_row "gitea 1.0.0 OK" gitea 1.0.0 "$C1" OK expect_row "gitea 1.1.0 MISSING" gitea 1.1.0 "$C2" MISSING run "$R" --ref main --plugin git expect_rows "--plugin git lists no gitea row" gitea 0 expect_rows "--plugin git lists git" git 1 echo "Case 9: every run after the newest tag is wanted" R="$(new_repo)" write_apm "$R" eta 1.0.0; commit "$R" a; V10="$(head_sha "$R")" write_apm "$R" eta 1.1.0; commit "$R" b; V11="$(head_sha "$R")" write_apm "$R" eta 1.2.0; commit "$R" c; V12="$(head_sha "$R")" write_apm "$R" eta 1.3.0; commit "$R" d; V13="$(head_sha "$R")" (cd "$R" && git tag -a eta-v1.1.0 -m x "$V11") run "$R" --ref main expect_rc "later runs missing" 1 expect_row "anchor 1.1.0 OK" eta 1.1.0 "$V11" OK expect_row "1.2.0 MISSING" eta 1.2.0 "$V12" MISSING expect_row "1.3.0 MISSING" eta 1.3.0 "$V13" MISSING expect_rows "older 1.0.0 not wanted" eta 3 if ! grep -q "${V10:0:7}" <<< "$OUT"; then pass "1.0.0 commit not named"; else fail "1.0.0 named"; fi echo "Case 10: --apply creates annotated tags, prints explicit push line, is idempotent" run "$R" --ref main --apply expect_rc "--apply" 0 for pair in "1.2.0:$V12" "1.3.0:$V13"; do v="${pair%%:*}"; sha="${pair#*:}" if [[ "$(git -C "$R" cat-file -t "refs/tags/eta-v$v" 2>/dev/null)" == tag ]]; then pass "eta-v$v is annotated"; else fail "eta-v$v not annotated"; fi if [[ "$(git -C "$R" rev-parse "eta-v$v^{commit}" 2>/dev/null)" == "$sha" ]]; then pass "eta-v$v peels to its target"; else fail "eta-v$v wrong target"; fi done if [[ "$(git -C "$R" tag -l --format='%(contents:subject)' eta-v1.3.0)" == "eta 1.3.0" ]]; then pass "tag message is '

'"; else fail "tag message wrong"; fi if grep -qx "git push origin eta-v1.2.0 eta-v1.3.0" <<< "$OUT"; then pass "push line names each tag"; else fail "push line wrong"; echo "$OUT" | sed 's/^/ /'; fi if ! grep -q -- "--tags" <<< "$OUT"; then pass "push line never uses --tags"; else fail "--tags printed"; fi run "$R" --ref main --apply expect_rc "second --apply" 0 if ! grep -q "git push" <<< "$OUT"; then pass "second --apply creates nothing"; else fail "second --apply printed a push line"; fi run "$R" --ref main expect_rc "dry run after --apply" 0 echo "Case 11: --apply is all-or-nothing" R="$(new_repo)" write_apm "$R" theta 1.0.0; write_apm "$R" iota 1.0.0; commit "$R" a; A="$(head_sha "$R")" write_apm "$R" iota 1.0.0 iota "Reworded."; commit "$R" b (cd "$R" && git tag iota-v1.0.0 "$A") run "$R" --ref main --apply expect_rc "ERROR blocks --apply" 2 if [[ -z "$(git -C "$R" tag -l 'theta-v*')" ]]; then pass "nothing created for the clean plugin"; else fail "theta tag created despite ERROR"; fi echo "Case 12: version recurring inside the wanted set is an ERROR" R="$(new_repo)" write_apm "$R" kappa 0.9.0; commit "$R" a; K0="$(head_sha "$R")" write_apm "$R" kappa 1.0.0; commit "$R" b write_apm "$R" kappa 1.1.0; commit "$R" c write_apm "$R" kappa 1.0.0; commit "$R" d (cd "$R" && git tag -a kappa-v0.9.0 -m x "$K0") run "$R" --ref main expect_rc "recurring version" 2 if grep -q "recurs" <<< "$OUT"; then pass "message names the recurrence"; else fail "no recurrence message"; fi echo "Case 13: bad input exits 2" run "$R" --ref no-such-ref expect_rc "unresolvable --ref" 2 run "$R" --ref main --plugin nope expect_rc "unknown --plugin" 2 run "$R" --bogus expect_rc "unknown flag" 2 R="$(new_repo)" write_apm "$R" lam 1.0.0; commit "$R" a (cd "$R" && git branch -q -m trunk) run "$R" expect_rc "no origin/main or main" 2 echo echo "Results: $PASS passed, $FAIL failed" [[ $FAIL -eq 0 ]]