mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
`make test-addons` used to depend on a markdown parser and then doc-kit to extract C++ addon examples from addons.md by guessing the file contents based on headings. This is hacky and brittle. The introduction of doc-kit also means tests intended for verifying the binary like `make test-only` now need to support doc-building toolchains e.g. minifier, highlighter, and indirect dependencies that rely on prebuilt-addon/wasm, which defeats the purpose and makes it harder to run for experimental platforms. This patch adds explicit `<!-- addon-verify-file dir/filename -->` markers in addons.md to locate extractable code blocks, avoiding fragile heuristics based on heading text or code block order and eliminating the dependency with simpler parsing. Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/62388 Fixes: https://github.com/nodejs/node/issues/62385 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Richard Lau <richard.lau@ibm.com>
103 lines
2.7 KiB
JavaScript
Executable File
103 lines
2.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
// Extracts C++ addon examples from doc/api/addons.md into numbered test
|
|
// directories under test/addons/.
|
|
//
|
|
// Each code block to extract is preceded by a marker in the markdown:
|
|
//
|
|
// <!-- addon-verify-file worker_support/addon.cc -->
|
|
// ```cpp
|
|
// #include <node.h>
|
|
// ...
|
|
// ```
|
|
//
|
|
// This produces test/addons/01_worker_support/addon.cc.
|
|
// Sections are numbered in order of first appearance.
|
|
|
|
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
import { open } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { parseArgs } from 'node:util';
|
|
|
|
const { values } = parseArgs({
|
|
options: {
|
|
input: { type: 'string' },
|
|
output: { type: 'string' },
|
|
},
|
|
});
|
|
|
|
if (!values.input || !values.output) {
|
|
console.error('Usage: addon-verify.mjs --input <file> --output <dir>');
|
|
process.exit(1);
|
|
}
|
|
|
|
const src = await open(values.input, 'r');
|
|
|
|
const MARKER_RE = /^<!--\s*addon-verify-file\s+(\S+?)\/(\S+)\s*-->$/;
|
|
|
|
const entries = [];
|
|
let nextBlockIsAddonVerifyFile = false;
|
|
let expectedClosingFenceMarker;
|
|
for await (const line of src.readLines()) {
|
|
if (expectedClosingFenceMarker) {
|
|
// We're inside a Addon snippet
|
|
if (line === expectedClosingFenceMarker) {
|
|
// End of the snippet
|
|
expectedClosingFenceMarker = null;
|
|
continue;
|
|
}
|
|
|
|
entries.at(-1).content += `${line}\n`;
|
|
}
|
|
if (nextBlockIsAddonVerifyFile) {
|
|
if (line) {
|
|
expectedClosingFenceMarker = line.replace(/\w/g, '');
|
|
nextBlockIsAddonVerifyFile = false;
|
|
}
|
|
continue;
|
|
}
|
|
const match = MARKER_RE.exec(line);
|
|
if (match) {
|
|
nextBlockIsAddonVerifyFile = true;
|
|
const [, dir, name] = match;
|
|
entries.push({ dir, name, content: '' });
|
|
}
|
|
}
|
|
|
|
// Collect files grouped by section directory name.
|
|
const sections = Map.groupBy(entries, (e) => e.dir);
|
|
|
|
let idx = 0;
|
|
for (const [name, files] of sections) {
|
|
const dirName = `${String(++idx).padStart(2, '0')}_${name}`;
|
|
const dir = join(values.output, dirName);
|
|
mkdirSync(dir, { recursive: true });
|
|
|
|
for (const file of files) {
|
|
let content = file.content;
|
|
if (file.name === 'test.js') {
|
|
content =
|
|
"'use strict';\n" +
|
|
"const common = require('../../common');\n" +
|
|
content.replace(
|
|
"'./build/Release/addon'",
|
|
// eslint-disable-next-line no-template-curly-in-string
|
|
'`./build/${common.buildType}/addon`',
|
|
);
|
|
}
|
|
writeFileSync(join(dir, file.name), content);
|
|
}
|
|
|
|
// Generate binding.gyp
|
|
const names = files.map((f) => f.name);
|
|
writeFileSync(join(dir, 'binding.gyp'), JSON.stringify({
|
|
targets: [{
|
|
target_name: 'addon',
|
|
sources: names,
|
|
includes: ['../common.gypi'],
|
|
}],
|
|
}));
|
|
|
|
console.log(`Generated ${dirName} with files: ${names.join(', ')}`);
|
|
}
|