From b5884fba9afb2df84137aaf746c3f5d23ac2f959 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 25 Mar 2018 01:56:10 +0200 Subject: test: amplify and optimize doctool/test-make-doc PR-URL: https://github.com/nodejs/node/pull/19581 Reviewed-By: Ruben Bridgewater Reviewed-By: Joyee Cheung --- test/doctool/test-make-doc.js | 46 +++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'test/doctool') diff --git a/test/doctool/test-make-doc.js b/test/doctool/test-make-doc.js index 74845b2444..b2bc7a05f6 100644 --- a/test/doctool/test-make-doc.js +++ b/test/doctool/test-make-doc.js @@ -12,8 +12,13 @@ const fs = require('fs'); const path = require('path'); const apiPath = path.resolve(__dirname, '..', '..', 'out', 'doc', 'api'); -const docs = fs.readdirSync(apiPath); -assert.ok(docs.includes('_toc.html')); +const allDocs = fs.readdirSync(apiPath); +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 re = /href="([^/]+\.html)"/; @@ -21,23 +26,26 @@ const globalRe = new RegExp(re, 'g'); const links = toc.match(globalRe); assert.notStrictEqual(links, null); -// Test that all the relative links in the TOC of the documentation -// work and all the generated documents are linked in TOC. -const linkedHtmls = links.map((link) => link.match(re)[1]); -for (const html of linkedHtmls) { - assert.ok(docs.includes(html), `${html} does not exist`); +// Filter out duplicate links, leave just filenames, add expected JSON files. +const linkedHtmls = [...new Set(links)].map((link) => link.match(re)[1]); +const expectedJsons = linkedHtmls + .map((name) => name.replace('.html', '.json')) + .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']; -const generatedHtmls = docs.filter(function(doc) { - for (const exclude of excludes) { - if (doc.includes(exclude)) { - return false; - } - } - return true; -}); - -for (const html of generatedHtmls) { - assert.ok(linkedHtmls.includes(html), `${html} is not linked in toc`); +// Test that all the actual documents match to the relative links in the TOC +// and that they are not empty files. +for (const actualDoc of actualDocs) { + assert.ok( + expectedDocs.includes(actualDoc), `${actualDoc} does not not match TOC`); + + assert.ok( + fs.statSync(path.join(apiPath, actualDoc)).size !== 0, + `${actualDoc} is empty` + ); } -- cgit v1.2.3