From a6baf63d902cf6ca42382186f54f99143d942181 Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Fri, 10 Nov 2023 10:13:59 -0700 Subject: [PATCH] src: add --disable-warnings option --- lib/internal/process/warning.js | 10 +++ src/node_options.cc | 3 + src/node_options.h | 1 + test/fixtures/disable-warnings.js | 11 ++++ .../test-process-disable-warnings.js | 65 +++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 test/fixtures/disable-warnings.js create mode 100644 test/sequential/test-process-disable-warnings.js diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index 4aa86b9df58a87..0eea9799fe277a 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -2,12 +2,17 @@ const { ArrayIsArray, + ArrayPrototypeIncludes, Error, ErrorPrototypeToString, ErrorCaptureStackTrace, String, } = primordials; +const { + getOptionValue, +} = require('internal/options'); + const assert = require('internal/assert'); const { codes: { @@ -91,6 +96,11 @@ function doEmitWarning(warning) { function onWarning(warning) { if (!(warning instanceof Error)) return; + + const disableWarnings = getOptionValue('--disable-warnings'); + if ((warning.code && ArrayPrototypeIncludes(disableWarnings, warning.code)) || + (warning.name && ArrayPrototypeIncludes(disableWarnings, warning.name))) return; + const isDeprecation = warning.name === 'DeprecationWarning'; if (isDeprecation && process.noDeprecation) return; const trace = process.traceProcessWarnings || diff --git a/src/node_options.cc b/src/node_options.cc index 417d4679e84ec3..2215128da57365 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -527,6 +527,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { &EnvironmentOptions::warnings, kAllowedInEnvvar, true); + AddOption("--disable-warnings", + "silence specific process warnings", + &EnvironmentOptions::disable_warnings); AddOption("--force-context-aware", "disable loading non-context-aware addons", &EnvironmentOptions::force_context_aware, diff --git a/src/node_options.h b/src/node_options.h index 04c8a9e438dcfb..af19dd612387ae 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -139,6 +139,7 @@ class EnvironmentOptions : public Options { bool allow_native_addons = true; bool global_search_paths = true; bool warnings = true; + std::vector disable_warnings; bool force_context_aware = false; bool pending_deprecation = false; bool preserve_symlinks = false; diff --git a/test/fixtures/disable-warnings.js b/test/fixtures/disable-warnings.js new file mode 100644 index 00000000000000..e9c61c46934d1a --- /dev/null +++ b/test/fixtures/disable-warnings.js @@ -0,0 +1,11 @@ +'use strict'; + +const sys = require('sys'); +const assert = require('assert'); +const vm = require('vm'); + +try { + assert.fail('a', 'b'); +} catch (e) {} + +vm.measureMemory(); \ No newline at end of file diff --git a/test/sequential/test-process-disable-warnings.js b/test/sequential/test-process-disable-warnings.js new file mode 100644 index 00000000000000..d7e7a64b816fae --- /dev/null +++ b/test/sequential/test-process-disable-warnings.js @@ -0,0 +1,65 @@ +'use strict'; + +require('../common'); +const fixtures = require('../common/fixtures'); +const assert = require('assert'); +const execFile = require('child_process').execFile; +const warnmod = require.resolve(fixtures.path('disable-warnings.js')); +const node = process.execPath; + +const dep0025Message = /\(node:\d+\) \[DEP0025\] DeprecationWarning: sys is deprecated\. Use util instead\./; +const dep0094Message = /\(node:\d+\) \[DEP0094\] DeprecationWarning: assert\.fail\(\) with more than one argument is deprecated\. Please use assert\.strictEqual\(\) instead or only pass a message\./; +const experimentalWarningMessage = /\(node:\d+\) ExperimentalWarning: vm\.measureMemory is an experimental feature and might change at any time/; + +// Show All Warnings +execFile(node, [warnmod], function(er, stdout, stderr) { + assert.strictEqual(er, null); + assert.strictEqual(stdout, ''); + assert.match(stderr, dep0025Message); + assert.match(stderr, dep0094Message); + assert.match(stderr, experimentalWarningMessage); +}); + +// Hide All Warnings +execFile(node, ['--no-warnings', warnmod], function(er, stdout, stderr) { + assert.strictEqual(er, null); + assert.strictEqual(stdout, ''); + assert.doesNotMatch(stderr, dep0025Message); + assert.doesNotMatch(stderr, dep0094Message); + assert.doesNotMatch(stderr, experimentalWarningMessage); +}); + +// Hide Only DEP0025 Warnings +execFile(node, ['--disable-warnings=DEP0025', warnmod], function(er, stdout, stderr) { + assert.strictEqual(er, null); + assert.strictEqual(stdout, ''); + assert.doesNotMatch(stderr, dep0025Message); + assert.match(stderr, dep0094Message); + assert.match(stderr, experimentalWarningMessage); +}); + +// Hide DEP0025 and DEP0094 Warnings +execFile(node, ['--disable-warnings=DEP0025', '--disable-warnings=DEP0094', warnmod], function(er, stdout, stderr) { + assert.strictEqual(er, null); + assert.strictEqual(stdout, ''); + assert.doesNotMatch(stderr, dep0025Message); + assert.doesNotMatch(stderr, dep0094Message); + assert.match(stderr, experimentalWarningMessage); +}); + +// Hide DeprecationWarnings +execFile(node, ['--disable-warnings=DeprecationWarning', warnmod], function(er, stdout, stderr) { + assert.strictEqual(er, null); + assert.strictEqual(stdout, ''); + assert.doesNotMatch(stderr, dep0025Message); + assert.doesNotMatch(stderr, dep0094Message); + assert.match(stderr, experimentalWarningMessage); +}); + +execFile(node, ['--disable-warnings=ExperimentalWarning', warnmod], function(er, stdout, stderr) { + assert.strictEqual(er, null); + assert.strictEqual(stdout, ''); + assert.match(stderr, dep0025Message); + assert.match(stderr, dep0094Message); + assert.doesNotMatch(stderr, experimentalWarningMessage); +});