Skip to content

Commit a6baf63

Browse files
author
Ethan Arrowood
committed
src: add --disable-warnings option
1 parent 609cd7f commit a6baf63

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

lib/internal/process/warning.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
const {
44
ArrayIsArray,
5+
ArrayPrototypeIncludes,
56
Error,
67
ErrorPrototypeToString,
78
ErrorCaptureStackTrace,
89
String,
910
} = primordials;
1011

12+
const {
13+
getOptionValue,
14+
} = require('internal/options');
15+
1116
const assert = require('internal/assert');
1217
const {
1318
codes: {
@@ -91,6 +96,11 @@ function doEmitWarning(warning) {
9196

9297
function onWarning(warning) {
9398
if (!(warning instanceof Error)) return;
99+
100+
const disableWarnings = getOptionValue('--disable-warnings');
101+
if ((warning.code && ArrayPrototypeIncludes(disableWarnings, warning.code)) ||
102+
(warning.name && ArrayPrototypeIncludes(disableWarnings, warning.name))) return;
103+
94104
const isDeprecation = warning.name === 'DeprecationWarning';
95105
if (isDeprecation && process.noDeprecation) return;
96106
const trace = process.traceProcessWarnings ||

src/node_options.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
527527
&EnvironmentOptions::warnings,
528528
kAllowedInEnvvar,
529529
true);
530+
AddOption("--disable-warnings",
531+
"silence specific process warnings",
532+
&EnvironmentOptions::disable_warnings);
530533
AddOption("--force-context-aware",
531534
"disable loading non-context-aware addons",
532535
&EnvironmentOptions::force_context_aware,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class EnvironmentOptions : public Options {
139139
bool allow_native_addons = true;
140140
bool global_search_paths = true;
141141
bool warnings = true;
142+
std::vector<std::string> disable_warnings;
142143
bool force_context_aware = false;
143144
bool pending_deprecation = false;
144145
bool preserve_symlinks = false;

test/fixtures/disable-warnings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const sys = require('sys');
4+
const assert = require('assert');
5+
const vm = require('vm');
6+
7+
try {
8+
assert.fail('a', 'b');
9+
} catch (e) {}
10+
11+
vm.measureMemory();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const assert = require('assert');
6+
const execFile = require('child_process').execFile;
7+
const warnmod = require.resolve(fixtures.path('disable-warnings.js'));
8+
const node = process.execPath;
9+
10+
const dep0025Message = /\(node:\d+\) \[DEP0025\] DeprecationWarning: sys is deprecated\. Use util instead\./;
11+
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\./;
12+
const experimentalWarningMessage = /\(node:\d+\) ExperimentalWarning: vm\.measureMemory is an experimental feature and might change at any time/;
13+
14+
// Show All Warnings
15+
execFile(node, [warnmod], function(er, stdout, stderr) {
16+
assert.strictEqual(er, null);
17+
assert.strictEqual(stdout, '');
18+
assert.match(stderr, dep0025Message);
19+
assert.match(stderr, dep0094Message);
20+
assert.match(stderr, experimentalWarningMessage);
21+
});
22+
23+
// Hide All Warnings
24+
execFile(node, ['--no-warnings', warnmod], function(er, stdout, stderr) {
25+
assert.strictEqual(er, null);
26+
assert.strictEqual(stdout, '');
27+
assert.doesNotMatch(stderr, dep0025Message);
28+
assert.doesNotMatch(stderr, dep0094Message);
29+
assert.doesNotMatch(stderr, experimentalWarningMessage);
30+
});
31+
32+
// Hide Only DEP0025 Warnings
33+
execFile(node, ['--disable-warnings=DEP0025', warnmod], function(er, stdout, stderr) {
34+
assert.strictEqual(er, null);
35+
assert.strictEqual(stdout, '');
36+
assert.doesNotMatch(stderr, dep0025Message);
37+
assert.match(stderr, dep0094Message);
38+
assert.match(stderr, experimentalWarningMessage);
39+
});
40+
41+
// Hide DEP0025 and DEP0094 Warnings
42+
execFile(node, ['--disable-warnings=DEP0025', '--disable-warnings=DEP0094', warnmod], function(er, stdout, stderr) {
43+
assert.strictEqual(er, null);
44+
assert.strictEqual(stdout, '');
45+
assert.doesNotMatch(stderr, dep0025Message);
46+
assert.doesNotMatch(stderr, dep0094Message);
47+
assert.match(stderr, experimentalWarningMessage);
48+
});
49+
50+
// Hide DeprecationWarnings
51+
execFile(node, ['--disable-warnings=DeprecationWarning', warnmod], function(er, stdout, stderr) {
52+
assert.strictEqual(er, null);
53+
assert.strictEqual(stdout, '');
54+
assert.doesNotMatch(stderr, dep0025Message);
55+
assert.doesNotMatch(stderr, dep0094Message);
56+
assert.match(stderr, experimentalWarningMessage);
57+
});
58+
59+
execFile(node, ['--disable-warnings=ExperimentalWarning', warnmod], function(er, stdout, stderr) {
60+
assert.strictEqual(er, null);
61+
assert.strictEqual(stdout, '');
62+
assert.match(stderr, dep0025Message);
63+
assert.match(stderr, dep0094Message);
64+
assert.doesNotMatch(stderr, experimentalWarningMessage);
65+
});

0 commit comments

Comments
 (0)