• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import * as common from '../common/index.mjs';
2
3import assert from 'assert';
4import fs from 'fs';
5import path from 'path';
6
7if (common.isWindows) {
8  common.skip('`make doc` does not run on Windows');
9}
10
11// This tests that `make doc` generates the documentation properly.
12// Note that for this test to pass, `make doc` must be run first.
13
14const apiURL = new URL('../../out/doc/api/', import.meta.url);
15const mdURL = new URL('../../doc/api/', import.meta.url);
16const allMD = fs.readdirSync(mdURL);
17const allDocs = fs.readdirSync(apiURL);
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(new URL('./index.html', apiURL), '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.notStrictEqual(
60    fs.statSync(new URL(`./${actualDoc}`, apiURL)).size,
61    0,
62    `${actualDoc} is empty`
63  );
64}
65