|
| 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 | + |
| 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, 'out', 'doc', 'node.1'); |
| 18 | +const manPageContents = readFileSync(manPagePath, { encoding: 'utf8' }); |
| 19 | + |
| 20 | +let insideOptionsSection = false; |
| 21 | + |
| 22 | +const rl = createInterface({ |
| 23 | + input: cliMdContentsStream, |
| 24 | +}); |
| 25 | + |
| 26 | +const isOptionLineRegex = /^###(?: `[^`]*`,?)*$/; |
| 27 | + |
| 28 | +for await (const line of rl) { |
| 29 | + if (line.startsWith('## ')) { |
| 30 | + if (insideOptionsSection) { |
| 31 | + // We were in the options section and we're now exiting it, |
| 32 | + // so there is no need to keep checking the remaining lines, |
| 33 | + // we might as well close the stream and exit the loop |
| 34 | + cliMdContentsStream.close(); |
| 35 | + break; |
| 36 | + } |
| 37 | + |
| 38 | + // We've just entered the options section |
| 39 | + insideOptionsSection = line === '## Options'; |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + if (insideOptionsSection && isOptionLineRegex.test(line)) { |
| 44 | + const flagNames = extractFlagNames(line); |
| 45 | + const flagMatcher = new RegExp(`^\\.It ${flagNames.map((f) => `Fl ${f}.*`).join(', ')}$`, 'm'); |
| 46 | + |
| 47 | + if (!manPageContents.match(flagMatcher)) { |
| 48 | + assert.fail( |
| 49 | + `The following flag${ |
| 50 | + flagNames.length === 1 ? '' : 's' |
| 51 | + } (present in \`doc/api/cli.md\`) ${flagNames.length === 1 ? 'is' : 'are'} missing in the \`doc/node.1\` file: ${ |
| 52 | + flagNames.map((flag) => `"-${flag}"`).join(', ') |
| 53 | + }` |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Function that given a string containing backtick enclosed cli flags |
| 61 | + * separated by `, ` returns the name of flags present in the string |
| 62 | + * e.g. `extractFlagNames('`-x`, `--print "script"`')` === `['x', 'print']` |
| 63 | + * @param {string} str target string |
| 64 | + * @returns {string[]} the name of the detected flags |
| 65 | + */ |
| 66 | +function extractFlagNames(str) { |
| 67 | + const match = str.match(/`[^`]*?`/g); |
| 68 | + if (!match) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + return match.map((flag) => { |
| 72 | + // Remove the backticks, and leading dash from the flag |
| 73 | + flag = flag.slice(2, -1); |
| 74 | + |
| 75 | + // If the flag contains parameters make sure to remove those |
| 76 | + const nameDelimiters = ['=', ' ', '[']; |
| 77 | + const nameCutOffIdx = Math.min(...nameDelimiters.map((d) => { |
| 78 | + const idx = flag.indexOf(d); |
| 79 | + if (idx > 0) { |
| 80 | + return idx; |
| 81 | + } |
| 82 | + return flag.length; |
| 83 | + })); |
| 84 | + flag = flag.slice(0, nameCutOffIdx); |
| 85 | + |
| 86 | + return flag; |
| 87 | + }); |
| 88 | +} |
0 commit comments