|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +if (process.config.variables.node_without_node_options) |
| 4 | + common.skip('missing NODE_OPTIONS support'); |
| 5 | + |
| 6 | +// Test options specified by env variable. |
| 7 | + |
| 8 | +const assert = require('assert'); |
| 9 | +const fs = require('fs'); |
| 10 | +const path = require('path'); |
| 11 | + |
| 12 | +const rootDir = path.resolve(__dirname, '..', '..'); |
| 13 | +const cliMd = path.join(rootDir, 'doc', 'api', 'cli.md'); |
| 14 | +const cliText = fs.readFileSync(cliMd, { encoding: 'utf8' }); |
| 15 | + |
| 16 | +const internalApiMd = path.join(rootDir, 'doc', 'contributing', 'internal-api.md'); |
| 17 | +const internalApiText = fs.readFileSync(internalApiMd, { encoding: 'utf8' }); |
| 18 | + |
| 19 | +const nodeOptionsCC = fs.readFileSync(path.resolve(rootDir, 'src', 'node_options.cc'), 'utf8'); |
| 20 | +const addOptionRE = /AddOption[\s\n\r]*\([\s\n\r]*"([^"]+)"(.*?)\);/gs; |
| 21 | + |
| 22 | +const nodeOptionsText = cliText.match(/<!-- node-options-node start -->(.*)<!-- node-options-others end -->/s)[1]; |
| 23 | +const v8OptionsText = cliText.match(/<!-- v8-options start -->(.*)<!-- v8-options end -->/s)[1]; |
| 24 | + |
| 25 | +// Documented in /doc/api/deprecations.md |
| 26 | +const deprecated = [ |
| 27 | + '--debug', |
| 28 | + '--debug-brk', |
| 29 | +]; |
| 30 | + |
| 31 | +for (const [, envVar, config] of nodeOptionsCC.matchAll(addOptionRE)) { |
| 32 | + let hasTrueAsDefaultValue = false; |
| 33 | + let isInNodeOption = false; |
| 34 | + let isV8Option = false; |
| 35 | + let isNoOp = false; |
| 36 | + |
| 37 | + if (config.includes('NoOp{}')) { |
| 38 | + isNoOp = true; |
| 39 | + } |
| 40 | + |
| 41 | + if (config.includes('kAllowedInEnvvar')) { |
| 42 | + isInNodeOption = true; |
| 43 | + } |
| 44 | + if (config.includes('kDisallowedInEnvvar')) { |
| 45 | + isInNodeOption = false; |
| 46 | + } |
| 47 | + |
| 48 | + if (config.includes('V8Option{}')) { |
| 49 | + isV8Option = true; |
| 50 | + } |
| 51 | + |
| 52 | + if (/^\s*true\s*$/.test(config.split(',').pop())) { |
| 53 | + hasTrueAsDefaultValue = true; |
| 54 | + } |
| 55 | + |
| 56 | + if ( |
| 57 | + envVar.startsWith('[') || |
| 58 | + deprecated.includes(envVar) || |
| 59 | + isNoOp |
| 60 | + ) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + // Internal API options are documented in /doc/contributing/internal-api.md |
| 65 | + if (new RegExp(`####.*\`${envVar}[\[=\\s\\b\`]`).test(internalApiText) === true) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + // CLI options |
| 70 | + if (!isV8Option && !hasTrueAsDefaultValue && new RegExp(`###.*\`${envVar}[\[=\\s\\b\`]`).test(cliText) === false) { |
| 71 | + assert(false, `Should have option ${envVar} documented`); |
| 72 | + } |
| 73 | + |
| 74 | + if (!hasTrueAsDefaultValue && new RegExp(`###.*\`--no-${envVar.slice(2)}[\[=\\s\\b\`]`).test(cliText) === true) { |
| 75 | + assert(false, `Should not have option --no-${envVar.slice(2)} documented`); |
| 76 | + } |
| 77 | + |
| 78 | + if (!isV8Option && hasTrueAsDefaultValue && new RegExp(`###.*\`--no-${envVar.slice(2)}[\[=\\s\\b\`]`).test(cliText) === false) { |
| 79 | + assert(false, `Should have option --no-${envVar.slice(2)} documented`); |
| 80 | + } |
| 81 | + |
| 82 | + // NODE_OPTIONS |
| 83 | + if (isInNodeOption && !hasTrueAsDefaultValue && new RegExp(`\`${envVar}\``).test(nodeOptionsText) === false) { |
| 84 | + assert(false, `Should have option ${envVar} in NODE_OPTIONS documented`); |
| 85 | + } |
| 86 | + |
| 87 | + if (isInNodeOption && hasTrueAsDefaultValue && new RegExp(`\`--no-${envVar.slice(2)}`).test(cliText) === false) { |
| 88 | + assert(false, `Should have option --no-${envVar.slice(2)} in NODE_OPTIONS documented`); |
| 89 | + } |
| 90 | + |
| 91 | + if (!hasTrueAsDefaultValue && new RegExp(`\`--no-${envVar.slice(2)}`).test(cliText) === true) { |
| 92 | + assert(false, `Should not have option --no-${envVar.slice(2)} in NODE_OPTIONS documented`); |
| 93 | + } |
| 94 | + |
| 95 | + // V8 options |
| 96 | + if (isV8Option && new RegExp(`###.*\`${envVar}[\[=\\s\\b\`]`).test(v8OptionsText) === false) { |
| 97 | + assert(false, `Should have option ${envVar} in V8 options documented`); |
| 98 | + } |
| 99 | +} |
0 commit comments