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