Skip to content
Permalink
Browse files

test: allow leaked global check to be skipped

This simplifies the process of running tests on different
versions of Node, which might have a different set of
global variables.

PR-URL: #27239
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
cjihrig authored and danbev committed Apr 15, 2019
1 parent 7581910 commit 57ab3b56fcba725e1af4ed7f8a05bbdbd0b8a4d9
Showing with 31 additions and 18 deletions.
  1. +20 −18 test/common/index.js
  2. +11 −0 test/parallel/test-common.js
@@ -279,34 +279,36 @@ if (global.gc) {
knownGlobals.push(global.gc);
}

if (process.env.NODE_TEST_KNOWN_GLOBALS) {
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
allowGlobals(...knownFromEnv);
}

function allowGlobals(...whitelist) {
knownGlobals = knownGlobals.concat(whitelist);
}

function leakedGlobals() {
const leaked = [];
if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
if (process.env.NODE_TEST_KNOWN_GLOBALS) {
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
allowGlobals(...knownFromEnv);
}

for (const val in global) {
if (!knownGlobals.includes(global[val])) {
leaked.push(val);
function leakedGlobals() {
const leaked = [];

for (const val in global) {
if (!knownGlobals.includes(global[val])) {
leaked.push(val);
}
}

return leaked;
}

return leaked;
process.on('exit', function() {
const leaked = leakedGlobals();
if (leaked.length > 0) {
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
}
});
}

process.on('exit', function() {
const leaked = leakedGlobals();
if (leaked.length > 0) {
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
}
});

const mustCallChecks = [];

function runCallChecks(exitCode) {
@@ -35,6 +35,17 @@ const { execFile } = require('child_process');
}));
}

// Test for disabling leaked global detection
{
const p = fixtures.path('leakedGlobal.js');
execFile(process.execPath, [p], {
env: { ...process.env, NODE_TEST_KNOWN_GLOBALS: 0 }
}, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err, null);
assert.strictEqual(stderr.trim(), '');
}));
}

// common.mustCall() tests
assert.throws(function() {
common.mustCall(function() {}, 'foo');

0 comments on commit 57ab3b5

Please sign in to comment.
You can’t perform that action at this time.