test-scripts.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #!/usr/bin/env bash
  2. # 单测:should-upgrade.sh(版本比较)、upgrade-dsh.sh(文件更新)、open-issue.sh(失败 Issue)、
  3. # check-issue-gate.sh(失败 Issue 门闩)、close-stale-issues.sh(过时 Issue 清理)、
  4. # check-pr-gate.sh(自动升级 PR 门闩)与 push-upgrade-pr.sh 的 upsert_pr(PR 创建/更新)
  5. # git 分支准备与推送部分难 mock,由 CI 真机验证
  6. #
  7. # Unit tests: should-upgrade.sh (version comparison), upgrade-dsh.sh (file updates), open-issue.sh
  8. # (failure Issue), check-issue-gate.sh (failure Issue gate), close-stale-issues.sh (stale Issue cleanup),
  9. # check-pr-gate.sh (auto-upgrade PR gate), and push-upgrade-pr.sh's upsert_pr (PR create/update)
  10. # The git branch prep and push parts are hard to mock and are verified live on CI
  11. # Usage: tests/test-scripts.sh
  12. set -euo pipefail
  13. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  14. TMP="$(mktemp -d)"
  15. trap 'rm -rf "$TMP"' EXIT
  16. PASS=0
  17. FAIL=0
  18. # 断言辅助
  19. #
  20. # assertion helpers
  21. assert_eq() { # <desc> <expected> <actual>
  22. if [ "$2" = "$3" ]; then PASS=$((PASS+1)); echo " ✓ $1"; else
  23. FAIL=$((FAIL+1)); echo " ✗ $1:期望 [$2] 实际 [$3]"
  24. fi
  25. }
  26. assert_grep() { # <desc> <pattern> <file>(期望存在匹配)
  27. if grep -q "$2" "$3"; then PASS=$((PASS+1)); echo " ✓ $1"; else
  28. FAIL=$((FAIL+1)); echo " ✗ $1:$3 未匹配 $2"
  29. fi
  30. }
  31. assert_no_grep() { # <desc> <pattern> <file>(期望无匹配)
  32. if grep -q "$2" "$3"; then FAIL=$((FAIL+1)); echo " ✗ $1:$3 意外匹配 $2"; else
  33. PASS=$((PASS+1)); echo " ✓ $1"
  34. fi
  35. }
  36. echo "== should-upgrade.sh(版本比较)=="
  37. SU="$ROOT/scripts/should-upgrade.sh"
  38. assert_eq "相等版本不升级" 0 "$("$SU" 0.1.0-rc.6 0.1.0-rc.6)"
  39. assert_eq "rc → 正式版应升级" 1 "$("$SU" 0.1.0-rc.6 0.1.0)"
  40. assert_eq "patch 升级应升级" 1 "$("$SU" 0.1.0 0.1.1)"
  41. assert_eq "minor 升级应升级" 1 "$("$SU" 0.1.0 0.2.0)"
  42. assert_eq "上游回退(降级)不升级" 0 "$("$SU" 0.1.0-rc.6 0.1.0-rc.5)"
  43. assert_eq "正式版后的 rc 不升级" 0 "$("$SU" 0.1.0 0.1.0-rc.9)"
  44. # 同元组更高 rc 原本就能过;跨元组 prerelease 才是 range 默认规则的盲区
  45. # (>0.1.0-rc.7 不匹配 0.1.1-rc.2,正是自动升级卡住的原因)
  46. #
  47. # A higher rc in the same tuple already passed; cross-tuple prereleases are the
  48. # range default's blind spot (>0.1.0-rc.7 does not match 0.1.1-rc.2 — why auto-upgrade stalled)
  49. assert_eq "同元组更高 rc 应升级" 1 "$("$SU" 0.1.0-rc.7 0.1.0-rc.8)"
  50. assert_eq "跨元组 prerelease 应升级" 1 "$("$SU" 0.1.0-rc.7 0.1.1-rc.2)"
  51. assert_eq "跨元组 alpha 应升级" 1 "$("$SU" 0.1.0-rc.7 0.1.2-alpha.1)"
  52. echo "== upgrade-dsh.sh(文件更新)=="
  53. # fixture:直接从仓库复制当前文件(模拟"升级前"状态)
  54. #
  55. # fixtures: copy the current repo files directly (simulating the "pre-upgrade" state)
  56. for f in Dockerfile docker-compose.yml README.md README.zh.md DESIGN.md; do
  57. cp "$ROOT/$f" "$TMP/"
  58. done
  59. # 当前版本号从 fixture Dockerfile 动态提取:升级脚本可能升级到任意上游版本,
  60. # 硬编码版本会让「旧版本清除」断言失效或空转
  61. #
  62. # Current version is read from the fixture Dockerfile: the upgrade script may bump to any
  63. # upstream version, so hardcoding one would make the "old version gone" asserts stale or vacuous
  64. CUR_VERSION="$(grep -E '^ARG DSH_VERSION=' "$TMP/Dockerfile" | head -n1 | cut -d= -f2)"
  65. ESC_VERSION="$(printf '%s' "$CUR_VERSION" | sed 's/[][\\^$.|*+?()]/\\&/g')"
  66. "$ROOT/scripts/upgrade-dsh.sh" 0.2.0 "$TMP" >/dev/null
  67. assert_no_grep "Dockerfile 旧版本已清除" "$ESC_VERSION" "$TMP/Dockerfile"
  68. assert_grep "Dockerfile 新版本已写入" "0\.2\.0" "$TMP/Dockerfile"
  69. assert_no_grep "docker-compose.yml 旧版本清除" "$ESC_VERSION" "$TMP/docker-compose.yml"
  70. assert_grep "docker-compose.yml 新版本写入" "0\.2\.0" "$TMP/docker-compose.yml"
  71. assert_no_grep "README.md 旧版本已清除" "$ESC_VERSION" "$TMP/README.md"
  72. assert_no_grep "README.zh.md 旧版本已清除" "$ESC_VERSION" "$TMP/README.zh.md"
  73. # DESIGN.md 是历史设计稿,不在升级范围:升级前后内容必须完全一致
  74. # (不依赖任何版本号,避免历史引用随版本演进失效)
  75. #
  76. # DESIGN.md is a historical design doc, out of the upgrade scope: its content must be
  77. # byte-identical after an upgrade (independent of any version number)
  78. assert_eq "DESIGN.md 不受影响" "$(md5sum < "$ROOT/DESIGN.md")" "$(md5sum < "$TMP/DESIGN.md")"
  79. echo "== upgrade-dsh.sh(版本边界:历史引用不被误改)=="
  80. TMP3="$TMP/boundary"
  81. mkdir -p "$TMP3"
  82. for f in Dockerfile README.md README.zh.md; do
  83. printf 'ARG DSH_VERSION=0.1.0\n' > "$TMP3/$f"
  84. done
  85. # shellcheck disable=SC2016 # 单引号是有意的:fixture 需要字面 ${DSH_VERSION:-0.1.0}
  86. printf 'DSH_VERSION: ${DSH_VERSION:-0.1.0}\n' > "$TMP3/docker-compose.yml"
  87. printf '历史版本参考 0.1.0-rc.6 仅供说明\n' >> "$TMP3/README.md"
  88. "$ROOT/scripts/upgrade-dsh.sh" 0.1.1 "$TMP3" >/dev/null
  89. assert_grep "新版本 0.1.1 已写入" "ARG DSH_VERSION=0\.1\.1" "$TMP3/README.md"
  90. assert_grep "compose 精确上下文已替换" "DSH_VERSION: \${DSH_VERSION:-0\.1\.1}" "$TMP3/docker-compose.yml"
  91. # 独立的 0.1.0 必须消失(版本边界匹配;0.1.0-rc.6 内的子串不算)
  92. #
  93. # standalone 0.1.0 must be gone (matched at version boundaries; the substring inside 0.1.0-rc.6 doesn't count)
  94. assert_no_grep "独立 0.1.0 全部替换" "\(^\|[^-0-9A-Za-z.]\)0\.1\.0\([^-0-9A-Za-z.]\|$\)" "$TMP3/README.md"
  95. assert_grep "历史引用 0.1.0-rc.6 保留" "0\.1\.0-rc\.6" "$TMP3/README.md"
  96. echo "== upgrade-dsh.sh(同版本幂等)=="
  97. TMP2="$TMP/idempotent"
  98. mkdir -p "$TMP2"
  99. for f in Dockerfile docker-compose.yml README.md README.zh.md; do
  100. cp "$ROOT/$f" "$TMP2/"
  101. done
  102. # 以 fixture 中的当前版本为参数:同版本必须走「无需升级」分支(版本号动态提取,
  103. # 与 upgrade-dsh.sh 内部读取 Dockerfile 的方式一致,避免硬编码随上游升级失配)
  104. #
  105. # Pass the fixture's own current version: an equal version must take the "no upgrade needed"
  106. # branch. The version is read the same way upgrade-dsh.sh reads the Dockerfile, so the test
  107. # cannot drift when the upstream version bumps
  108. CUR_VERSION="$(grep -E '^ARG DSH_VERSION=' "$TMP2/Dockerfile" | head -n1 | cut -d= -f2)"
  109. "$ROOT/scripts/upgrade-dsh.sh" "$CUR_VERSION" "$TMP2" > "$TMP2/idempotent.out" 2>&1 || true
  110. assert_eq "同版本返回提示且不报错" 0 "$?"
  111. assert_grep "同版本提示输出" "无需升级" "$TMP2/idempotent.out"
  112. echo "== open-issue.sh (mock gh)=="
  113. MOCK_GH="$TMP/mock-gh"
  114. cat > "$MOCK_GH" <<'MOCK'
  115. #!/usr/bin/env bash
  116. echo "$*" >> "${MOCK_LOG:?}"
  117. case "$1 $2" in
  118. "issue list")
  119. if [[ "$*" == *"-q"* ]]; then
  120. # 仿真 gh 的 -q jq 查询:[] → 空输出;[{"number":7}] → 7
  121. python3 -c "import json,sys; d=json.load(sys.stdin); print(d[0]['number'] if d else '')" <<< "${MOCK_LIST_OUT-}"
  122. else
  123. # 原始 JSON 输出(gate/close 脚本自行 jq 解析);未设置时输出空 = 仿真 gh 失败
  124. echo "${MOCK_LIST_OUT-}"
  125. fi
  126. ;;
  127. "issue create") echo "created" ;;
  128. "issue close") echo "closed $3" ;;
  129. "pr list") echo "${MOCK_LIST_OUT-}" ;;
  130. "pr view") echo "${MOCK_PR_STATE-}" ;;
  131. "pr create") echo "created" ;;
  132. "pr edit") echo "edited" ;;
  133. esac
  134. MOCK
  135. chmod +x "$MOCK_GH"
  136. OPEN_ISSUE="$ROOT/scripts/open-issue.sh"
  137. MOCK_LOG="$TMP/mock1.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
  138. "$OPEN_ISSUE" 0.2.0 0.1.0-rc.6 > "$TMP/issue1.out" 2>&1
  139. assert_grep "无已有 Issue 时创建" "已创建 Issue" "$TMP/issue1.out"
  140. assert_grep "create 调用含新版本标题" "issue create --title \[自动升级\] dsh 上游 0.2.0" "$TMP/mock1.log"
  141. assert_grep "create 标题为通用失败措辞" "升级流程失败" "$TMP/mock1.log"
  142. assert_grep "create 调用含上游版本正文" "0.2.0" "$TMP/mock1.log"
  143. MOCK_LOG="$TMP/mock2.log" MOCK_LIST_OUT='[{"number":7}]' GH="$MOCK_GH" \
  144. "$OPEN_ISSUE" 0.2.0 0.1.0-rc.6 > "$TMP/issue2.out" 2>&1
  145. assert_grep "已有同版本 Issue 时跳过" "已存在同版本 Issue #7" "$TMP/issue2.out"
  146. assert_no_grep "去重时不调用 create" "issue create" "$TMP/mock2.log"
  147. # GitHub Actions 兜底:仅设 GITHUB_TOKEN 时,脚本应将其映射为 GH_TOKEN 并透传给 gh
  148. # (upstream-check.yml 早期版本漏配 env,导致 exit 4 的根因)
  149. #
  150. # GitHub Actions fallback: when only GITHUB_TOKEN is set, the script must surface it as
  151. # GH_TOKEN to gh. (This is the root cause of the upstream-check.yml exit-4 failure before
  152. # the env was wired up)
  153. GH_TOKEN_GUARD="$TMP/gh-token-guard"
  154. cat > "$GH_TOKEN_GUARD" <<'GUARD'
  155. #!/usr/bin/env bash
  156. echo "GH_TOKEN=${GH_TOKEN-unset}" >> "${GUARD_LOG:?}"
  157. exec "$GH_REAL" "$@"
  158. GUARD
  159. chmod +x "$GH_TOKEN_GUARD"
  160. GUARD_LOG="$TMP/mock_guard.log" MOCK_LOG="$TMP/mock_guard_gh.log" MOCK_LIST_OUT='[]' \
  161. GH="$GH_TOKEN_GUARD" GH_REAL="$MOCK_GH" \
  162. GITHUB_TOKEN=ghp_ci_fallback \
  163. "$OPEN_ISSUE" 0.2.0 0.1.0-rc.6 > "$TMP/issue_guard.out" 2>&1
  164. assert_grep "GITHUB_TOKEN 兜底为 GH_TOKEN" "GH_TOKEN=ghp_ci_fallback" "$TMP/mock_guard.log"
  165. # 显式 GH_TOKEN 应优先于 GITHUB_TOKEN(避免 CI 中被无关变量意外覆盖)
  166. #
  167. # Explicit GH_TOKEN must win over GITHUB_TOKEN (so unrelated CI vars cannot clobber it)
  168. GUARD_LOG="$TMP/mock_guard2.log" MOCK_LOG="$TMP/mock_guard2_gh.log" MOCK_LIST_OUT='[]' \
  169. GH="$GH_TOKEN_GUARD" GH_REAL="$MOCK_GH" \
  170. GH_TOKEN=ghp_explicit GITHUB_TOKEN=ghp_should_lose \
  171. "$OPEN_ISSUE" 0.2.0 0.1.0-rc.6 > "$TMP/issue_guard2.out" 2>&1
  172. assert_grep "显式 GH_TOKEN 优先" "GH_TOKEN=ghp_explicit" "$TMP/mock_guard2.log"
  173. echo "== check-issue-gate.sh (mock gh)=="
  174. GATE="$ROOT/scripts/check-issue-gate.sh"
  175. GATE_LIST='[{"title":"[自动升级] dsh 上游 0.2.0 构建冒烟测试失败","number":5}]'
  176. MOCK_LOG="$TMP/mock3.log" MOCK_LIST_OUT="$GATE_LIST" GH="$MOCK_GH" \
  177. "$GATE" 0.2.0 > "$TMP/gate1.out" 2>&1
  178. assert_eq "同版本失败 Issue 存在时拦截" 1 "$(cat "$TMP/gate1.out")"
  179. MOCK_LOG="$TMP/mock4.log" MOCK_LIST_OUT="$GATE_LIST" GH="$MOCK_GH" \
  180. "$GATE" 0.2.1 > "$TMP/gate2.out" 2>&1
  181. assert_eq "不同版本失败 Issue 不拦截" 0 "$(cat "$TMP/gate2.out")"
  182. MOCK_LOG="$TMP/mock5.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
  183. "$GATE" 0.2.0 > "$TMP/gate3.out" 2>&1
  184. assert_eq "无失败 Issue 不拦截" 0 "$(cat "$TMP/gate3.out")"
  185. MOCK_LOG="$TMP/mock6.log" MOCK_LIST_OUT='' GH="$MOCK_GH" \
  186. "$GATE" 0.2.0 > "$TMP/gate4.out" 2>&1
  187. assert_eq "gh 查询失败(空输出)不拦截" 0 "$(cat "$TMP/gate4.out")"
  188. echo "== close-stale-issues.sh (mock gh)=="
  189. CLOSE="$ROOT/scripts/close-stale-issues.sh"
  190. CLOSE_LIST='[{"title":"[自动升级] dsh 上游 0.1.9 构建冒烟测试失败","number":9},{"title":"[自动升级] dsh 上游 0.2.0 构建冒烟测试失败","number":7}]'
  191. MOCK_LOG="$TMP/mock7.log" MOCK_LIST_OUT="$CLOSE_LIST" GH="$MOCK_GH" \
  192. "$CLOSE" > "$TMP/close1.out" 2>&1
  193. assert_grep "关闭第一条过时 Issue" "关闭过时 Issue #9" "$TMP/close1.out"
  194. assert_grep "关闭第二条过时 Issue" "关闭过时 Issue #7" "$TMP/close1.out"
  195. assert_eq "close 调用两次" 2 "$(grep -c 'issue close' "$TMP/mock7.log")"
  196. assert_grep "close 参数为 Issue 编号" "issue close 9" "$TMP/mock7.log"
  197. MOCK_LOG="$TMP/mock8.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
  198. "$CLOSE" > "$TMP/close2.out" 2>&1
  199. assert_grep "无 Issue 时提示" "无过时的失败 Issue" "$TMP/close2.out"
  200. assert_no_grep "无 Issue 时不调用 close" "issue close" "$TMP/mock8.log"
  201. echo "== check-pr-gate.sh (mock gh)=="
  202. PRGATE="$ROOT/scripts/check-pr-gate.sh"
  203. PRGATE_LIST='[{"title":"【维护,构建】升级 dsh 版本至 0.2.0","number":11}]'
  204. MOCK_LOG="$TMP/prgate1.log" MOCK_LIST_OUT="$PRGATE_LIST" GH="$MOCK_GH" \
  205. "$PRGATE" 0.2.0 > "$TMP/prgate1.out" 2>&1
  206. assert_eq "同版本 open PR 存在时拦截" 1 "$(cat "$TMP/prgate1.out")"
  207. MOCK_LOG="$TMP/prgate2.log" MOCK_LIST_OUT="$PRGATE_LIST" GH="$MOCK_GH" \
  208. "$PRGATE" 0.2.1 > "$TMP/prgate2.out" 2>&1
  209. assert_eq "不同版本 PR 不拦截" 0 "$(cat "$TMP/prgate2.out")"
  210. # 版本前缀边界:0.2.0 不得匹配标题中的 0.2.0-rc.1(与 upgrade-dsh.sh 的边界替换同思想)
  211. #
  212. # Version-prefix boundary: 0.2.0 must not match 0.2.0-rc.1 in a title
  213. # (same idea as upgrade-dsh.sh's boundary replacement)
  214. PRGATE_RC='[{"title":"【维护,构建】升级 dsh 版本至 0.2.0-rc.1","number":12}]'
  215. MOCK_LOG="$TMP/prgate3.log" MOCK_LIST_OUT="$PRGATE_RC" GH="$MOCK_GH" \
  216. "$PRGATE" 0.2.0 > "$TMP/prgate3.out" 2>&1
  217. assert_eq "版本前缀不误拦(0.2.0 vs 0.2.0-rc.1)" 0 "$(cat "$TMP/prgate3.out")"
  218. MOCK_LOG="$TMP/prgate4.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
  219. "$PRGATE" 0.2.0 > "$TMP/prgate4.out" 2>&1
  220. assert_eq "无 PR 不拦截" 0 "$(cat "$TMP/prgate4.out")"
  221. MOCK_LOG="$TMP/prgate5.log" MOCK_LIST_OUT='' GH="$MOCK_GH" \
  222. "$PRGATE" 0.2.0 > "$TMP/prgate5.out" 2>&1
  223. assert_eq "gh 查询失败(空输出)不拦截" 0 "$(cat "$TMP/prgate5.out")"
  224. echo "== push-upgrade-pr.sh upsert_pr (mock gh)=="
  225. # source 载入函数(脚本入口有 BASH_SOURCE 守卫,不执行 main);
  226. # 需先设置 GH 再 source,使脚本顶部 GH_BIN 指向 mock
  227. #
  228. # Source to load the function (the script has a BASH_SOURCE guard and won't run main);
  229. # set GH before sourcing so the script's top-level GH_BIN points at the mock
  230. PRPUSH="$ROOT/scripts/push-upgrade-pr.sh"
  231. # shellcheck disable=SC1090
  232. GH="$MOCK_GH" source "$PRPUSH"
  233. MOCK_LOG="$TMP/prpush1.log" MOCK_PR_STATE=OPEN GH="$MOCK_GH" \
  234. upsert_pr 0.2.0 0.1.0-rc.6 > "$TMP/prpush1.out" 2>&1
  235. assert_grep "PR 为 OPEN 时调用 edit" "pr edit auto-upgrade/dsh" "$TMP/prpush1.log"
  236. assert_grep "edit 标题含新版本" "升级 dsh 版本至 0.2.0" "$TMP/prpush1.log"
  237. MOCK_LOG="$TMP/prpush2.log" MOCK_PR_STATE='' GH="$MOCK_GH" \
  238. upsert_pr 0.2.0 0.1.0-rc.6 > "$TMP/prpush2.out" 2>&1
  239. assert_grep "无 PR 时调用 create" "pr create" "$TMP/prpush2.log"
  240. assert_grep "create head 为常驻分支" "auto-upgrade/dsh" "$TMP/prpush2.log"
  241. assert_grep "create body 含上游版本" "0.2.0" "$TMP/prpush2.log"
  242. MOCK_LOG="$TMP/prpush3.log" MOCK_PR_STATE=MERGED GH="$MOCK_GH" \
  243. upsert_pr 0.2.0 0.1.0-rc.6 > "$TMP/prpush3.out" 2>&1
  244. assert_grep "PR 已合并时重新 create" "pr create" "$TMP/prpush3.log"
  245. MOCK_LOG="$TMP/prpush4.log" MOCK_PR_STATE=CLOSED GH="$MOCK_GH" \
  246. upsert_pr 0.2.0 0.1.0-rc.6 > "$TMP/prpush4.out" 2>&1
  247. assert_grep "PR 已关闭时重新 create" "pr create" "$TMP/prpush4.log"
  248. echo
  249. echo "结果:$PASS 通过,$FAIL 失败"
  250. [ "$FAIL" -eq 0 ] || exit 1