• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (common.isWindows) {
4  common.skip('`make doc` does not run on Windows');
5}
6
7// This tests that `make doc` generates the documentation properly.
8// Note that for this test to pass, `make doc` must be run first.
9
10const assert = require('assert');
11const fs = require('fs');
12const path = require('path');
13
14const apiPath = path.resolve(__dirname, '..', '..', 'out', 'doc', 'api');
15const mdPath = path.resolve(__dirname, '..', '..', 'doc', 'api');
16const allMD = fs.readdirSync(mdPath);
17const allDocs = fs.readdirSync(apiPath);
18assert.ok(allDocs.includes('index.html'));
19
20const actualDocs = allDocs.filter(
21  (name) => {
22    const extension = path.extname(name);
23    return extension === '.html' || extension === '.json';
24  }
25);
26
27for (const name of actualDocs) {
28  if (name.startsWith('all.')) continue;
29
30  assert.ok(
31    allMD.includes(name.replace(/\.\w+$/, '.md')),
32    `Unexpected output: out/doc/api/${name}, remove and rerun.`
33  );
34}
35
36const toc = fs.readFileSync(path.resolve(apiPath, 'index.html'), 'utf8');
37const re = /href="([^/]+\.html)"/;
38const globalRe = new RegExp(re, 'g');
39const links = toc.match(globalRe);
40assert.notStrictEqual(links, null);
41
42// Filter out duplicate links, leave just filenames, add expected JSON files.
43const linkedHtmls = [...new Set(links)].map((link) => link.match(re)[1]);
44const expectedJsons = linkedHtmls
45                       .map((name) => name.replace('.html', '.json'));
46const expectedDocs = linkedHtmls.concat(expectedJsons);
47
48// Test that all the relative links in the TOC match to the actual documents.
49for (const expectedDoc of expectedDocs) {
50  assert.ok(actualDocs.includes(expectedDoc), `${expectedDoc} does not exist`);
51}
52
53// Test that all the actual documents match to the relative links in the TOC
54// and that they are not empty files.
55for (const actualDoc of actualDocs) {
56  assert.ok(
57    expectedDocs.includes(actualDoc), `${actualDoc} does not match TOC`);
58
59  assert.ok(
60    fs.statSync(path.join(apiPath, actualDoc)).size !== 0,
61    `${actualDoc} is empty`
62  );
63}
64