test: amplify and optimize doctool/test-make-doc

PR-URL: https://github.com/nodejs/node/pull/19581
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Vse Mozhet Byt 2018-03-25 01:56:10 +02:00
parent 1a85328264
commit b5884fba9a

View File

@ -12,8 +12,13 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const apiPath = path.resolve(__dirname, '..', '..', 'out', 'doc', 'api'); const apiPath = path.resolve(__dirname, '..', '..', 'out', 'doc', 'api');
const docs = fs.readdirSync(apiPath); const allDocs = fs.readdirSync(apiPath);
assert.ok(docs.includes('_toc.html')); assert.ok(allDocs.includes('_toc.html'));
const filter = ['assets', '_toc.html', '.md'];
const actualDocs = allDocs.filter(
(name) => !filter.some((str) => name.includes(str))
);
const toc = fs.readFileSync(path.resolve(apiPath, '_toc.html'), 'utf8'); const toc = fs.readFileSync(path.resolve(apiPath, '_toc.html'), 'utf8');
const re = /href="([^/]+\.html)"/; const re = /href="([^/]+\.html)"/;
@ -21,23 +26,26 @@ const globalRe = new RegExp(re, 'g');
const links = toc.match(globalRe); const links = toc.match(globalRe);
assert.notStrictEqual(links, null); assert.notStrictEqual(links, null);
// Test that all the relative links in the TOC of the documentation // Filter out duplicate links, leave just filenames, add expected JSON files.
// work and all the generated documents are linked in TOC. const linkedHtmls = [...new Set(links)].map((link) => link.match(re)[1]);
const linkedHtmls = links.map((link) => link.match(re)[1]); const expectedJsons = linkedHtmls
for (const html of linkedHtmls) { .map((name) => name.replace('.html', '.json'))
assert.ok(docs.includes(html), `${html} does not exist`); .concat('_toc.json');
const expectedDocs = linkedHtmls.concat(expectedJsons);
// Test that all the relative links in the TOC match to the actual documents.
for (const expectedDoc of expectedDocs) {
assert.ok(actualDocs.includes(expectedDoc), `${expectedDoc} does not exist`);
} }
const excludes = ['.json', '.md', '_toc', 'assets']; // Test that all the actual documents match to the relative links in the TOC
const generatedHtmls = docs.filter(function(doc) { // and that they are not empty files.
for (const exclude of excludes) { for (const actualDoc of actualDocs) {
if (doc.includes(exclude)) { assert.ok(
return false; expectedDocs.includes(actualDoc), `${actualDoc} does not not match TOC`);
}
}
return true;
});
for (const html of generatedHtmls) { assert.ok(
assert.ok(linkedHtmls.includes(html), `${html} is not linked in toc`); fs.statSync(path.join(apiPath, actualDoc)).size !== 0,
`${actualDoc} is empty`
);
} }