Skip to content

Commit

Permalink
src: add --disable-warnings option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Arrowood committed Nov 10, 2023
1 parent 609cd7f commit a6baf63
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const {
ArrayIsArray,
ArrayPrototypeIncludes,
Error,
ErrorPrototypeToString,
ErrorCaptureStackTrace,
String,
} = primordials;

const {
getOptionValue,
} = require('internal/options');

const assert = require('internal/assert');
const {
codes: {
Expand Down Expand Up @@ -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 ||
Expand Down
3 changes: 3 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class EnvironmentOptions : public Options {
bool allow_native_addons = true;
bool global_search_paths = true;
bool warnings = true;
std::vector<std::string> disable_warnings;
bool force_context_aware = false;
bool pending_deprecation = false;
bool preserve_symlinks = false;
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/disable-warnings.js
Original file line number Diff line number Diff line change
@@ -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();
65 changes: 65 additions & 0 deletions test/sequential/test-process-disable-warnings.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit a6baf63

Please sign in to comment.