/** * 判据总入口 —— **全部跑完再算退出码**。 * * 为什么不再用 `&&` 串起来: * * 原先 `npm test` 是 `a && b && c …`。这种行为有个不起眼但很贵的后果 —— * **前面红一条,后面全部不跑**。于是"只红了一条"看起来像"只有一个问题", * 实际上后面那些判据连跑都没跑(这次就真发生了:`background` 红着, * `packaging` 从来没跑到过,而它正是能发现"界面改了没重打包"的那条)。 * 换句话说:`&&` 链下的"全绿"是可信的,**"红"是不可信的**。 * * 现在:每条判据都跑,红的收集起来,最后一起报、一起退出。 * * 另外两条防"判据自己不会跑"的自检(与 process.exit 之后写判据是同一族问题): * 1. 清单里的文件必须存在(名字写错 = 静默跳过一条判据); * 2. `test/` 下的每个 `*.test.mjs` 都必须在清单里 * —— 这次 `cross-client-theme.test.mjs` 就是"写好了但没接进套件", * 在它进套件之前一直是隐身状态。加了这条,**新增判据忘了接线会直接红**。 */ import { spawnSync } from 'node:child_process'; import { existsSync, readFileSync, readdirSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const HERE = dirname(fileURLToPath(import.meta.url)); const ROOT = join(HERE, '..'); /** * 判据清单:[文件, 额外 node 参数]。 * * `--test` 给用 node:test 写的判据;鸿蒙那条要 `--experimental-strip-types` * 才能直接执行 `client/harmony/.../MailGrouping.ts`(判据跑的是客户端真正引用的那份逻辑)。 */ const SUITE = [ ['test/markdown-xss.test.mjs', []], ['test/narrow-layout.test.mjs', []], ['test/nav-merge.test.mjs', ['--test']], ['test/theme.test.mjs', []], ['test/background.test.mjs', []], ['test/cross-client-theme.test.mjs', ['--test']], ['test/harmony-logic.test.mjs', ['--experimental-strip-types', '--no-warnings', '--test']], ['test/harmony-system-api.test.mjs', ['--test']], // P4 外观同步:跑 model/Appearance.ts(纯逻辑),所以也要 strip-types ['test/harmony-appearance.test.mjs', ['--experimental-strip-types', '--no-warnings', '--test']], ['test/build-stamp.test.mjs', ['--test']], ['test/packaging.test.mjs', []] ]; // 自检 1:清单里的文件必须真的存在(写错名字 = 那条判据永远不跑) const ghosts = SUITE.map(([f]) => f).filter((f) => !existsSync(join(ROOT, f))); // 自检 2:test/ 下每个 *.test.mjs 都要在清单里(防"写好了没接线") const onDisk = readdirSync(join(ROOT, 'test')) .filter((f) => f.endsWith('.test.mjs')) .map((f) => `test/${f}`); const unwired = onDisk.filter((f) => !SUITE.some(([s]) => s === f)); if (ghosts.length || unwired.length) { if (ghosts.length) console.error(`清单里的判据文件不存在:${ghosts.join('、')}`); if (unwired.length) { console.error(`这些判据文件没接进套件(写了却不会跑):${unwired.join('、')}`); } process.exit(1); } /* * 自检 3:判据不得写在 `process.exit()` **之后**(pi 提议,2026-09-14)。 * * 自检 1/2 管的是"文件没接线",管不到"检查写在了退出之后" —— 而那正是实际发生过的 * 第 4 例:4 条玻璃判据被并发写入落到了文件末尾、`process.exit()` 后面, * 于是**一条都不执行、也不计入通过/失败**,输出看起来完全正常。 * 这种事的成因是结构性的(并发写入总是往文件末尾追加),所以它一定会再发生, * 而它下一次仍然不报错 —— 静态扫一遍最省事。 */ const buried = []; for (const [file, flags] of SUITE) { if (flags.includes('--test')) { continue; // node:test 那几条没有 process.exit,结构上不会踩这个 } const src = readFileSync(join(ROOT, file), 'utf8'); const exitAt = src.lastIndexOf('process.exit('); if (exitAt >= 0 && /(^|\n)\s*check\(/.test(src.slice(exitAt))) { buried.push(file); } } if (buried.length) { console.error(`判据写在 process.exit() 之后,永远不会跑(挪到汇总之前):${buried.join('、')}`); process.exit(1); } const reds = []; for (const [file, flags] of SUITE) { console.log(`\n========== ${file} ==========`); const r = spawnSync(process.execPath, [...flags, join(ROOT, file)], { stdio: 'inherit' }); // 不 break:后面每条都要跑出来,否则"红了几条"这个信息本身是假的 if (r.status !== 0) reds.push(`${file}(退出码 ${r.status})`); } console.log(`\n========== 判据汇总 ==========`); if (reds.length === 0) { console.log(`全部通过(${SUITE.length} 个判据文件:${SUITE.map(([f]) => f.replace('test/', '').replace('.test.mjs', '')).join('、')})`); process.exit(0); } console.error(`红的判据(${reds.length}/${SUITE.length}):`); for (const r of reds) console.error(` - ${r}`); process.exit(1);