#!/usr/bin/env bash # 上游测试用例**移植台账**自检(第五把尺子 · 上游对齐向) # # 用法: # bash scripts/check-upstream-tests.sh # 自检(绿 = 0) # bash scripts/check-upstream-tests.sh --report # 只出报告,不判红(排障用) # # 退出码:0 = 绿 / 1 = 有失败 / 2 = 前置错 # # 🔴 **纯静态**:不起后端、不连库、不跑 cargo(秒级)⇒ 可进任何一道闸门。 # (对位用例**真的跑不跑**由 `cargo test` 负责;本尺子只管「台账与代码**对得上**」) # # 检查项: # ① 台账每行 8 列、status 合法、reason 非空 # ② PORTED 行的对位文件**存在**、每个登记的用例函数**真在**那个文件里 # ③ OUT-OF-SCOPE 行的对位必须为空(`-`) # ④ PORTED 行登记的用例数 == 对位文件里**实际**的移植用例数(防少登记) # ⑤ 上游克隆在场时:上游测试类集合 == 台账登记集合(`upstream-test-census.py --check`) # # 上游克隆路径可用 `UPSTREAM_TDC_CLONE` / `UPSTREAM_TDS_CLONE` 覆盖; # 克隆不在场 ⇒ 跳过 ⑤(不判红)。 set -u ROOT="$(cd "$(dirname "$0")/.." && pwd)" LEDGER="${LEDGER:-${ROOT}/scripts/upstream-test-ledger.tsv}" PY_BIN="${PY_BIN-$(command -v python3 2>/dev/null || true)}" REPORT_ONLY=0 [ "${1:-}" = "--report" ] && REPORT_ONLY=1 [ -n "${PY_BIN}" ] || { echo "❌ 前置错误:找不到 python3。"; exit 2; } [ -f "${LEDGER}" ] || { echo "❌ 前置错误:台账 ${LEDGER} 不存在。"; exit 2; } TMP="$(mktemp -d)" trap 'rm -rf "${TMP}"' EXIT cat >"${TMP}/check.py" <<'PYEOF' import os import re import sys ROOT = sys.argv[1] LEDGER = sys.argv[2] REPORT_ONLY = sys.argv[3] == "report" failures = [] ported = 0 out_of_scope = 0 # ④ 用「文件级集合相等」防少登记 / 多登记: # 台账登记的用例集合 == 对位文件里**实际**的移植用例集合。 registered = {} # 文件 → {用例名} actual = {} # 文件 → {用例名} with open(LEDGER, encoding="utf-8") as fh: for lineno, raw in enumerate(fh, 1): line = raw.rstrip("\n") if not line.strip() or line.lstrip().startswith("#"): continue parts = line.split("\t") cls = parts[0].strip() if len(parts) < 8: failures.append("台账 %d 行:列数 %d(应为 8)⇒ %s" % (lineno, len(parts), cls)) continue repo, upfile, kind, asserts_n, counterpart, status, reason = ( parts[1].strip(), parts[2].strip(), parts[3].strip(), parts[4].strip(), parts[5].strip(), parts[6].strip(), parts[7].strip(), ) if status not in ("PORTED", "OUT-OF-SCOPE"): failures.append("台账 %d 行:status=%s(只能是 PORTED / OUT-OF-SCOPE)⇒ %s" % (lineno, status, cls)) continue if not reason: failures.append("台账 %d 行:reason 为空(搬/不搬都要写理由)⇒ %s" % (lineno, cls)) continue if not upfile or not upfile.endswith(".java"): failures.append("台账 %d 行:upstream_file 不合法 ⇒ %s" % (lineno, cls)) continue if kind not in ("JUNIT", "SELFTEST"): failures.append("台账 %d 行:kind=%s(只能是 JUNIT / SELFTEST)⇒ %s" % (lineno, kind, cls)) continue if status == "OUT-OF-SCOPE": out_of_scope += 1 if counterpart != "-": failures.append("台账 %d 行:OUT-OF-SCOPE 的对位必须是 `-` ⇒ %s" % (lineno, cls)) continue # ── PORTED ── ported += 1 if "::" not in counterpart: failures.append("台账 %d 行:PORTED 的对位必须是 `文件::fn1+fn2` ⇒ %s" % (lineno, cls)) continue path, _, fns = counterpart.partition("::") abs_path = os.path.join(ROOT, path) if not os.path.isfile(abs_path): failures.append("台账 %d 行:对位文件不存在:%s ⇒ %s" % (lineno, path, cls)) continue with open(abs_path, encoding="utf-8") as fh2: src = fh2.read() names = [n.strip() for n in fns.split("+") if n.strip()] if not names: failures.append("台账 %d 行:对位用例列表为空 ⇒ %s" % (lineno, cls)) continue for n in names: if not re.search(r"\bfn\s+" + re.escape(n) + r"\b", src): failures.append("台账 %d 行:对位用例 `%s` 不在 %s 里 ⇒ %s" % (lineno, n, path, cls)) registered.setdefault(path, set()).update(names) # ④ 文件级集合相等:台账登记的 == 文件里实际有的移植用例 # (前缀约定:移植用例一律带下列前缀之一 ⇒ 辅助函数不会被误计) PORTED_PREFIX = ("upstream_", "hop1_", "hop2_", "runtime_guard_", "prod_guard_") for path, names in sorted(registered.items()): abs_path = os.path.join(ROOT, path) with open(abs_path, encoding="utf-8") as fh2: src = fh2.read() found = set(re.findall(r"\bfn\s+([A-Za-z0-9_]+)\b", src)) found = {n for n in found if n.startswith(PORTED_PREFIX)} for n in sorted(found - names): failures.append("%s 里的移植用例 `%s` **未登记台账** ⇒ 补台账(或它不该存在)" % (path, n)) for n in sorted(names - found): failures.append("台账登记的 `%s` 在 %s 里找不到(已改名/已删?)" % (n, path)) print("台账:%d 行(PORTED %d / OUT-OF-SCOPE %d)" % (ported + out_of_scope, ported, out_of_scope)) if failures: print("❌ 台账自检不通过:") for f in failures: print(" · %s" % f) sys.exit(1) print("✅ 台账自检通过:每条 PORTED 的对位用例都在、每条 OUT-OF-SCOPE 都写了理由") PYEOF "${PY_BIN}" "${TMP}/check.py" "${ROOT}" "${LEDGER}" "${REPORT_ONLY}" STATIC_RC=$? # ⑤ 上游克隆在场时:上游测试类集合 ⇄ 台账登记集合 CENSUS_RC=0 if "${PY_BIN}" "${ROOT}/scripts/upstream-test-census.py" --check; then : else CENSUS_RC=$? fi if [ "${REPORT_ONLY}" = "1" ]; then echo "(--report:不判红)" exit 0 fi if [ "${STATIC_RC}" != "0" ] || [ "${CENSUS_RC}" != "0" ]; then exit 1 fi exit 0