|
| 1 | +import'../common/index.mjs'; |
| 2 | +importassertfrom'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 | +constrootDir=resolve(import.meta.dirname,'..','..'); |
| 14 | + |
| 15 | +constcliMdPath=join(rootDir,'doc','api','cli.md'); |
| 16 | +constcliMdContentsStream=createReadStream(cliMdPath); |
| 17 | + |
| 18 | +constmanPagePath=join(rootDir,'doc','node.1'); |
| 19 | +constmanPageContents=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 | +constknownFlagsMissingFromManPage=newSet([ |
| 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-async-context-frame', |
| 33 | +'experimental-network-inspection', |
| 34 | +'experimental-print-required-tla', |
| 35 | +'experimental-require-module', |
| 36 | +'experimental-sea-config', |
| 37 | +'experimental-worker-inspection', |
| 38 | +'expose-gc', |
| 39 | +'force-node-api-uncaught-exceptions-policy', |
| 40 | +'import', |
| 41 | +'network-family-autoselection-attempt-timeout', |
| 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 | +constoptionsEncountered={dash: 0,dashDash: 0,named: 0}; |
| 63 | +letinsideOptionsSection=false; |
| 64 | + |
| 65 | +constrl=createInterface({ |
| 66 | +input: cliMdContentsStream, |
| 67 | +}); |
| 68 | + |
| 69 | +constisOptionLineRegex=/^###(?:`[^`]*`,?)*$/; |
| 70 | + |
| 71 | +forawait(constlineofrl){ |
| 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 | +thrownewError(`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 | +thrownewError(`The \`--\` flag is missing in the \`doc/node.1\` file`); |
| 98 | +} |
| 99 | +optionsEncountered.dashDash++; |
| 100 | +continue; |
| 101 | +} |
| 102 | + |
| 103 | +constflagNames=extractFlagNames(line); |
| 104 | + |
| 105 | +optionsEncountered.named+=flagNames.length; |
| 106 | + |
| 107 | +constmanLine=`.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 | +functionextractFlagNames(str){ |
| 143 | +constmatch=str.match(/`[^`]*?`/g); |
| 144 | +if(!match){ |
| 145 | +return[]; |
| 146 | +} |
| 147 | +returnmatch.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 | +constnameDelimiters=['=',' ','[']; |
| 156 | +constnameCutOffIdx=Math.min(...nameDelimiters.map((d)=>{ |
| 157 | +constidx=flag.indexOf(d); |
| 158 | +if(idx>0){ |
| 159 | +returnidx; |
| 160 | +} |
| 161 | +returnflag.length; |
| 162 | +})); |
| 163 | +flag=flag.slice(0,nameCutOffIdx); |
| 164 | + |
| 165 | +returnflag; |
| 166 | +}); |
| 167 | +} |
0 commit comments