Skip to content

Commit

Permalink
docs: test for cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Jan 31, 2024
1 parent 64c6d97 commit 43b4c91
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2602,11 +2602,15 @@ V8 options that are allowed are:

<!-- node-options-v8 end -->

<!-- node-options-others start -->

`--perf-basic-prof-only-functions`, `--perf-basic-prof`,
`--perf-prof-unwinding-info`, and `--perf-prof` are only available on Linux.

`--enable-etw-stack-walking` is only available on Windows.

<!-- node-options-others end -->

### `NODE_PATH=path[:…]`

<!-- YAML
Expand Down
62 changes: 62 additions & 0 deletions test/parallel/test-cli-node-options-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';
const common = require('../common');
if (process.config.variables.node_without_node_options)
common.skip('missing NODE_OPTIONS support');

// Test options specified by env variable.

const assert = require('assert');
const fs = require('fs');
const path = require('path')

const rootDir = path.resolve(__dirname, '..', '..');
const cliMd = path.join(rootDir, 'doc', 'api', 'cli.md');
const cliText = fs.readFileSync(cliMd, { encoding: 'utf8' });

const nodeOptionsCC = fs.readFileSync(path.resolve(rootDir, 'src', 'node_options.cc'), 'utf8');
const addOptionRE = /AddOption[\s\n\r]*\([\s\n\r]*"([^"]+)"(.*?)\);/gs;

const nodeOptionsText = cliText.match(/<!-- node-options-node start -->(.*)<!-- node-options-others end -->/s)[1];

for (const [, envVar, config ] of nodeOptionsCC.matchAll(addOptionRE)) {
let hasTrueAsDefaultValue = false
let isNodeOption = false;
if (envVar.startsWith('[')) {
continue;
}

if (config.includes('kAllowedInEnvvar')) {
isNodeOption = true;
}
if (config.includes('kDisallowedInEnvvar')) {
isNodeOption = false;
}

if (/^\s*true\s*$/.test(config.split(',').pop())) {
hasTrueAsDefaultValue = true
}

if (!hasTrueAsDefaultValue && new RegExp(`\#\#\#.*\`${envVar}[\[=\\s\\b\`]`).test(cliText) === false) {
console.log(`Should have option ${envVar} documented`)
}

if (!hasTrueAsDefaultValue && new RegExp(`\#\#\#.*\`--no-${envVar.slice(2)}[\[=\\s\\b\`]`).test(cliText) === true) {
console.log(`Should not have option --no-${envVar.slice(2)} documented`)
}

if (hasTrueAsDefaultValue && new RegExp(`\#\#\#.*\`--no-${envVar.slice(2)}[\[=\\s\\b\`]`).test(cliText) === false) {
console.log(`Should have option --no-${envVar.slice(2)} documented`)
}

if (!hasTrueAsDefaultValue && new RegExp(`\`${envVar}\``).test(nodeOptionsText) === false) {
console.log(`Should have option ${envVar} in NODE_OPTIONS documented`)
}

if (hasTrueAsDefaultValue && new RegExp(`\`--no-${envVar.slice(2)}`).test(cliText) === false) {
console.log(`Should have option --no-${envVar.slice(2)} in NODE_OPTIONS documented`)
}

if (!hasTrueAsDefaultValue && new RegExp(`\`--no-${envVar.slice(2)}`).test(cliText) === true) {
console.log(`Should not have option --no-${envVar.slice(2)} in NODE_OPTIONS documented`)
}
}

0 comments on commit 43b4c91

Please sign in to comment.