Skip to content

Commit 57ab3b5

Browse files
cjihrigdanbev
authored andcommitted
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7581910 commit 57ab3b5

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

test/common/index.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -279,34 +279,36 @@ if (global.gc) {
279279
knownGlobals.push(global.gc);
280280
}
281281

282-
if (process.env.NODE_TEST_KNOWN_GLOBALS) {
283-
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
284-
allowGlobals(...knownFromEnv);
285-
}
286-
287282
function allowGlobals(...whitelist) {
288283
knownGlobals = knownGlobals.concat(whitelist);
289284
}
290285

291-
function leakedGlobals() {
292-
const leaked = [];
286+
if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
287+
if (process.env.NODE_TEST_KNOWN_GLOBALS) {
288+
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
289+
allowGlobals(...knownFromEnv);
290+
}
293291

294-
for (const val in global) {
295-
if (!knownGlobals.includes(global[val])) {
296-
leaked.push(val);
292+
function leakedGlobals() {
293+
const leaked = [];
294+
295+
for (const val in global) {
296+
if (!knownGlobals.includes(global[val])) {
297+
leaked.push(val);
298+
}
297299
}
300+
301+
return leaked;
298302
}
299303

300-
return leaked;
304+
process.on('exit', function() {
305+
const leaked = leakedGlobals();
306+
if (leaked.length > 0) {
307+
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
308+
}
309+
});
301310
}
302311

303-
process.on('exit', function() {
304-
const leaked = leakedGlobals();
305-
if (leaked.length > 0) {
306-
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
307-
}
308-
});
309-
310312
const mustCallChecks = [];
311313

312314
function runCallChecks(exitCode) {

test/parallel/test-common.js

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ const { execFile } = require('child_process');
3535
}));
3636
}
3737

38+
// Test for disabling leaked global detection
39+
{
40+
const p = fixtures.path('leakedGlobal.js');
41+
execFile(process.execPath, [p], {
42+
env: { ...process.env, NODE_TEST_KNOWN_GLOBALS: 0 }
43+
}, common.mustCall((err, stdout, stderr) => {
44+
assert.strictEqual(err, null);
45+
assert.strictEqual(stderr.trim(), '');
46+
}));
47+
}
48+
3849
// common.mustCall() tests
3950
assert.throws(function() {
4051
common.mustCall(function() {}, 'foo');

0 commit comments

Comments
 (0)