|
| 1 | +import test from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import fs from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | + |
| 7 | +// Ensures executable paths referenced in the docs and README (e.g. |
| 8 | +// `./build/release/examples/02-memory-cache/aos_vs_soa_bench`) correspond to |
| 9 | +// real CMake targets. Executables are emitted flat into |
| 10 | +// build/<preset>/examples/<module>/ (there is no src/ or bench/ output |
| 11 | +// subdirectory), and benchmark targets are named `<NAME>_bench`. Both facts |
| 12 | +// drifted in the docs before after the target-naming refactor; this test runs |
| 13 | +// as part of `npm test` (CI docs job) to keep them pinned. |
| 14 | + |
| 15 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 16 | +const docsRoot = path.resolve(__dirname, '..', '..') // docs/ |
| 17 | +const projectRoot = path.resolve(docsRoot, '..') // repo root |
| 18 | + |
| 19 | +// --- Collect real targets from examples/*/CMakeLists.txt --------------------- |
| 20 | + |
| 21 | +const targets = new Set() |
| 22 | +const examplesDir = path.join(projectRoot, 'examples') |
| 23 | + |
| 24 | +for (const entry of fs.readdirSync(examplesDir, { withFileTypes: true })) { |
| 25 | + if (!entry.isDirectory() || !/^[0-9][0-9]-/.test(entry.name)) continue |
| 26 | + const cmakeFile = path.join(examplesDir, entry.name, 'CMakeLists.txt') |
| 27 | + if (!fs.existsSync(cmakeFile)) continue |
| 28 | + const content = fs.readFileSync(cmakeFile, 'utf8') |
| 29 | + // hpc_add_example(NAME foo ...) → `foo`, plus `foo_bench` when |
| 30 | + // BENCHMARK_SOURCES is given. hpc_add_benchmark(NAME foo ...) → `foo`. |
| 31 | + for (const call of content.matchAll(/hpc_add_(example|benchmark)\s*\(([^)]*)\)/g)) { |
| 32 | + const [, kind, body] = call |
| 33 | + const name = /NAME\s+([A-Za-z0-9_]+)/.exec(body)?.[1] |
| 34 | + if (!name) continue |
| 35 | + targets.add(name) |
| 36 | + if (kind === 'example' && /BENCHMARK_SOURCES/.test(body)) { |
| 37 | + targets.add(`${name}_bench`) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// --- Collect referenced binaries from docs + README -------------------------- |
| 43 | + |
| 44 | +const mdFiles = [path.join(projectRoot, 'README.md')] |
| 45 | +function walk(dir) { |
| 46 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 47 | + const full = path.join(dir, entry.name) |
| 48 | + if (entry.isDirectory()) walk(full) |
| 49 | + else if (entry.name.endsWith('.md')) mdFiles.push(full) |
| 50 | + } |
| 51 | +} |
| 52 | +walk(path.join(docsRoot, 'zh')) |
| 53 | + |
| 54 | +const problems = [] |
| 55 | +const checked = new Set() |
| 56 | + |
| 57 | +for (const file of mdFiles) { |
| 58 | + const content = fs.readFileSync(file, 'utf8') |
| 59 | + const where = path.relative(projectRoot, file) |
| 60 | + |
| 61 | + // Binary references: build/<preset>/examples/<module>/<binary>. The binary |
| 62 | + // token stops at whitespace/backticks, so trailing flags are not captured. |
| 63 | + for (const match of content.matchAll( |
| 64 | + /build\/(?:release|debug)\/examples\/([0-9][0-9]-[a-z-]*)\/([A-Za-z0-9_.-]+)/g |
| 65 | + )) { |
| 66 | + const [, module, binary] = match |
| 67 | + const key = `${module}/${binary}` |
| 68 | + if (checked.has(key)) continue |
| 69 | + checked.add(key) |
| 70 | + if (!targets.has(binary)) { |
| 71 | + problems.push({ key, where, reason: '无此 CMake 目标(目标已重命名?)' }) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Binaries are emitted flat into the module directory; src/ and bench/ are |
| 76 | + // source-tree directories only and never appear in output paths. |
| 77 | + for (const match of content.matchAll( |
| 78 | + /build\/(?:release|debug)\/examples\/[0-9][0-9]-[a-z-]*\/(?:src|bench)\/[A-Za-z0-9_.-]+/g |
| 79 | + )) { |
| 80 | + problems.push({ key: match[0], where, reason: '二进制路径含 src/ 或 bench/ 前缀(输出目录无此层级)' }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +test('markdown binary-path references resolve to real CMake targets', () => { |
| 85 | + assert.ok(targets.size > 0, '未能从 examples/*/CMakeLists.txt 解析出任何目标——解析器坏了吗?') |
| 86 | + assert.deepEqual( |
| 87 | + problems, |
| 88 | + [], |
| 89 | + `文档引用了不存在的可执行文件路径(目标重命名/移动后文档未同步):\n${problems |
| 90 | + .map((p) => ` ${p.key} (in ${p.where}) — ${p.reason}`) |
| 91 | + .join('\n')}` |
| 92 | + ) |
| 93 | +}) |
0 commit comments