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