1f74794696
一个"永远返回 0 的校验器"比没有校验更糟 —— 它给人一种已经检查过的错觉。 所以每条判据都要有反例,而且反例必须在**隔离副本**里做,不往日常 main 推故意失败的提交。 scripts/ci_negative_fixtures.sh 的两道保险:必须显式设 CI_NEGATIVE_FIXTURE=1, 且工作区有 origin 远端时拒绝运行。逐项做完即恢复,并断言恢复后会重新变绿。 覆盖:格式(Go / 前端各一条)、前端用例失败、变更记录缺日期、版本用例被 SKIP(变异测试)、 缺锁文件、例外表缺失、合成密钥、事件 SHA 错配、必需项未运行不得报全绿(全量跑一次看 not_run)、 通知不可达不改质量结论、汇总报告缺失必须非 0。 实测(在 /home/zheromiku/.hermes/cache/scratch/kg-neg 的副本里,副本无 origin): 21 条全部符合预期、0 失败,夹具退出码 0。 顺带修掉一个真问题:夹具第一稿把"注入路径写错"改在 build-go 那一处(同名但无关), 于是看起来"契约没红"—— 其实是夹具没生效。现在断言注入点在文件里唯一命中。
157 lines
8.6 KiB
Bash
157 lines
8.6 KiB
Bash
#!/usr/bin/env bash
|
||
# 反向验收夹具:**只在隔离副本里跑**。它故意弄坏东西,断言门禁会红;再恢复,断言会绿。
|
||
#
|
||
# 为什么要有它:一个"永远返回 0 的校验器"比没有校验更糟 —— 它给人一种已经检查过的错觉
|
||
# (正本 §0-2)。所以每条判据都要有反例,而且反例要在**隔离副本**里做,
|
||
# 绝不往日常 main 上推故意失败的提交(正本 §10)。
|
||
#
|
||
# 用法(在副本里):
|
||
# cp -a <repo> /tmp/kg-neg && cd /tmp/kg-neg && CI_NEGATIVE_FIXTURE=1 bash scripts/ci_negative_fixtures.sh
|
||
#
|
||
# 两道保险,防止有人在本仓里直接跑:
|
||
# 1. 必须显式设 CI_NEGATIVE_FIXTURE=1
|
||
# 2. 本仓有 origin 指向真实 Gitea 时拒绝运行(副本里通常没有 origin)
|
||
set -uo pipefail
|
||
|
||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||
cd "$ROOT"
|
||
|
||
if [ "${CI_NEGATIVE_FIXTURE:-}" != "1" ]; then
|
||
echo "❌ 拒绝运行:本脚本会故意弄坏文件。先设 CI_NEGATIVE_FIXTURE=1,并确认自己在隔离副本里。" >&2
|
||
exit 2
|
||
fi
|
||
if git remote get-url origin >/dev/null 2>&1; then
|
||
echo "❌ 拒绝运行:这个工作区有 origin 远端($(git remote get-url origin))—— 反向夹具只在无远端的副本里跑。" >&2
|
||
exit 2
|
||
fi
|
||
|
||
PY="${PYTHON:-python3}"
|
||
pass=0; fail=0
|
||
say() { printf '\n\033[1m== %s ==\033[0m\n' "$*"; }
|
||
ok() { pass=$((pass+1)); echo " ✅ $1"; }
|
||
no() { fail=$((fail+1)); echo " ❌ $1"; }
|
||
|
||
# expect <期望退出码> <名字> -- <命令...>
|
||
expect() {
|
||
local want="$1" name="$2"; shift 3
|
||
local out; out="$("$@" 2>&1)"; local got=$?
|
||
if [ "$got" = "$want" ]; then ok "$name(退出码 $got,符合预期)"
|
||
else no "$name:期望退出码 $want,实际 $got"; echo "$out" | tail -12 | sed 's/^/ /'; fi
|
||
}
|
||
|
||
[ -d web/node_modules ] || { echo "先装依赖:npm --prefix web ci" >&2; exit 2; }
|
||
|
||
say "0. 基线:现在应当全绿"
|
||
expect 0 "identity(事件 SHA = 当前 HEAD)" -- env GITHUB_SHA="$(git rev-parse HEAD)" "$PY" scripts/ci_checks.py --only identity
|
||
expect 0 "format" -- "$PY" scripts/ci_checks.py --only format
|
||
expect 0 "security-secrets" -- "$PY" scripts/ci_checks.py --only security-secrets
|
||
|
||
say "1. 格式错误必须红(Go / 前端各一条)"
|
||
printf 'package main\nfunc main( ){ println("x") }\n' > zz_fixture_bad.go
|
||
git add zz_fixture_bad.go
|
||
expect 1 "Go 未格式化 ⇒ format 红" -- "$PY" scripts/ci_checks.py --only format
|
||
git rm -q --cached zz_fixture_bad.go; rm -f zz_fixture_bad.go
|
||
|
||
printf 'export function f( x:number ){return x*2}\n' > web/src/zzFixture.ts
|
||
git -C "$ROOT" add web/src/zzFixture.ts 2>/dev/null || git add web/src/zzFixture.ts
|
||
expect 1 "前端未格式化(基线外的新文件)⇒ format 红" -- "$PY" scripts/ci_checks.py --only format
|
||
git rm -q --cached web/src/zzFixture.ts; rm -f web/src/zzFixture.ts
|
||
expect 0 "还原后 format 恢复绿" -- "$PY" scripts/ci_checks.py --only format
|
||
|
||
say "2. 前端测试失败必须让门禁红"
|
||
cat > web/src/zzFixture.test.ts <<'TS'
|
||
import { describe, expect, it } from 'vitest'
|
||
describe('fixture', () => {
|
||
it('故意失败', () => { expect(1).toBe(2) })
|
||
})
|
||
TS
|
||
expect 1 "vitest 有用例失败 ⇒ tests-web 红" -- "$PY" scripts/ci_checks.py --only tests-web
|
||
rm -f web/src/zzFixture.test.ts
|
||
expect 0 "删除后 tests-web 恢复绿" -- "$PY" scripts/ci_checks.py --only tests-web
|
||
|
||
say "3. 版本与变更记录契约"
|
||
cp CHANGELOG.md /tmp/kg-changelog.bak
|
||
python3 - <<'PY'
|
||
import pathlib
|
||
p = pathlib.Path('CHANGELOG.md')
|
||
t = p.read_text(encoding='utf-8')
|
||
# 造一个"没有日期"的已发布版本标题(结构自检必须抓住)
|
||
old = '## [0.1.0] - 2026-10-10'
|
||
assert t.count(old) == 1, f'夹具锚点不唯一({t.count(old)} 处)—— 别静默跳过'
|
||
p.write_text(t.replace(old, '## [9.9.9]', 1), encoding='utf-8')
|
||
PY
|
||
expect 1 "变更记录里的已发布版本缺日期 ⇒ contracts 红" -- "$PY" scripts/ci_checks.py --only contracts
|
||
cp /tmp/kg-changelog.bak CHANGELOG.md
|
||
|
||
# 变异测试:把**契约那一处**的注入目标路径写错 ⇒ 版本用例会 SKIP ⇒ 契约必须红(跳过不算通过)
|
||
# ⚠️ 必须只改契约那一处:ci_checks.py 里还有一处同名的注入是给 `build-go` 用的,
|
||
# 改错地方就会出现"夹具没生效、但看起来通过了"的假结论(这一版夹具第一稿就是这么错的)。
|
||
cp scripts/ci_checks.py /tmp/kg-cichecks.bak
|
||
python3 - <<'PY'
|
||
import pathlib
|
||
p = pathlib.Path('scripts/ci_checks.py')
|
||
t = p.read_text(encoding='utf-8')
|
||
needle = '-ldflags=-X knowledge-governance/internal/version.Number={version}'
|
||
assert t.count(needle) == 1, f'契约注入点不唯一({t.count(needle)} 处),夹具失效'
|
||
p.write_text(t.replace(needle, '-ldflags=-X knowledge-governance/internal/version.Nope={version}', 1),
|
||
encoding='utf-8')
|
||
PY
|
||
"$PY" scripts/ci_checks.py --only contracts > /tmp/kg-skip.log 2>&1; rc=$?
|
||
if [ "$rc" -ne 0 ] && grep -q 'SKIP' /tmp/kg-skip.log; then
|
||
ok "注入路径写错 ⇒ 版本用例被 SKIP ⇒ contracts 红(确认走的是 SKIP 分支)"
|
||
else
|
||
no "注入路径写错没有判红,或没走到 SKIP 分支(rc=$rc)"; tail -8 /tmp/kg-skip.log | sed 's/^/ /'
|
||
fi
|
||
cp /tmp/kg-cichecks.bak scripts/ci_checks.py
|
||
expect 0 "还原后 contracts 恢复绿" -- "$PY" scripts/ci_checks.py --only contracts
|
||
|
||
say "4. 依赖锁与例外文件"
|
||
mv web/package-lock.json /tmp/kg-lock.bak
|
||
expect 1 "缺 package-lock.json ⇒ deps-install 红(不退化无约束安装)" -- "$PY" scripts/ci_checks.py --only deps-install
|
||
mv /tmp/kg-lock.bak web/package-lock.json
|
||
mv security/dependency-exceptions.json /tmp/kg-exc.bak
|
||
expect 1 "例外登记表缺失 ⇒ security-deps 红(不静默放行)" -- "$PY" scripts/ci_checks.py --only security-deps
|
||
mv /tmp/kg-exc.bak security/dependency-exceptions.json
|
||
|
||
say "5. 合成密钥夹具(无真实秘密)必须被拦住"
|
||
python3 - <<'PY'
|
||
import pathlib
|
||
# 值在运行期拼出来:夹具文件里不留真命中文本
|
||
ak = "AKIA" + "Q7ZP2M4XR9TLB6VW"
|
||
pathlib.Path('zz_fixture_secret.txt').write_text(f"aws_access_key = \"{ak}\"\n", encoding='utf-8')
|
||
PY
|
||
git add zz_fixture_secret.txt
|
||
expect 1 "合成 AWS AK ⇒ security-secrets 红" -- "$PY" scripts/ci_checks.py --only security-secrets
|
||
git rm -q --cached zz_fixture_secret.txt; rm -f zz_fixture_secret.txt
|
||
|
||
say "6. 检出身份(事件 SHA 错配必须红)"
|
||
expect 1 "事件 SHA 与 HEAD 不一致 ⇒ identity 红" -- env GITHUB_SHA="0000000000000000000000000000000000000000" "$PY" scripts/ci_checks.py --only identity
|
||
expect 0 "事件 SHA 与 HEAD 一致 ⇒ identity 绿" -- env GITHUB_SHA="$(git rev-parse HEAD)" "$PY" scripts/ci_checks.py --only identity
|
||
|
||
say "7. 必需检查未运行不得报全绿(全量跑一次,故意让 format 红)"
|
||
printf 'package main\nfunc main( ){ println("x") }\n' > zz_fixture_bad.go
|
||
git add zz_fixture_bad.go
|
||
"$PY" scripts/ci_checks.py > /tmp/kg-neg-gate.log 2>&1; rc=$?
|
||
if [ "$rc" -ne 0 ]; then ok "全量门禁非 0(退出码 $rc)"; else no "全量门禁竟然返回 0"; fi
|
||
python3 - <<'PY' && ok "报告里后续必需项被记为 not_run,且总体为 failure" || no "报告的 not_run / overall 断言不成立"
|
||
import json, sys
|
||
d = json.load(open('ci-report.json'))
|
||
by = {c['id']: c['status'] for c in d['checks']}
|
||
assert by.get('format') == 'failure', by
|
||
later = [by.get(k) for k in ('tests-go', 'tests-web', 'contracts', 'security-deps')]
|
||
assert all(x == 'not_run' for x in later if x is not None), by
|
||
assert d['overall_status'] == 'failure', d['overall_status']
|
||
assert any('not_run' in p or '未运行' in p for p in d['problems']), d['problems']
|
||
PY
|
||
git rm -q --cached zz_fixture_bad.go; rm -f zz_fixture_bad.go
|
||
|
||
say "8. 上报:通知失败不改变质量结果;汇总不可用必须非 0"
|
||
printf '{"schema":"ci-report/1","overall_status":"success","policy_revision":"ci-checks/1","repository":"fixture/local","commit_sha":"%s","checks":[{"id":"format","status":"success","reason":"ok","duration_s":1}],"duration_s":2}' "$(git rev-parse HEAD)" > /tmp/kg-report-ok.json
|
||
CI_BRIDGE_RESOLVED="http://127.0.0.1:1" "$PY" .gitea/scripts/ci_report.py --report /tmp/kg-report-ok.json > /tmp/kg-notify.log 2>&1
|
||
rc=$?
|
||
if [ "$rc" = 0 ]; then ok "通知不可达时退出码仍是 0(质量结论不变)"; else no "通知失败把退出码带成了 $rc"; fi
|
||
if grep -q '"notification_status": "failed' /tmp/kg-report-ok.json; then ok "通知失败被单独标注(notification_status=failed)"; else no "没看到 notification_status 被标注"; fi
|
||
expect 1 "汇总报告缺失 ⇒ 上报脚本非 0(不能报绿)" -- "$PY" .gitea/scripts/ci_report.py --report /tmp/kg-does-not-exist.json
|
||
|
||
printf '\n\033[1m════ 反向验收汇总 ════\033[0m\n ✅ 通过 %d | ❌ 失败 %d\n' "$pass" "$fail"
|
||
[ "$fail" -eq 0 ] || exit 1 |