Browse Source

【修复,构建】升级脚本按版本边界替换

原全局子串替换会把 OLD 当作更长版本的前缀误改写(如 0.1.0 把历史引用 0.1.0-rc.6 改成不存在的 0.1.1-rc.6,用户照 README 构建必失败)。改为:Dockerfile/README 用版本边界锚定(前后非版本字符集才替换),docker-compose.yml 因 ${DSH_VERSION:-OLD} 前置 - 无法与 prerelease 内部 - 区分,用精确上下文替换;原子验证同步按边界匹配。新增前缀保护单测。
PJ568 3 months ago
parent
commit
d1e81b1c49
2 changed files with 49 additions and 10 deletions
  1. 31 10
      scripts/upgrade-dsh.sh
  2. 18 0
      tests/test-scripts.sh

+ 31 - 10
scripts/upgrade-dsh.sh

@@ -19,20 +19,41 @@ OLD="$(grep -E '^ARG DSH_VERSION=' "$DIR/Dockerfile" | head -n1 | cut -d= -f2)"
 [ -n "$OLD" ] || { echo "错误:未能在 Dockerfile 找到 ARG DSH_VERSION" >&2; exit 1; }
 [ "$NEW" != "$OLD" ] || { echo "新版本与当前版本相同($NEW),无需升级" >&2; exit 0; }
 
-FILES=(Dockerfile docker-compose.yml README.md README.zh.md)
-for f in "${FILES[@]}"; do
+# npm 版本号字符集为 [0-9A-Za-z.-];先转义 OLD 为正则字面量,再以「版本边界」锚定替换:
+# 仅当 OLD 前后都不是版本字符时才替换,避免把 OLD 当作更长版本的前缀误改写
+# (如 0.1.0 不应把历史引用 0.1.0-rc.6 改成 0.1.1-rc.6)。NEW 为 npm 版本号
+# (不含 & 与 \),可直接作替换串。
+#
+# npm version chars are [0-9A-Za-z.-]; escape OLD to a regex literal, then anchor on
+# version boundaries: replace OLD only when neither neighbour is a version char, so OLD
+# is never rewritten as a prefix of a longer version (e.g. 0.1.0 must not turn the
+# historical reference 0.1.0-rc.6 into 0.1.1-rc.6). NEW is an npm version (no & or \),
+# safe as a replacement string.
+ESC="$(printf '%s' "$OLD" | sed 's/[][\\^$.|*+?()]/\\&/g')"
+# Dockerfile / README:版本边界替换(见上注释)
+#
+# Dockerfile / README: version-boundary replacement (see the note above)
+for f in Dockerfile README.md README.zh.md; do
   [ -f "$DIR/$f" ] || { echo "错误:缺少 $f" >&2; exit 1; }
-  # npm 版本号仅含 [0-9a-zA-Z.-],无 sed 特殊字符,可安全作替换串
-  #
-  # npm versions only contain [0-9a-zA-Z.-], no sed metacharacters, safe as a replacement string
-  sed -i "s/${OLD}/${NEW}/g" "$DIR/$f"
+  sed -i "s/\\(^\\|[^-0-9A-Za-z.]\\)${ESC}\\([^-0-9A-Za-z.]\\|$\\)/\\1${NEW}\\2/g" "$DIR/$f"
 done
+# docker-compose.yml:版本号嵌在 ${DSH_VERSION:-OLD} 中,前置 `-` 是 bash 默认值语法,
+# 与 prerelease 内部的 `-` 无法仅凭字符类区分,故用精确上下文替换(compose 中版本号只出现于该位置)
+#
+# docker-compose.yml: the version sits inside ${DSH_VERSION:-OLD}, whose leading `-` is bash
+# default-value syntax and is indistinguishable from a prerelease `-` by character class alone,
+# so replace with an exact context match (the version appears only at that spot in compose)
+[ -f "$DIR/docker-compose.yml" ] || { echo "错误:缺少 docker-compose.yml" >&2; exit 1; }
+sed -i "s/\${DSH_VERSION:-${ESC}}/\${DSH_VERSION:-${NEW}}/g" "$DIR/docker-compose.yml"
 
-# 原子验证:旧版本号必须全部消失,新版本号已写入
+# 原子验证:作为完整版本号的 OLD 必须全部消失,新版本号已写入
+# (验证同样按版本边界/精确上下文匹配,历史引用的更长版本号不视为残留)
 #
-# Atomic verification: the old version must be fully gone and the new version written
-LEFT="$(grep -l "$OLD" "$DIR"/Dockerfile "$DIR"/docker-compose.yml "$DIR"/README.md "$DIR"/README.zh.md 2>/dev/null || true)"
-[ -z "$LEFT" ] || { echo "错误:旧版本仍残留于 $LEFT" >&2; exit 1; }
+# Atomic verification: OLD as a full version number must be fully gone and the new one written
+# (also matched at version boundaries / exact contexts; longer historical versions are not leftovers)
+LEFT="$(grep -lE "(^|[^-0-9A-Za-z.])${ESC}([^-0-9A-Za-z.]|$)" "$DIR"/Dockerfile "$DIR"/README.md "$DIR"/README.zh.md 2>/dev/null || true)"
+LEFT_COMPOSE="$(grep -lE "\$\{DSH_VERSION:-${ESC}\}" "$DIR"/docker-compose.yml 2>/dev/null || true)"
+[ -z "$LEFT$LEFT_COMPOSE" ] || { echo "错误:旧版本仍残留于 $LEFT $LEFT_COMPOSE" >&2; exit 1; }
 grep -q "$NEW" "$DIR/Dockerfile" || { echo "错误:新版本未写入" >&2; exit 1; }
 
 echo "已升级:$OLD → $NEW(Dockerfile / docker-compose.yml / README.md / README.zh.md)"

+ 18 - 0
tests/test-scripts.sh

@@ -56,6 +56,24 @@ assert_no_grep "README.md 旧版本已清除"        "0\.1\.0-rc\.6" "$TMP/READM
 assert_no_grep "README.zh.md 旧版本已清除"     "0\.1\.0-rc\.6" "$TMP/README.zh.md"
 assert_grep     "DESIGN.md 不受影响"           "0\.1\.0-rc\.6" "$TMP/DESIGN.md"
 
+echo "== upgrade-dsh.sh(版本边界:历史引用不被误改)=="
+TMP3="$TMP/boundary"
+mkdir -p "$TMP3"
+for f in Dockerfile README.md README.zh.md; do
+  printf 'ARG DSH_VERSION=0.1.0\n' > "$TMP3/$f"
+done
+# shellcheck disable=SC2016 # 单引号是有意的:fixture 需要字面 ${DSH_VERSION:-0.1.0}
+printf 'DSH_VERSION: ${DSH_VERSION:-0.1.0}\n' > "$TMP3/docker-compose.yml"
+printf '历史版本参考 0.1.0-rc.6 仅供说明\n' >> "$TMP3/README.md"
+"$ROOT/scripts/upgrade-dsh.sh" 0.1.1 "$TMP3" >/dev/null
+assert_grep     "新版本 0.1.1 已写入"          "ARG DSH_VERSION=0\.1\.1" "$TMP3/README.md"
+assert_grep     "compose 精确上下文已替换"     "DSH_VERSION: \${DSH_VERSION:-0\.1\.1}" "$TMP3/docker-compose.yml"
+# 独立的 0.1.0 必须消失(版本边界匹配;0.1.0-rc.6 内的子串不算)
+#
+# standalone 0.1.0 must be gone (matched at version boundaries; the substring inside 0.1.0-rc.6 doesn't count)
+assert_no_grep  "独立 0.1.0 全部替换"          "\(^\|[^-0-9A-Za-z.]\)0\.1\.0\([^-0-9A-Za-z.]\|$\)" "$TMP3/README.md"
+assert_grep     "历史引用 0.1.0-rc.6 保留"     "0\.1\.0-rc\.6" "$TMP3/README.md"
+
 echo "== upgrade-dsh.sh(同版本幂等)=="
 TMP2="$TMP/idempotent"
 mkdir -p "$TMP2"