返回 CodeWhale
ensure-release-on-main.test.sh
根目录 / scripts / release / ensure-release-on-main.test.sh
1 #!/usr/bin/env bash
2 # Hermetic test for scripts/release/ensure-release-on-main.sh.
3 set -euo pipefail
4
5 script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6 guard="${script_dir}/ensure-release-on-main.sh"
7
8 work="$(mktemp -d)"
9 cleanup() { rm -rf "${work}"; }
10 trap cleanup EXIT
11
12 fail=0
13 check() {
14 local desc="$1" needle="$2" hay
15 hay="$(cat)"
16 if grep -qF -- "${needle}" <<<"${hay}"; then
17 echo "ok - ${desc}"
18 else
19 echo "FAIL - ${desc}"
20 echo " expected to find: ${needle}"
21 echo "------ output ------"
22 echo "${hay}"
23 echo "--------------------"
24 fail=1
25 fi
26 }
27
28 mkdir -p "${work}/repo/scripts/release"
29 cp "${guard}" "${work}/repo/scripts/release/ensure-release-on-main.sh"
30 guard="${work}/repo/scripts/release/ensure-release-on-main.sh"
31
32 cd "${work}/repo"
33 export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null
34 git init -q -b main .
35 git config user.name "Release Test"
36 git config user.email "release-test@example.com"
37
38 commit() {
39 echo "$2" >"$1"
40 git add -A
41 git commit -q -m "$3"
42 }
43
44 commit README.md "base" "base"
45 base_sha="$(git rev-parse HEAD)"
46
47 git init -q --bare "${work}/origin.git"
48 git remote add origin "${work}/origin.git"
49 git push -q -u origin main
50
51 # Prove the guard fetches origin/main when the remote-tracking ref is absent.
52 git update-ref -d refs/remotes/origin/main
53 base_out="$(bash "${guard}" "${base_sha}" 2>&1)"
54 check "main commit is accepted after fetching origin/main" \
55 "is reachable from origin/main" <<<"${base_out}"
56
57 git switch -q -c release-only
58 commit release.txt "branch-only" "branch-only release work"
59 release_sha="$(git rev-parse HEAD)"
60
61 set +e
62 reject_out="$(bash "${guard}" "${release_sha}" 2>&1)"
63 reject_ec=$?
64 set -e
65 check "branch-only commit is rejected" \
66 "is not reachable from origin/main" <<<"${reject_out}"
67 if [[ "${reject_ec}" -ne 1 ]]; then
68 echo "FAIL - branch-only commit should exit 1, got ${reject_ec}"
69 fail=1
70 else
71 echo "ok - branch-only commit exits non-zero"
72 fi
73
74 git switch -q main
75 git merge -q --ff-only release-only
76 git push -q origin main
77 git update-ref -d refs/remotes/origin/main
78 merged_out="$(bash "${guard}" "${release_sha}" 2>&1)"
79 check "commit is accepted after release branch lands on main" \
80 "is reachable from origin/main" <<<"${merged_out}"
81
82 echo
83 if [[ "${fail}" -eq 0 ]]; then
84 echo "ensure-release-on-main.test.sh: all checks passed"
85 else
86 echo "ensure-release-on-main.test.sh: FAILURES above"
87 fi
88 exit "${fail}"
89
89 lines BASH