+113
-21
@@ -37,7 +37,7 @@ from typing import Callable
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
WEB = ROOT / "web"
|
||||
REPORT_DEFAULT = ROOT / "ci-report.json"
|
||||
POLICY_REVISION = "ci-checks/3" # 判据变更时改这里,报告里跟着变
|
||||
POLICY_REVISION = "ci-checks/5" # 判据变更时改这里,报告里跟着变
|
||||
MANIFESTS = ["go.mod", "go.sum", "web/package.json", "web/package-lock.json"]
|
||||
|
||||
SUCCESS, FAILURE, SKIPPED, NOT_RUN = "success", "failure", "skipped", "not_run"
|
||||
@@ -251,6 +251,13 @@ def checks() -> list[Check]:
|
||||
[py3, "scripts/ci_checks.py", "--internal-web-test"], env=env),
|
||||
Check("contracts", "版本与变更记录契约(不许 SKIP;开发构建不许自称正式版)",
|
||||
[py3, "scripts/ci_checks.py", "--internal-contracts", g], env=env),
|
||||
# 演练判据本身也要被验:认领/决策行缺一条、只有汇总行、杀前日志都不许算通过。
|
||||
# 2026-10-29 那次演练 exit 5 就是"脚本要的老文案只有一条代码路径会打印"。
|
||||
Check("takeover-log-contract", "接管日志契约(认领/决策行判据 + 正负例自检)",
|
||||
[py3, "scripts/takeover_log_contract.py", "--self-test"]),
|
||||
# F12:运行镜像少 COPY 一个模块只会在真实刷新时炸;静态闭包比对进日常门禁。
|
||||
Check("runtime-scripts", "运行镜像脚本依赖闭包(缺模块负例必须红)",
|
||||
[py3, "scripts/test_runtime_script_packaging.py"]),
|
||||
Check("version", "CHANGELOG 与版本记录(四类小节无空栏目 + 正式 tag 与提交内版本一致)",
|
||||
[py3, "scripts/check_changelog.py", "--tags"]),
|
||||
Check("security-secrets", "密钥泄漏扫描(含扫描器自检)",
|
||||
@@ -353,9 +360,88 @@ def cmd_internal_web_test() -> int:
|
||||
return 0 if status == SUCCESS and p.returncode == 0 else 1
|
||||
|
||||
|
||||
CONTRACT_PASS_TESTS = ("TestNumberMatchesChangelog", "TestDevBuildDoesNotClaimRelease")
|
||||
# 负例必须失败在**预期断言**上。下面这些标记说明失败来自编译/网络/工具链:
|
||||
# 那种 rc!=0 什么也不能证明(构建坏了也会非 0),不能拿它当"断言真的活着"。
|
||||
CONTRACT_INFRA_MARKERS = (
|
||||
"build failed", "cannot find package", "no required module provides", "undefined:",
|
||||
"no such host", "dial tcp", "i/o timeout", "tls handshake timeout", "network is unreachable",
|
||||
"connection refused", "signal: killed", "permission denied", "go: downloading",
|
||||
)
|
||||
|
||||
|
||||
def judge_contract_positive(rc: int, text: str, tests: tuple[str, ...] = CONTRACT_PASS_TESTS) -> tuple[bool, str]:
|
||||
"""正向:**进程成功** + 每个必需测试真 PASS + 没有失败事件。
|
||||
|
||||
只看 `--- PASS: X` 是不够的:同一进程里别的测试失败时 go test 仍会打印
|
||||
指定测试的 PASS 行,退出码却是非 0。那种"两个 PASS"证明不了这一轮测试成功
|
||||
(F19 的原始缺陷:383 行拿到 rc1 却没用来判断)。
|
||||
"""
|
||||
if rc != 0:
|
||||
return False, f"正向测试进程退出码 {rc} != 0:指定测试的 PASS 行不能证明这一轮测试成功"
|
||||
if "--- FAIL:" in text:
|
||||
failed = [line.strip() for line in text.splitlines() if line.strip().startswith("--- FAIL:")]
|
||||
return False, f"正向测试里有失败事件:{failed[:3]}"
|
||||
if re.search(r"^FAIL\b", text, re.M):
|
||||
return False, "正向测试包整体 FAIL"
|
||||
for name in tests:
|
||||
if f"--- SKIP: {name}" in text:
|
||||
return False, f"{name} 被跳过(跳过不算通过)"
|
||||
if f"--- PASS: {name}" not in text:
|
||||
return False, f"{name} 没有真正 PASS(被 SKIP 或没跑到)"
|
||||
return True, "进程退出码 0,且每个必需测试都真的 PASS"
|
||||
|
||||
|
||||
def judge_contract_negative(rc: int, text: str, expected: str = "TestDevBuildDoesNotClaimRelease") -> tuple[bool, str]:
|
||||
"""负例:必须非 0,且失败归因于**预期断言**,不是编译/网络等基础设施错误。"""
|
||||
if rc == 0:
|
||||
return False, "负例竟然通过:这条断言是死的"
|
||||
lowered = text.lower()
|
||||
for marker in CONTRACT_INFRA_MARKERS:
|
||||
if marker in lowered:
|
||||
return False, f"负例因基础设施/编译问题失败(命中 {marker!r}),不能算预期断言生效"
|
||||
if f"--- FAIL: {expected}" not in text:
|
||||
return False, f"负例没有失败在预期断言 {expected} 上(没有它的 FAIL 行)"
|
||||
return True, f"负例非 0 且失败在 {expected}"
|
||||
|
||||
|
||||
def contract_judgement_problems() -> list[str]:
|
||||
"""判据本身的**反例自检**:这些分支必须由函数判定,不能靠远端 CI 碰巧没有别的问题。
|
||||
|
||||
verify 的 contracts 每一步都会先跑它,所以"判据被改坏"在本地就会红,
|
||||
而不是等到某天真的出现"进程非 0 但两个 PASS"时才静默放行。
|
||||
"""
|
||||
passes = ("--- PASS: TestNumberMatchesChangelog\n"
|
||||
"--- PASS: TestDevBuildDoesNotClaimRelease\n")
|
||||
cases = [
|
||||
("正向 rc=0 且两个 PASS ⇒ 绿", judge_contract_positive(0, passes)[0], True),
|
||||
("正向 rc=1 但两个 PASS 都在 ⇒ 仍要红(F19 原始缺陷)",
|
||||
judge_contract_positive(1, passes + "--- FAIL: TestSomethingElse\n")[0], False),
|
||||
("正向 rc=1、PASS 齐全、无 FAIL 行 ⇒ 仍要红",
|
||||
judge_contract_positive(1, passes)[0], False),
|
||||
("正向缺必需 PASS ⇒ 红", judge_contract_positive(0, "--- FAIL:\n")[0], False),
|
||||
("正向必需测试被 SKIP ⇒ 红",
|
||||
judge_contract_positive(0, "--- SKIP: TestDevBuildDoesNotClaimRelease\n")[0], False),
|
||||
("负例 rc=0 ⇒ 红", judge_contract_negative(0, "PASS\n")[0], False),
|
||||
("负例失败在预期断言 ⇒ 绿",
|
||||
judge_contract_negative(1, "--- FAIL: TestDevBuildDoesNotClaimRelease\n")[0], True),
|
||||
("负例因编译错误非 0 ⇒ 红",
|
||||
judge_contract_negative(1, "build failed: undefined: foo\n")[0], False),
|
||||
("负例因网络错误非 0 ⇒ 红",
|
||||
judge_contract_negative(1, "dial tcp 10.0.0.1:443: i/o timeout\n")[0], False),
|
||||
("负例非 0 但没有预期断言的 FAIL 行 ⇒ 红",
|
||||
judge_contract_negative(1, "--- FAIL: TestOther\n")[0], False),
|
||||
]
|
||||
return [f"版本契约判据:{name} 期望 {want},实际 {got}" for name, got, want in cases if got != want]
|
||||
|
||||
|
||||
def cmd_internal_contracts(go: str) -> int:
|
||||
"""版本契约:结构 + 注入身份必须**真 PASS**(SKIP 判红),
|
||||
并且**开发构建不许自称正式版**(负例:标成 release ⇒ 必须红)。"""
|
||||
并且**开发构建不许自称正式版**(负例:标成 release ⇒ 必须红且红在预期断言上)。
|
||||
|
||||
判据同时核三件事:进程退出码、必需测试集合、负例失败归因——
|
||||
缺任意一件,"绿"就可能是别的测试失败/编译错误伪装出来的。
|
||||
"""
|
||||
qa_rc = subprocess.run(py("-m", "unittest", "discover", "-s", "scripts", "-p", "test_qa_env.py", "-v"),
|
||||
cwd=ROOT).returncode
|
||||
if qa_rc:
|
||||
@@ -367,6 +453,10 @@ def cmd_internal_contracts(go: str) -> int:
|
||||
snapshot_rc = subprocess.run(py("scripts/development_snapshot.py","--self-test"),cwd=ROOT).returncode
|
||||
if snapshot_rc:
|
||||
return 1
|
||||
judgement = contract_judgement_problems()
|
||||
if judgement:
|
||||
print(f"❌ 版本契约判据本身不成立:{judgement}", file=sys.stderr)
|
||||
return 1
|
||||
rc = subprocess.run(py("scripts/check_changelog.py"), cwd=ROOT).returncode
|
||||
if rc:
|
||||
return 1
|
||||
@@ -376,7 +466,7 @@ def cmd_internal_contracts(go: str) -> int:
|
||||
return 1
|
||||
print(f"-- 版本契约:显示版本 {ident['display_version']}({ident['build_kind']}"
|
||||
f"|release_version={ident['release_version']!r})--")
|
||||
tests = ("TestNumberMatchesChangelog", "TestDevBuildDoesNotClaimRelease")
|
||||
tests = CONTRACT_PASS_TESTS
|
||||
|
||||
def run(ld: str) -> tuple[int, str]:
|
||||
p = subprocess.run([go, "test", f"-ldflags={ld}", "-count=1", "-run", "Test", "-v", "-json",
|
||||
@@ -386,28 +476,25 @@ def cmd_internal_contracts(go: str) -> int:
|
||||
|
||||
rc1, text = run(ldflags_for(ident))
|
||||
print(text[-2000:], end="")
|
||||
# ⚠️ 被跳过的断言等于没有断言:必须看到 PASS,SKIP 一律判红。
|
||||
# 用 in 而不是 `| grep -q`:pipefail 下 grep -q 会因 SIGPIPE 谎报失败(trap-7)。
|
||||
for name in tests:
|
||||
if f"--- PASS: {name}" not in text:
|
||||
print(f"❌ 版本契约 {name} 没有真正 PASS(被 SKIP 或没跑到)—— "
|
||||
f"缺 -ldflags,或 CHANGELOG / 构建身份有问题", file=sys.stderr)
|
||||
return 1
|
||||
if f"--- SKIP: {name}" in text:
|
||||
print(f"❌ 版本契约 {name} 被跳过(构建身份没注入)—— 跳过不算通过", file=sys.stderr)
|
||||
return 1
|
||||
ok, why = judge_contract_positive(rc1, text, tests)
|
||||
if not ok:
|
||||
print(f"❌ 版本契约不成立:{why} —— 缺 -ldflags,或 CHANGELOG / 构建身份有问题",
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
|
||||
# 负例:把开发构建标成正式版(ReleaseVersion 指向 CHANGELOG 里没有的号)⇒ **必须红**。
|
||||
# 没有这条,"开发版沿用正式版号"这个病会静默回归。
|
||||
# 而且必须红在**预期断言**上:编译错误也会非 0,那种"红"证明不了断言活着。
|
||||
bogus = "9.9.9" if ident.get("release_version") != "9.9.9" else "8.8.8"
|
||||
rc2, _ = run(ldflags_for(ident, build_kind="release", release_version=bogus))
|
||||
if rc2 == 0:
|
||||
print(f"❌ 把构建标成正式版(BuildKind=release / ReleaseVersion={bogus})竟然还是绿的 —— "
|
||||
f"这条断言是死的", file=sys.stderr)
|
||||
rc2, text2 = run(ldflags_for(ident, build_kind="release", release_version=bogus))
|
||||
print(text2[-2000:], end="")
|
||||
ok, why = judge_contract_negative(rc2, text2, "TestDevBuildDoesNotClaimRelease")
|
||||
if not ok:
|
||||
print(f"❌ 负例不合格:{why}", file=sys.stderr)
|
||||
return 1
|
||||
print(f"-- 负例确认:自称正式版({bogus})⇒ 红 ✓ --")
|
||||
print(f"-- 负例确认:自称正式版({bogus})⇒ 红于预期断言 ✓ --")
|
||||
print("✅ 版本契约成立:注入 == CHANGELOG 顶部(没有正式版本时为空),"
|
||||
"开发构建带 dev 标识,且断言真的执行了")
|
||||
"开发构建带 dev 标识,进程退出码与断言归因都已核对")
|
||||
return 0
|
||||
|
||||
|
||||
@@ -447,6 +534,8 @@ CHECK_MANIFEST: dict[str, tuple[str, ...]] = {
|
||||
"tests-go": ("local", "ci"),
|
||||
"tests-web": ("local", "ci"),
|
||||
"contracts": ("local", "ci"),
|
||||
"takeover-log-contract": ("local", "ci"),
|
||||
"runtime-scripts": ("local", "ci"),
|
||||
"version": ("local", "ci"),
|
||||
"security-secrets": ("local", "ci"),
|
||||
"security-deps": ("local", "ci"),
|
||||
@@ -541,13 +630,16 @@ def self_test() -> int:
|
||||
# 完整 SHA 的形状
|
||||
if not FULL_SHA.match("a" * 40) or FULL_SHA.match("a" * 12):
|
||||
problems.append("完整 SHA 的形状判定不对(12 位短 sha 必须判为不完整)")
|
||||
# 版本契约判据的反例(F19)——与 verify 里 contracts 那一步共用同一份逻辑
|
||||
problems.extend(contract_judgement_problems())
|
||||
if problems:
|
||||
print("❌ 汇总逻辑自检失败:", file=sys.stderr)
|
||||
for p in problems:
|
||||
print(f" · {p}", file=sys.stderr)
|
||||
return 1
|
||||
print(f"✅ 汇总逻辑自检通过:{len(cases)} 个分支"
|
||||
f"(含'必需项未运行/被跳过/缺失/重复/未知项都不得报全绿')+ 清单一致性 + 完整 SHA 形状")
|
||||
print(f"✅ 汇总逻辑自检通过:{len(cases)} 个汇总分支"
|
||||
f"(含'必需项未运行/被跳过/缺失/重复/未知项都不得报全绿')"
|
||||
f" + 10 个版本契约判据反例 + 清单一致性 + 完整 SHA 形状")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user