node/test/parallel/test-cli-options-negation.js
Michaël Zasso 1bbe66f432
src: allow to negate boolean CLI flags
This change allows all boolean flags to be negated using the `--no-`
prefix.
Flags that are `true` by default (for example `--deprecation`) are
still documented as negations.
With this change, it becomes possible to easily flip the default
value of a boolean flag and to override the value of a flag passed
in the NODE_OPTIONS environment variable.
`process.allowedNodeEnvironmentFlags` contains both the negated and
non-negated versions of boolean flags.

Co-authored-by: Anna Henningsen <anna@addaleax.net>

PR-URL: https://github.com/nodejs/node/pull/39023
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2021-06-21 20:18:20 +02:00

40 lines
1.1 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
// --warnings is on by default.
assertHasWarning(spawnWithFlags([]));
// --warnings can be passed alone.
assertHasWarning(spawnWithFlags(['--warnings']));
// --no-warnings can be passed alone.
assertHasNoWarning(spawnWithFlags(['--no-warnings']));
// Last flag takes precedence.
assertHasWarning(spawnWithFlags(['--no-warnings', '--warnings']));
// Non-boolean flags cannot be negated.
assert(spawnWithFlags(['--no-max-http-header-size']).stderr.toString().includes(
'--no-max-http-header-size is an invalid negation because it is not ' +
'a boolean option',
));
// Inexistant flags cannot be negated.
assert(spawnWithFlags(['--no-i-dont-exist']).stderr.toString().includes(
'bad option: --no-i-dont-exist',
));
function spawnWithFlags(flags) {
return spawnSync(process.execPath, [...flags, '-e', 'new Buffer(0)']);
}
function assertHasWarning(proc) {
assert(proc.stderr.toString().includes('Buffer() is deprecated'));
}
function assertHasNoWarning(proc) {
assert(!proc.stderr.toString().includes('Buffer() is deprecated'));
}