Skip to content

Commit 5d59d00

Browse files
jasnelladuh95
authored andcommitted
test: make test-doc-api-json more robust
The test hard coded the number of deprecated apis so whenever one is added or removed it always needs to be updated. This makes the check more robust by calculating the values on each run. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #67701 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent 91414a1 commit 5d59d00

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

‎test/doctool/test-doc-api-json.mjs‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,25 @@ function validateMisc(misc) {
2828
assert.strictEqual(typeof misc.textRaw, 'string');
2929
}
3030

31+
// Counts of `deprecated` and `removed` metadata found in the current file's
32+
// JSON output. Reset for each file and compared against the Markdown source.
3133
let numberOfDeprecatedSections = 0;
3234
let numberOfRemovedAPIs = 0;
35+
let totalDeprecatedSections = 0;
36+
let totalRemovedAPIs = 0;
37+
38+
// Counts the YAML metadata blocks in a Markdown source file that declare
39+
// `deprecated` or `removed`, so the JSON output can be checked against them.
40+
const yamlBlockRe = /<!--\s*YAML\r?\n([\s\S]*?)-->/g;
41+
function countSourceMeta(markdown) {
42+
let deprecated = 0;
43+
let removed = 0;
44+
for (const { 1: body } of markdown.matchAll(yamlBlockRe)) {
45+
if (/^\s*deprecated:/m.test(body)) deprecated++;
46+
if (/^\s*removed:/m.test(body)) removed++;
47+
}
48+
return { deprecated, removed };
49+
}
3350

3451
const metaExpectedKeys = new Set([
3552
'added',
@@ -155,8 +172,22 @@ for await (const dirent of await fs.opendir(new URL('../../out/doc/api/', import
155172
'report.md': ['introduced_in', 'meta', 'stability', 'stabilityText', 'miscs'],
156173
}[dirent.name] ?? ['modules'])]);
157174

175+
numberOfDeprecatedSections = 0;
176+
numberOfRemovedAPIs = 0;
158177
assert.partialDeepStrictEqual(allExpectedKeys, findAllKeys(json));
178+
179+
const markdown = await fs.readFile(
180+
new URL(`../../${expectedSource}`, import.meta.url), 'utf8');
181+
assert.deepStrictEqual(
182+
{ deprecated: numberOfDeprecatedSections, removed: numberOfRemovedAPIs },
183+
countSourceMeta(markdown),
184+
`deprecated/removed metadata in ${jsonPath} does not match ${expectedSource}`,
185+
);
186+
totalDeprecatedSections += numberOfDeprecatedSections;
187+
totalRemovedAPIs += numberOfRemovedAPIs;
159188
}
160189

161-
assert.strictEqual(numberOfDeprecatedSections, 51); // Increase this number every time a new API is deprecated.
162-
assert.strictEqual(numberOfRemovedAPIs, 49); // Increase this number every time a section is marked as removed.
190+
// Guard against the source scan and the JSON output both silently finding
191+
// nothing (e.g. if the YAML metadata format changes).
192+
assert.ok(totalDeprecatedSections > 0);
193+
assert.ok(totalRemovedAPIs > 0);

0 commit comments

Comments
 (0)