test-scripts.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env bash
  2. # 单测:should-upgrade.sh(版本比较)与 upgrade-dsh.sh(文件更新)
  3. #
  4. # Unit tests: should-upgrade.sh (version comparison) and upgrade-dsh.sh (file updates)
  5. # Usage: tests/test-scripts.sh
  6. set -euo pipefail
  7. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  8. TMP="$(mktemp -d)"
  9. trap 'rm -rf "$TMP"' EXIT
  10. PASS=0
  11. FAIL=0
  12. # 断言辅助
  13. #
  14. # assertion helpers
  15. assert_eq() { # <desc> <expected> <actual>
  16. if [ "$2" = "$3" ]; then PASS=$((PASS+1)); echo " ✓ $1"; else
  17. FAIL=$((FAIL+1)); echo " ✗ $1:期望 [$2] 实际 [$3]"
  18. fi
  19. }
  20. assert_grep() { # <desc> <pattern> <file>(期望存在匹配)
  21. if grep -q "$2" "$3"; then PASS=$((PASS+1)); echo " ✓ $1"; else
  22. FAIL=$((FAIL+1)); echo " ✗ $1:$3 未匹配 $2"
  23. fi
  24. }
  25. assert_no_grep() { # <desc> <pattern> <file>(期望无匹配)
  26. if grep -q "$2" "$3"; then FAIL=$((FAIL+1)); echo " ✗ $1:$3 意外匹配 $2"; else
  27. PASS=$((PASS+1)); echo " ✓ $1"
  28. fi
  29. }
  30. echo "== should-upgrade.sh(版本比较)=="
  31. SU="$ROOT/scripts/should-upgrade.sh"
  32. assert_eq "相等版本不升级" 0 "$("$SU" 0.1.0-rc.6 0.1.0-rc.6)"
  33. assert_eq "rc → 正式版应升级" 1 "$("$SU" 0.1.0-rc.6 0.1.0)"
  34. assert_eq "patch 升级应升级" 1 "$("$SU" 0.1.0 0.1.1)"
  35. assert_eq "minor 升级应升级" 1 "$("$SU" 0.1.0 0.2.0)"
  36. assert_eq "上游回退(降级)不升级" 0 "$("$SU" 0.1.0-rc.6 0.1.0-rc.5)"
  37. assert_eq "正式版后的 rc 不升级" 0 "$("$SU" 0.1.0 0.1.0-rc.9)"
  38. # 同元组更高 rc 原本就能过;跨元组 prerelease 才是 range 默认规则的盲区
  39. # (>0.1.0-rc.7 不匹配 0.1.1-rc.2,正是自动升级卡住的原因)
  40. #
  41. # A higher rc in the same tuple already passed; cross-tuple prereleases are the
  42. # range default's blind spot (>0.1.0-rc.7 does not match 0.1.1-rc.2 — why auto-upgrade stalled)
  43. assert_eq "同元组更高 rc 应升级" 1 "$("$SU" 0.1.0-rc.7 0.1.0-rc.8)"
  44. assert_eq "跨元组 prerelease 应升级" 1 "$("$SU" 0.1.0-rc.7 0.1.1-rc.2)"
  45. assert_eq "跨元组 alpha 应升级" 1 "$("$SU" 0.1.0-rc.7 0.1.2-alpha.1)"
  46. echo "== upgrade-dsh.sh(文件更新)=="
  47. # fixture:直接从仓库复制当前文件(模拟"升级前"状态)
  48. #
  49. # fixtures: copy the current repo files directly (simulating the "pre-upgrade" state)
  50. for f in Dockerfile docker-compose.yml README.md README.zh.md DESIGN.md; do
  51. cp "$ROOT/$f" "$TMP/"
  52. done
  53. # 当前版本号从 fixture Dockerfile 动态提取:升级脚本可能升级到任意上游版本,
  54. # 硬编码版本会让「旧版本清除」断言失效或空转
  55. #
  56. # Current version is read from the fixture Dockerfile: the upgrade script may bump to any
  57. # upstream version, so hardcoding one would make the "old version gone" asserts stale or vacuous
  58. CUR_VERSION="$(grep -E '^ARG DSH_VERSION=' "$TMP/Dockerfile" | head -n1 | cut -d= -f2)"
  59. ESC_VERSION="$(printf '%s' "$CUR_VERSION" | sed 's/[][\\^$.|*+?()]/\\&/g')"
  60. "$ROOT/scripts/upgrade-dsh.sh" 0.2.0 "$TMP" >/dev/null
  61. assert_no_grep "Dockerfile 旧版本已清除" "$ESC_VERSION" "$TMP/Dockerfile"
  62. assert_grep "Dockerfile 新版本已写入" "0\.2\.0" "$TMP/Dockerfile"
  63. assert_no_grep "docker-compose.yml 旧版本清除" "$ESC_VERSION" "$TMP/docker-compose.yml"
  64. assert_grep "docker-compose.yml 新版本写入" "0\.2\.0" "$TMP/docker-compose.yml"
  65. assert_no_grep "README.md 旧版本已清除" "$ESC_VERSION" "$TMP/README.md"
  66. assert_no_grep "README.zh.md 旧版本已清除" "$ESC_VERSION" "$TMP/README.zh.md"
  67. # DESIGN.md 是历史设计稿,不在升级范围:升级前后内容必须完全一致
  68. # (不依赖任何版本号,避免历史引用随版本演进失效)
  69. #
  70. # DESIGN.md is a historical design doc, out of the upgrade scope: its content must be
  71. # byte-identical after an upgrade (independent of any version number)
  72. assert_eq "DESIGN.md 不受影响" "$(md5sum < "$ROOT/DESIGN.md")" "$(md5sum < "$TMP/DESIGN.md")"
  73. echo "== upgrade-dsh.sh(版本边界:历史引用不被误改)=="
  74. TMP3="$TMP/boundary"
  75. mkdir -p "$TMP3"
  76. for f in Dockerfile README.md README.zh.md; do
  77. printf 'ARG DSH_VERSION=0.1.0\n' > "$TMP3/$f"
  78. done
  79. # shellcheck disable=SC2016 # 单引号是有意的:fixture 需要字面 ${DSH_VERSION:-0.1.0}
  80. printf 'DSH_VERSION: ${DSH_VERSION:-0.1.0}\n' > "$TMP3/docker-compose.yml"
  81. printf '历史版本参考 0.1.0-rc.6 仅供说明\n' >> "$TMP3/README.md"
  82. "$ROOT/scripts/upgrade-dsh.sh" 0.1.1 "$TMP3" >/dev/null
  83. assert_grep "新版本 0.1.1 已写入" "ARG DSH_VERSION=0\.1\.1" "$TMP3/README.md"
  84. assert_grep "compose 精确上下文已替换" "DSH_VERSION: \${DSH_VERSION:-0\.1\.1}" "$TMP3/docker-compose.yml"
  85. # 独立的 0.1.0 必须消失(版本边界匹配;0.1.0-rc.6 内的子串不算)
  86. #
  87. # standalone 0.1.0 must be gone (matched at version boundaries; the substring inside 0.1.0-rc.6 doesn't count)
  88. assert_no_grep "独立 0.1.0 全部替换" "\(^\|[^-0-9A-Za-z.]\)0\.1\.0\([^-0-9A-Za-z.]\|$\)" "$TMP3/README.md"
  89. assert_grep "历史引用 0.1.0-rc.6 保留" "0\.1\.0-rc\.6" "$TMP3/README.md"
  90. echo "== upgrade-dsh.sh(同版本幂等)=="
  91. TMP2="$TMP/idempotent"
  92. mkdir -p "$TMP2"
  93. for f in Dockerfile docker-compose.yml README.md README.zh.md; do
  94. cp "$ROOT/$f" "$TMP2/"
  95. done
  96. # 以 fixture 中的当前版本为参数:同版本必须走「无需升级」分支(版本号动态提取,
  97. # 与 upgrade-dsh.sh 内部读取 Dockerfile 的方式一致,避免硬编码随上游升级失配)
  98. #
  99. # Pass the fixture's own current version: an equal version must take the "no upgrade needed"
  100. # branch. The version is read the same way upgrade-dsh.sh reads the Dockerfile, so the test
  101. # cannot drift when the upstream version bumps
  102. CUR_VERSION="$(grep -E '^ARG DSH_VERSION=' "$TMP2/Dockerfile" | head -n1 | cut -d= -f2)"
  103. "$ROOT/scripts/upgrade-dsh.sh" "$CUR_VERSION" "$TMP2" > "$TMP2/idempotent.out" 2>&1 || true
  104. assert_eq "同版本返回提示且不报错" 0 "$?"
  105. assert_grep "同版本提示输出" "无需升级" "$TMP2/idempotent.out"
  106. echo "== open-issue.sh (mock gh)=="
  107. MOCK_GH="$TMP/mock-gh"
  108. cat > "$MOCK_GH" <<'MOCK'
  109. #!/usr/bin/env bash
  110. echo "$*" >> "${MOCK_LOG:?}"
  111. case "$1 $2" in
  112. "issue list")
  113. if [[ "$*" == *"-q"* ]]; then
  114. # 仿真 gh 的 -q jq 查询:[] → 空输出;[{"number":7}] → 7
  115. python3 -c "import json,sys; d=json.load(sys.stdin); print(d[0]['number'] if d else '')" <<< "${MOCK_LIST_OUT-}"
  116. else
  117. # 原始 JSON 输出(gate/close 脚本自行 jq 解析);未设置时输出空 = 仿真 gh 失败
  118. echo "${MOCK_LIST_OUT-}"
  119. fi
  120. ;;
  121. "issue create") echo "created" ;;
  122. "issue close") echo "closed $3" ;;
  123. esac
  124. MOCK
  125. chmod +x "$MOCK_GH"
  126. OPEN_ISSUE="$ROOT/scripts/open-issue.sh"
  127. MOCK_LOG="$TMP/mock1.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
  128. "$OPEN_ISSUE" 0.2.0 0.1.0-rc.6 > "$TMP/issue1.out" 2>&1
  129. assert_grep "无已有 Issue 时创建" "已创建 Issue" "$TMP/issue1.out"
  130. assert_grep "create 调用含新版本标题" "issue create --title \[自动升级\] dsh 上游 0.2.0" "$TMP/mock1.log"
  131. assert_grep "create 调用含上游版本正文" "0.2.0" "$TMP/mock1.log"
  132. MOCK_LOG="$TMP/mock2.log" MOCK_LIST_OUT='[{"number":7}]' GH="$MOCK_GH" \
  133. "$OPEN_ISSUE" 0.2.0 0.1.0-rc.6 > "$TMP/issue2.out" 2>&1
  134. assert_grep "已有同版本 Issue 时跳过" "已存在同版本 Issue #7" "$TMP/issue2.out"
  135. assert_no_grep "去重时不调用 create" "issue create" "$TMP/mock2.log"
  136. echo "== check-issue-gate.sh (mock gh)=="
  137. GATE="$ROOT/scripts/check-issue-gate.sh"
  138. GATE_LIST='[{"title":"[自动升级] dsh 上游 0.2.0 构建冒烟测试失败","number":5}]'
  139. MOCK_LOG="$TMP/mock3.log" MOCK_LIST_OUT="$GATE_LIST" GH="$MOCK_GH" \
  140. "$GATE" 0.2.0 > "$TMP/gate1.out" 2>&1
  141. assert_eq "同版本失败 Issue 存在时拦截" 1 "$(cat "$TMP/gate1.out")"
  142. MOCK_LOG="$TMP/mock4.log" MOCK_LIST_OUT="$GATE_LIST" GH="$MOCK_GH" \
  143. "$GATE" 0.2.1 > "$TMP/gate2.out" 2>&1
  144. assert_eq "不同版本失败 Issue 不拦截" 0 "$(cat "$TMP/gate2.out")"
  145. MOCK_LOG="$TMP/mock5.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
  146. "$GATE" 0.2.0 > "$TMP/gate3.out" 2>&1
  147. assert_eq "无失败 Issue 不拦截" 0 "$(cat "$TMP/gate3.out")"
  148. MOCK_LOG="$TMP/mock6.log" MOCK_LIST_OUT='' GH="$MOCK_GH" \
  149. "$GATE" 0.2.0 > "$TMP/gate4.out" 2>&1
  150. assert_eq "gh 查询失败(空输出)不拦截" 0 "$(cat "$TMP/gate4.out")"
  151. echo "== close-stale-issues.sh (mock gh)=="
  152. CLOSE="$ROOT/scripts/close-stale-issues.sh"
  153. CLOSE_LIST='[{"title":"[自动升级] dsh 上游 0.1.9 构建冒烟测试失败","number":9},{"title":"[自动升级] dsh 上游 0.2.0 构建冒烟测试失败","number":7}]'
  154. MOCK_LOG="$TMP/mock7.log" MOCK_LIST_OUT="$CLOSE_LIST" GH="$MOCK_GH" \
  155. "$CLOSE" > "$TMP/close1.out" 2>&1
  156. assert_grep "关闭第一条过时 Issue" "关闭过时 Issue #9" "$TMP/close1.out"
  157. assert_grep "关闭第二条过时 Issue" "关闭过时 Issue #7" "$TMP/close1.out"
  158. assert_eq "close 调用两次" 2 "$(grep -c 'issue close' "$TMP/mock7.log")"
  159. assert_grep "close 参数为 Issue 编号" "issue close 9" "$TMP/mock7.log"
  160. MOCK_LOG="$TMP/mock8.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
  161. "$CLOSE" > "$TMP/close2.out" 2>&1
  162. assert_grep "无 Issue 时提示" "无过时的失败 Issue" "$TMP/close2.out"
  163. assert_no_grep "无 Issue 时不调用 close" "issue close" "$TMP/mock8.log"
  164. echo
  165. echo "结果:$PASS 通过,$FAIL 失败"
  166. [ "$FAIL" -eq 0 ] || exit 1