From 02cc39f221980b6406dceab04c9ecafaee95a003 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 4 Dec 2010 13:40:39 -0800 Subject: [PATCH] Check for global leaks in all tests --- test/common.js | 32 ++++++++++++++++++++++++++ test/simple/test-global-leak.js | 27 ---------------------- test/simple/test-global.js | 2 ++ test/simple/test-repl.js | 2 ++ test/simple/test-script-new.js | 4 +++- test/simple/test-script-static-new.js | 3 ++- test/simple/test-script-static-this.js | 3 ++- test/simple/test-script-this.js | 3 ++- 8 files changed, 45 insertions(+), 31 deletions(-) delete mode 100644 test/simple/test-global-leak.js diff --git a/test/common.js b/test/common.js index c510eb338..3e88c7c4b 100644 --- a/test/common.js +++ b/test/common.js @@ -24,3 +24,35 @@ exports.indirectInstanceOf = function (obj, cls) { var objChain = protoCtrChain(obj); return objChain.slice(-clsChain.length) === clsChain; }; + + +// Turn this off if the test should not check for global leaks. +exports.globalCheck = true; + +process.on('exit', function () { + if (!exports.globalCheck) return; + var knownGlobals = [ setTimeout, + setInterval, + clearTimeout, + clearInterval, + console, + Buffer, + process, + global ]; + + for (var x in global) { + var found = false; + + for (var y in knownGlobals) { + if (global[x] === knownGlobals[y]) { + found = true; + break; + } + } + + if (!found) { + console.error("Unknown global: %s", x); + exports.assert.ok(false); + } + } +}); diff --git a/test/simple/test-global-leak.js b/test/simple/test-global-leak.js deleted file mode 100644 index a0226ae0c..000000000 --- a/test/simple/test-global-leak.js +++ /dev/null @@ -1,27 +0,0 @@ -var assert = require('assert'); - -var knownGlobals = [ setTimeout - , setInterval - , clearTimeout - , clearInterval - , console - , Buffer - , process - , global - ]; - -for (var x in global) { - var found = false; - - for (var y in knownGlobals) { - if (global[x] === knownGlobals[y]) { - found = true; - break; - } - } - - if (!found) { - console.error("Unknown global: %s", x); - assert.ok(false); - } -} diff --git a/test/simple/test-global.js b/test/simple/test-global.js index 61510034c..28f1dfc4e 100644 --- a/test/simple/test-global.js +++ b/test/simple/test-global.js @@ -1,6 +1,8 @@ var common = require('../common'); var assert = require('assert'); +common.globalCheck = false; + baseFoo = "foo"; global.baseBar = "bar"; diff --git a/test/simple/test-repl.js b/test/simple/test-repl.js index a3280d998..7d47ad279 100644 --- a/test/simple/test-repl.js +++ b/test/simple/test-repl.js @@ -1,6 +1,8 @@ var common = require('../common'); var assert = require('assert'); +common.globalCheck = false; + var net = require("net"), repl = require("repl"), message = "Read, Eval, Print Loop", diff --git a/test/simple/test-script-new.js b/test/simple/test-script-new.js index 73cdf14d7..29041df5b 100644 --- a/test/simple/test-script-new.js +++ b/test/simple/test-script-new.js @@ -1,7 +1,9 @@ var common = require('../common'); var assert = require('assert'); - var Script = require('vm').Script; + +common.globalCheck = false; + common.debug('run a string'); var script = new Script('"passed";'); common.debug('script created'); diff --git a/test/simple/test-script-static-new.js b/test/simple/test-script-static-new.js index ef135c925..426029dce 100644 --- a/test/simple/test-script-static-new.js +++ b/test/simple/test-script-static-new.js @@ -1,8 +1,9 @@ var common = require('../common'); var assert = require('assert'); - var Script = require('vm').Script; +common.globalCheck = false; + common.debug('run a string'); var result = Script.runInNewContext('"passed";'); assert.equal('passed', result); diff --git a/test/simple/test-script-static-this.js b/test/simple/test-script-static-this.js index dc031939e..302ea6527 100644 --- a/test/simple/test-script-static-this.js +++ b/test/simple/test-script-static-this.js @@ -1,8 +1,9 @@ var common = require('../common'); var assert = require('assert'); - var Script = require('vm').Script; +common.globalCheck = false; + common.debug('run a string'); var result = Script.runInThisContext('"passed";'); assert.equal('passed', result); diff --git a/test/simple/test-script-this.js b/test/simple/test-script-this.js index 576a5b714..677f68b38 100644 --- a/test/simple/test-script-this.js +++ b/test/simple/test-script-this.js @@ -1,8 +1,9 @@ var common = require('../common'); var assert = require('assert'); - var Script = require('vm').Script; +common.globalCheck = false; + common.debug('run a string'); var script = new Script('"passed";'); var result = script.runInThisContext(script);