13.3 发布检查与质量门禁
门禁分两级:block 失败阻止发布,warn 失败只记录。发布结论只看 block;报告同时列出两级的失败项。空门禁视为通过——没有检查就不该拦人,但也没提供任何保证。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 自动化测试 | 本卷 14.1 | 14.1 产生检查结果;本节把结果转成发布结论 |
| 构建 | 本卷 13.1 | 13.1 产出构建物;本节决定能不能发 |
| 全卷脉络 | 本卷目录 | 本节收口构建发布章 |
从零实现:发布检查与质量门禁
评估遍历门禁:通过项跳过,失败项按严重级别进入两张清单。发布结论只由阻塞清单是否为空决定;报告同时给出总门禁数,便于界面展示「32 项检查通过 30 项」这样的概括。
对标构建接口
Unity 构建前回调可作为执行检查的入口。helio 的 evaluateGates 消费已经计算好的检查结果,汇总阻断项和警告。检查执行与发布决策由调用方接入。 Unity 构建前回调
可运行产物:6 条用例
下面是 src/release-gates.ts,层级 L1,完整可编译。运行命令 node --test test/13-3-release-check-gates.test.ts:
// file: src/release-gates.ts
// tier: L1
// verify: node --test test/13-3-release-check-gates.test.ts
//
// 卷四(第十三章):发布检查与质量门禁。
// 门禁分两级:block 失败阻止发布,warn 失败只记录。发布结论只看 block;
// 报告同时列出两级的失败项。空门禁视为通过——没有检查就不该拦人,但也没
// 提供任何保证。
export type GateSeverity = 'block' | 'warn';
export interface Gate {
id: string;
severity: GateSeverity;
passed: boolean;
}
export interface GateReport {
release: boolean;
blocking: string[];
warnings: string[];
total: number;
}
export function evaluateGates(gates: Gate[]): GateReport {
const blocking: string[] = [];
const warnings: string[] = [];
for (const gate of gates) {
if (gate.passed) continue;
if (gate.severity === 'block') blocking.push(gate.id);
else warnings.push(gate.id);
}
return {
release: blocking.length === 0,
blocking,
warnings,
total: gates.length,
};
}
只分两级是刻意的:门禁一旦有三级以上,团队就会争论某项该算哪级,而不是去修它。warn 项保留在报告里,让技术债可见但不阻塞发布;如果某条 warn 长期无人处理,应当把它升级为 block。
下面是 test/13-3-release-check-gates.test.ts,层级 L1:
// file: test/13-3-release-check-gates.test.ts
// tier: L1
// verify: node --test test/13-3-release-check-gates.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { evaluateGates } from '../src/release-gates.ts';
const gate = (id: string, severity: 'block' | 'warn', passed: boolean) => ({ id, severity, passed });
test('全部通过可发布', () => {
const report = evaluateGates([gate('lint', 'block', true), gate('size', 'warn', true)]);
assert.equal(report.release, true);
assert.deepEqual(report.blocking, []);
assert.equal(report.total, 2);
});
test('block 失败阻止发布', () => {
const report = evaluateGates([gate('smoke', 'block', false)]);
assert.equal(report.release, false);
assert.deepEqual(report.blocking, ['smoke']);
});
test('warn 失败不阻止发布', () => {
const report = evaluateGates([gate('size', 'warn', false)]);
assert.equal(report.release, true);
assert.deepEqual(report.warnings, ['size']);
});
test('多个失败按门禁顺序列出', () => {
const report = evaluateGates([
gate('a', 'block', false),
gate('b', 'warn', false),
gate('c', 'block', false),
]);
assert.deepEqual(report.blocking, ['a', 'c']);
assert.deepEqual(report.warnings, ['b']);
});
test('空门禁视为通过', () => {
assert.deepEqual(evaluateGates([]), { release: true, blocking: [], warnings: [], total: 0 });
});
test('统计包含通过项', () => {
const report = evaluateGates([
gate('a', 'block', true),
gate('b', 'block', false),
gate('c', 'warn', false),
]);
assert.equal(report.total, 3);
assert.equal(report.release, false);
});
本机实测(Node v26.5.0,node --test test/13-3-release-check-gates.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉全部通过与阻塞;第三条钉警告不阻塞;第四条钉多失败顺序;后两条钉空门禁与总数。
小结
- 门禁只有两级:阻塞与警告。
- 发布结论只看阻塞项,警告只记录。
- 空门禁视为通过,但提供不了任何保证。
- 报告保留总数,便于展示通过比例。
阅读导航:上一节:13.2 构建配置与自动化 · 下一节:14.1 编辑器冒烟测试 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。