Skip to content

Commit 31315b2

Browse files
test: add tests to ensure that node.1 is kept in sync with cli.md
add tests to make sure that the content of the doc/node.1 file is kept in snyc with the content of the doc/api/cli.md file (to make sure that when a flag or environment variable is added or removed to one, the same change is also applied to the other)
1 parent 4d5ee24 commit 31315b2

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import '../common/index.mjs';
2+
import assert from 'assert';
3+
import { createReadStream } from 'node:fs';
4+
import { createInterface } from 'node:readline';
5+
import { resolve, join } from 'node:path';
6+
7+
// This test checks that all the environment variables defined in the public CLI documentation (doc/api/cli.md)
8+
// are also documented in the manpage file (doc/node.1) and vice-versa (that all the environment variables
9+
// in the manpage are present in the CLI documentation)
10+
11+
const rootDir = resolve(import.meta.dirname, '..', '..');
12+
13+
const cliMdEnvVarNames = await collectCliMdEnvVarNames();
14+
const manpageEnvVarNames = await collectManPageEnvVarNames();
15+
16+
assert(cliMdEnvVarNames.size > 0,
17+
'Unexpectedly not even a single env variable was detected when scanning the `doc/api/cli.md` file'
18+
);
19+
20+
assert(manpageEnvVarNames.size > 0,
21+
'Unexpectedly not even a single env variable was detected when scanning the `doc/node.1` file'
22+
);
23+
24+
// TODO(dario-piotrowicz): add the missing env variables to the manpage and remove this set
25+
const knownEnvVariablesMissingFromManPage = new Set([
26+
'NODE_COMPILE_CACHE',
27+
'NODE_DISABLE_COMPILE_CACHE',
28+
'NODE_PENDING_PIPE_INSTANCES',
29+
'NODE_TEST_CONTEXT',
30+
'NODE_USE_ENV_PROXY',
31+
]);
32+
33+
for (const envVarName of cliMdEnvVarNames) {
34+
if (!manpageEnvVarNames.has(envVarName) && !knownEnvVariablesMissingFromManPage.has(envVarName)) {
35+
assert.fail(`The "${envVarName}" environment variable (present in \`doc/api/cli.md\`) is missing from the \`doc/node.1\` file`);
36+
}
37+
manpageEnvVarNames.delete(envVarName);
38+
}
39+
40+
if (manpageEnvVarNames.size > 0) {
41+
assert.fail(`The following env variables are present in the \`doc/node.1\` file but not in the \`doc/api/cli.md\` file: ${
42+
[...manpageEnvVarNames].map((name) => `"${name}"`).join(', ')
43+
}`);
44+
}
45+
46+
async function collectManPageEnvVarNames() {
47+
const manPagePath = join(rootDir, 'doc', 'node.1');
48+
const fileStream = createReadStream(manPagePath);
49+
50+
const rl = createInterface({
51+
input: fileStream,
52+
});
53+
54+
const envVarNames = new Set();
55+
56+
for await (const line of rl) {
57+
const match = line.match(/^\.It Ev (?<envName>[^ ]*)/);
58+
if (match) {
59+
envVarNames.add(match.groups.envName);
60+
}
61+
}
62+
63+
return envVarNames;
64+
}
65+
66+
async function collectCliMdEnvVarNames() {
67+
const cliMdPath = join(rootDir, 'doc', 'api', 'cli.md');
68+
const fileStream = createReadStream(cliMdPath);
69+
70+
let insideEnvVariablesSection = false;
71+
72+
const rl = createInterface({
73+
input: fileStream,
74+
});
75+
76+
const envVariableRE = /^### `(?<varName>[^`]*?)(=[^`]+)?`$/;
77+
78+
const envVarNames = new Set();
79+
80+
for await (const line of rl) {
81+
if (line.startsWith('## ')) {
82+
if (insideEnvVariablesSection) {
83+
// We were in the environment variables section and we're now exiting it,
84+
// so there is no need to keep checking the remaining lines,
85+
// we might as well close the stream and return
86+
fileStream.close();
87+
return envVarNames;
88+
}
89+
90+
// We've just entered the options section
91+
insideEnvVariablesSection = line === '## Environment variables';
92+
continue;
93+
}
94+
95+
if (insideEnvVariablesSection) {
96+
const match = line.match(envVariableRE);
97+
if (match) {
98+
const { varName } = match.groups;
99+
envVarNames.add(varName);
100+
}
101+
}
102+
}
103+
104+
return envVarNames;
105+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import '../common/index.mjs';
2+
import assert from 'assert';
3+
import { createReadStream, readFileSync } from 'node:fs';
4+
import { createInterface } from 'node:readline';
5+
import { resolve, join } from 'node:path';
6+
7+
// This test checks that all the CLI flags defined in the public CLI documentation (doc/api/cli.md)
8+
// are also documented in the manpage file (doc/node.1)
9+
// Note: the opposite (that all variables in doc/node.1 are documented in the CLI documentation)
10+
// is covered in the test-cli-node-options-docs.js file
11+
12+
const rootDir = resolve(import.meta.dirname, '..', '..');
13+
14+
const cliMdPath = join(rootDir, 'doc', 'api', 'cli.md');
15+
const cliMdContentsStream = createReadStream(cliMdPath);
16+
17+
const manPagePath = join(rootDir, 'doc', 'node.1');
18+
const manPageContents = readFileSync(manPagePath, { encoding: 'utf8' });
19+
20+
// TODO(dario-piotrowicz): add the missing flags to the node.1 and remove this set
21+
const knownFlagsMissingFromManPage = new Set([
22+
'build-snapshot',
23+
'build-snapshot-config',
24+
'disable-sigusr1',
25+
'disable-warning',
26+
'dns-result-order',
27+
'enable-network-family-autoselection',
28+
'env-file-if-exists',
29+
'env-file',
30+
'experimental-network-inspection',
31+
'experimental-print-required-tla',
32+
'experimental-require-module',
33+
'experimental-sea-config',
34+
'experimental-worker-inspection',
35+
'expose-gc',
36+
'force-node-api-uncaught-exceptions-policy',
37+
'import',
38+
'network-family-autoselection-attempt-timeout',
39+
'no-async-context-frame',
40+
'no-experimental-detect-module',
41+
'no-experimental-global-navigator',
42+
'no-experimental-require-module',
43+
'no-network-family-autoselection',
44+
'openssl-legacy-provider',
45+
'openssl-shared-config',
46+
'report-dir',
47+
'report-directory',
48+
'report-exclude-env',
49+
'report-exclude-network',
50+
'run',
51+
'snapshot-blob',
52+
'trace-env',
53+
'trace-env-js-stack',
54+
'trace-env-native-stack',
55+
'trace-require-module',
56+
'use-system-ca',
57+
'watch-preserve-output',
58+
]);
59+
60+
const optionsEncountered = { dash: 0, dashDash: 0, named: 0 };
61+
let insideOptionsSection = false;
62+
63+
const rl = createInterface({
64+
input: cliMdContentsStream,
65+
});
66+
67+
const isOptionLineRegex = /^###( `[^`]*`(,)?)*$/;
68+
69+
for await (const line of rl) {
70+
if (line.startsWith('## ')) {
71+
if (insideOptionsSection) {
72+
// We were in the options section and we're now exiting it,
73+
// so there is no need to keep checking the remaining lines,
74+
// we might as well close the stream and exit the loop
75+
cliMdContentsStream.close();
76+
break;
77+
}
78+
79+
// We've just entered the options section
80+
insideOptionsSection = line === '## Options';
81+
continue;
82+
}
83+
84+
if (insideOptionsSection && isOptionLineRegex.test(line)) {
85+
if (line === '### `-`') {
86+
if (!manPageContents.includes('\n.It Sy -\n')) {
87+
throw new Error(`The \`-\` flag is missing in the \`doc/node.1\` file`);
88+
}
89+
optionsEncountered.dash++;
90+
continue;
91+
}
92+
93+
if (line === '### `--`') {
94+
if (!manPageContents.includes('\n.It Fl -\n')) {
95+
throw new Error(`The \`--\` flag is missing in the \`doc/node.1\` file`);
96+
}
97+
optionsEncountered.dashDash++;
98+
continue;
99+
}
100+
101+
const flagNames = extractFlagNames(line);
102+
103+
optionsEncountered.named += flagNames.length;
104+
105+
const manLine = `.It ${flagNames
106+
.map((flag) => `Fl ${flag.length > 1 ? '-' : ''}${flag}`)
107+
.join(' , ')}`;
108+
109+
if (
110+
// Note: we don't check the full line (note the `\n` only at the beginning) because
111+
// options can have arguments and we do want to ignore those
112+
!manPageContents.includes(`\n${manLine}`) &&
113+
!flagNames.every((flag) => knownFlagsMissingFromManPage.has(flag))) {
114+
assert.fail(
115+
`The following flag${
116+
flagNames.length === 1 ? '' : 's'
117+
} (present in \`doc/api/cli.md\`) ${flagNames.length === 1 ? 'is' : 'are'} missing in the \`doc/node.1\` file: ${
118+
flagNames.map((flag) => `"${flag}"`).join(', ')
119+
}`
120+
);
121+
}
122+
}
123+
}
124+
125+
assert.strictEqual(optionsEncountered.dash, 1);
126+
127+
assert.strictEqual(optionsEncountered.dashDash, 1);
128+
129+
assert(optionsEncountered.named > 0,
130+
'Unexpectedly not even a single cli flag/option was detected when scanning the `doc/cli.md` file'
131+
);
132+
133+
/**
134+
* Function that given a string containing backtick enclosed cli flags
135+
* separated by `, ` returns the name of flags present in the string
136+
* e.g. `extractFlagNames('`-x`, `--print "script"`')` === `['x', 'print']`
137+
* @param {string} str target string
138+
* @returns {string[]} the name of the detected flags
139+
*/
140+
function extractFlagNames(str) {
141+
const match = str.match(/`[^`]*?`/g);
142+
if (!match) {
143+
return [];
144+
}
145+
return match.map((flag) => {
146+
// Remove the backticks from the flag
147+
flag = flag.slice(1, -1);
148+
149+
// Remove the dash or dashes
150+
flag = flag.replace(/^--?/, '');
151+
152+
// If the flag contains parameters make sure to remove those
153+
const nameDelimiters = ['=', ' ', '['];
154+
const nameCutOffIdx = Math.min(...nameDelimiters.map((d) => {
155+
const idx = flag.indexOf(d);
156+
if (idx > 0) {
157+
return idx;
158+
}
159+
return flag.length;
160+
}));
161+
flag = flag.slice(0, nameCutOffIdx);
162+
163+
return flag;
164+
});
165+
}

0 commit comments

Comments
 (0)