From a0a4a982927189ac214827350109318a5b84e887 Mon Sep 17 00:00:00 2001 From: Jerome Covington Date: Sat, 14 Jul 2018 06:40:35 -0400 Subject: [PATCH] test: extract globals & use in test-abort-backtrace First steps towards not needing to require test/common/index.js everywhere. Refs: https://github.com/nodejs/node/issues/20128#issuecomment-389933546 --- test/abort/test-abort-backtrace.js | 3 +- test/common/globals.js | 71 ++++++++++++++++++++++++++ test/common/index.js | 71 ++------------------------ test/sequential/test-module-loading.js | 1 + 4 files changed, 77 insertions(+), 69 deletions(-) create mode 100644 test/common/globals.js diff --git a/test/abort/test-abort-backtrace.js b/test/abort/test-abort-backtrace.js index 7f87ef0e7f4559..7f8aa0ace17748 100644 --- a/test/abort/test-abort-backtrace.js +++ b/test/abort/test-abort-backtrace.js @@ -1,5 +1,6 @@ +/* eslint-disable node-core/required-modules */ 'use strict'; -const common = require('../common'); +const common = require('../common/globals'); const assert = require('assert'); const cp = require('child_process'); diff --git a/test/common/globals.js b/test/common/globals.js new file mode 100644 index 00000000000000..7cdf54b78154f8 --- /dev/null +++ b/test/common/globals.js @@ -0,0 +1,71 @@ +/* eslint-disable node-core/required-modules */ +'use strict'; +const assert = require('assert'); + +let knownGlobals = [ + Buffer, + clearImmediate, + clearInterval, + clearTimeout, + global, + process, + setImmediate, + setInterval, + setTimeout +]; + +if (global.gc) { + knownGlobals.push(global.gc); +} + +if (global.DTRACE_HTTP_SERVER_RESPONSE) { + knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); + knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); + knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); + knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); + knownGlobals.push(DTRACE_NET_STREAM_END); + knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); +} + +if (global.COUNTER_NET_SERVER_CONNECTION) { + knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); + knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); + knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); + knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); + knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); + knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); +} + +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); +} +exports.allowGlobals = allowGlobals; + +function leakedGlobals() { + const leaked = []; + + for (const val in global) { + if (!knownGlobals.includes(global[val])) { + leaked.push(val); + } + } + + if (global.__coverage__) { + return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname)); + } else { + return leaked; + } +} +exports.leakedGlobals = leakedGlobals; + +process.on('exit', function() { + const leaked = leakedGlobals(); + if (leaked.length > 0) { + assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`); + } +}); diff --git a/test/common/index.js b/test/common/index.js index 904e5a8ef379fa..1f30b929cd2375 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -32,6 +32,9 @@ const util = require('util'); const { hasTracing } = process.binding('config'); const { fixturesDir } = require('./fixtures'); const tmpdir = require('./tmpdir'); +const { allowGlobals, leakedGlobals } = require('./globals'); +exports.allowGlobals = allowGlobals; +exports.leakedGlobals = leakedGlobals; const noop = () => {}; @@ -329,74 +332,6 @@ exports.platformTimeout = function(ms) { return ms; // ARMv8+ }; -let knownGlobals = [ - Buffer, - clearImmediate, - clearInterval, - clearTimeout, - global, - process, - setImmediate, - setInterval, - setTimeout -]; - -if (global.gc) { - knownGlobals.push(global.gc); -} - -if (global.DTRACE_HTTP_SERVER_RESPONSE) { - knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); - knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); - knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); - knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); - knownGlobals.push(DTRACE_NET_STREAM_END); - knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); -} - -if (global.COUNTER_NET_SERVER_CONNECTION) { - knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); - knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); - knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); - knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); - knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); - knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); -} - -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); -} -exports.allowGlobals = allowGlobals; - -function leakedGlobals() { - const leaked = []; - - for (const val in global) { - if (!knownGlobals.includes(global[val])) { - leaked.push(val); - } - } - - if (global.__coverage__) { - return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname)); - } else { - return leaked; - } -} -exports.leakedGlobals = leakedGlobals; - -process.on('exit', function() { - const leaked = leakedGlobals(); - if (leaked.length > 0) { - assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`); - } -}); - const mustCallChecks = []; function runCallChecks(exitCode) { diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index abea73c4aa616c..591e7df62def32 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -252,6 +252,7 @@ try { assert.deepStrictEqual(children, { 'common/index.js': { 'common/fixtures.js': {}, + 'common/globals.js': {}, 'common/tmpdir.js': {} }, 'fixtures/not-main-module.js': {},