Skip to content

Commit

Permalink
process: add NODE_NO_WARNINGS environment variable
Browse files Browse the repository at this point in the history
This commit adds support for a NODE_NO_WARNINGS environment
variable, which duplicates the functionality of the --no-warnings
command line flag.

Fixes: #10802
PR-URL: #10842
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Italo A. Casas <me@italoacasas.com>
  • Loading branch information
cjihrig authored and MylesBorins committed May 16, 2017
1 parent f5938ef commit 846270f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ added: v0.11.15
Data path for ICU (Intl object) data. Will extend linked-in data when compiled
with small-icu support.

### `NODE_NO_WARNINGS=1`
<!-- YAML
added: REPLACEME
-->

When set to `1`, process warnings are silenced.

### `NODE_REPL_HISTORY=file`
<!-- YAML
Expand Down
4 changes: 4 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ When set to 1 colors will not be used in the REPL.
Data path for ICU (Intl object) data. Will extend linked-in data when compiled
with small\-icu support.

.TP
.BR NODE_NO_WARNINGS =\fI1\fR
When set to \fI1\fR, process warnings are silenced.

.TP
.BR NODE_REPL_HISTORY =\fIfile\fR
Path to the file used to store the persistent REPL history. The default path
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const prefix = `(${process.release.name}:${process.pid}) `;
exports.setup = setupProcessWarnings;

function setupProcessWarnings() {
if (!process.noProcessWarnings) {
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
process.on('warning', (warning) => {
if (!(warning instanceof Error)) return;
const isDeprecation = warning.name === 'DeprecationWarning';
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-env-var-no-warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
process.emitWarning('foo');
} else {
function test(env) {
const cmd = `${process.execPath} ${__filename} child`;

cp.exec(cmd, { env }, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err, null);
assert.strictEqual(stdout, '');

if (env.NODE_NO_WARNINGS === '1')
assert.strictEqual(stderr, '');
else
assert(/Warning: foo$/.test(stderr.trim()));
}));
}

test({});
test(process.env);
test({ NODE_NO_WARNINGS: undefined });
test({ NODE_NO_WARNINGS: null });
test({ NODE_NO_WARNINGS: 'foo' });
test({ NODE_NO_WARNINGS: true });
test({ NODE_NO_WARNINGS: false });
test({ NODE_NO_WARNINGS: {} });
test({ NODE_NO_WARNINGS: [] });
test({ NODE_NO_WARNINGS: function() {} });
test({ NODE_NO_WARNINGS: 0 });
test({ NODE_NO_WARNINGS: -1 });
test({ NODE_NO_WARNINGS: '0' });
test({ NODE_NO_WARNINGS: '01' });
test({ NODE_NO_WARNINGS: '2' });
// Don't test the number 1 because it will come through as a string in the
// the child process environment.
test({ NODE_NO_WARNINGS: '1' });
}

0 comments on commit 846270f

Please sign in to comment.