Files

54 lines
2.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
// e2e 门禁 spec 自动发现。
//
// 动机:scripts/serve-test.js 过去**硬编码**约 33 个 spec 路径。新写的 spec 只要忘了手动加进清单,
// 就永远不会被门禁跑到——而且是**静默**的(standards §13.20 早就写下"等于没有守护",但一直是纸面要求)。
// 现在改为自动扫描 tests/*.spec.js:唯一的排除规则是**下划线前缀**(_*.spec.js = 人工 dev 探针),
// 且被排除的探针文件**必须**显式带 `iwb-dev-probe` 标记注释;否则 discoverSpecs 直接抛错。
// 即:把"静默排除"变成"必须显式声明"。
const fs = require('fs')
const path = require('path')
const SPEC_SUFFIX = '.spec.js'
const EXCLUDED_PREFIX = '_'
const PROBE_MARKER = 'iwb-dev-probe'
function listSpecFileNames(dir) {
return fs
.readdirSync(dir, { withFileTypes: true })
.filter((entry) => entry.isFile() && entry.name.endsWith(SPEC_SUFFIX))
.map((entry) => entry.name)
.sort()
}
// 返回 { specs, excluded }:
// specs = 进入常规门禁的 spec 路径(testsDir 原样拼接文件名,便于直接交给 Playwright CLI)
// excluded = 被排除的 dev 探针路径(下划线前缀 + 已声明 iwb-dev-probe 标记)
// 不变量:specs.length + excluded.length === 磁盘上全部 *.spec.js,且两集合无交集。
function discoverSpecs(testsDir) {
const dir = path.resolve(testsDir)
const specs = []
const excluded = []
for (const name of listSpecFileNames(dir)) {
const full = path.join(testsDir, name)
if (!name.startsWith(EXCLUDED_PREFIX)) {
specs.push(full)
continue
}
const text = fs.readFileSync(path.join(dir, name), 'utf8')
if (!text.includes(PROBE_MARKER)) {
throw new Error(
`[list-e2e-specs] "${name}" 以 "_" 开头(会被排除出 e2e 门禁),但缺少 "${PROBE_MARKER}" 标记注释。` +
`请在文件顶部加一行注释(例如 "// ${PROBE_MARKER}: 手动 drag/zoom/pan 探针,不入常规门禁"),` +
'或去掉下划线前缀让它进入常规门禁。'
)
}
excluded.push(full)
}
return { specs, excluded }
}
module.exports = { discoverSpecs, PROBE_MARKER, SPEC_SUFFIX }