Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global leaks improvements #783

Merged
merged 3 commits into from
Sep 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,15 @@ Runner.prototype.checkGlobals = function(test){
var leaks;

// check length - 2 ('errno' and 'location' globals)
if (isNode && 1 == ok.length - globals.length) return
if (isNode && 1 == ok.length - globals.length) return;
else if (2 == ok.length - globals.length) return;


if(this.prevGlobalsLength == globals.length) return;
this.prevGlobalsLength = globals.length;

//console.time('filterLeaksTime');
leaks = filterLeaks(ok, globals);
//console.timeEnd('filterLeaksTime');
this._globals = this._globals.concat(leaks);

if (leaks.length > 1) {
Expand Down Expand Up @@ -523,10 +528,21 @@ Runner.prototype.run = function(fn){

function filterLeaks(ok, globals) {
return filter(globals, function(key){
// Opera and IE expose global variables for HTML element IDs (issue #243)

// in firefox
// if runner runs in an iframe, this iframe's window.getInterface method not init at first
// it is assigned in some seconds
if (global.navigator && /^getInterface/.test(key)) return false;

// an iframe could be approached by window[iframeIndex]
// in ie6,7,8 and opera, iframeIndex is enumerable, this could cause leak
if (global.navigator && /^\d+/.test(key)) return false;

if (/^mocha-/.test(key)) return false;

var matched = filter(ok, function(ok){
if (~ok.indexOf('*')) return 0 == key.indexOf(ok.split('*')[0]);
// Opera and IE expose global variables for HTML element IDs (issue #243)
if (/^mocha-/.test(key)) return true;
return key == ok;
});
return matched.length == 0 && (!global.navigator || 'onerror' !== key);
Expand Down
146 changes: 78 additions & 68 deletions mocha.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
;(function(){


// CommonJS require()

function require(p){
Expand Down Expand Up @@ -32,11 +31,11 @@ require.register = function (path, fn){
require.relative = function (parent) {
return function(p){
if ('.' != p.charAt(0)) return require(p);

var path = parent.split('/')
, segs = p.split('/');
path.pop();

for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
Expand All @@ -52,9 +51,9 @@ require.register("browser/debug.js", function(module, exports, require){

module.exports = function(type){
return function(){

}
};

}); // module: browser/debug.js

require.register("browser/diff.js", function(module, exports, require){
Expand Down Expand Up @@ -805,7 +804,6 @@ Hook.prototype.error = function(err){
this._error = err;
};


}); // module: hook.js

require.register("interfaces/bdd.js", function(module, exports, require){
Expand All @@ -819,7 +817,7 @@ var Suite = require('../suite')

/**
* BDD-style interface:
*
*
* describe('Array', function(){
* describe('#indexOf()', function(){
* it('should return -1 when not present', function(){
Expand All @@ -831,7 +829,7 @@ var Suite = require('../suite')
* });
* });
* });
*
*
*/

module.exports = function(suite){
Expand Down Expand Up @@ -876,7 +874,7 @@ module.exports = function(suite){
* and callback `fn` containing nested suites
* and/or tests.
*/

context.describe = context.context = function(title, fn){
var suite = Suite.create(suites[0], title);
suites.unshift(suite);
Expand Down Expand Up @@ -956,19 +954,19 @@ var Suite = require('../suite')

/**
* TDD-style interface:
*
*
* exports.Array = {
* '#indexOf()': {
* 'should return -1 when the value is not present': function(){
*
*
* },
*
* 'should return the correct index when the value is present': function(){
*
*
* }
* }
* };
*
*
*/

module.exports = function(suite){
Expand Down Expand Up @@ -1006,6 +1004,7 @@ module.exports = function(suite){
}
}
};

}); // module: interfaces/exports.js

require.register("interfaces/index.js", function(module, exports, require){
Expand All @@ -1028,27 +1027,27 @@ var Suite = require('../suite')

/**
* QUnit-style interface:
*
*
* suite('Array');
*
*
* test('#length', function(){
* var arr = [1,2,3];
* ok(arr.length == 3);
* });
*
*
* test('#indexOf()', function(){
* var arr = [1,2,3];
* ok(arr.indexOf(1) == 0);
* ok(arr.indexOf(2) == 1);
* ok(arr.indexOf(3) == 2);
* });
*
*
* suite('String');
*
*
* test('#length', function(){
* ok('foo'.length == 3);
* });
*
*
*/

module.exports = function(suite){
Expand Down Expand Up @@ -1091,7 +1090,7 @@ module.exports = function(suite){
/**
* Describe a "suite" with the given `title`.
*/

context.suite = function(title){
if (suites.length > 1) suites.shift();
var suite = Suite.create(suites[0], title);
Expand Down Expand Up @@ -1129,7 +1128,7 @@ var Suite = require('../suite')
* suiteSetup(function(){
*
* });
*
*
* test('should return -1 when not present', function(){
*
* });
Expand Down Expand Up @@ -1710,7 +1709,7 @@ exports.colors = {
/**
* Default symbol map.
*/

exports.symbols = {
ok: '✓',
err: '✖',
Expand Down Expand Up @@ -3080,7 +3079,7 @@ exports = module.exports = Min;

function Min(runner) {
Base.call(this, runner);

runner.on('start', function(){
// clear screen
process.stdout.write('\u001b[2J');
Expand All @@ -3100,10 +3099,10 @@ F.prototype = Base.prototype;
Min.prototype = new F;
Min.prototype.constructor = Min;


}); // module: reporters/min.js

require.register("reporters/nyan.js", function(module, exports, require){

/**
* Module dependencies.
*/
Expand Down Expand Up @@ -3249,44 +3248,39 @@ NyanCat.prototype.drawRainbow = function(){
NyanCat.prototype.drawNyanCat = function(status) {
var self = this;
var startWidth = this.scoreboardWidth + this.trajectories[0].length;

[0, 1, 2, 3].forEach(function(index) {
write('\u001b[' + startWidth + 'C');

switch (index) {
case 0:
write('_,------,');
write('\n');
break;
case 1:
var padding = self.tick ? ' ' : ' ';
write('_|' + padding + '/\\_/\\ ');
write('\n');
break;
case 2:
var padding = self.tick ? '_' : '__';
var tail = self.tick ? '~' : '^';
var face;
switch (status) {
case 'pass':
face = '( ^ .^)';
break;
case 'fail':
face = '( o .o)';
break;
default:
face = '( - .-)';
}
write(tail + '|' + padding + face + ' ');
write('\n');
break;
case 3:
var padding = self.tick ? ' ' : ' ';
write(padding + '"" "" ');
write('\n');
break;
}
});
var color = '\u001b[' + startWidth + 'C';
var padding = '';

write(color);
write('_,------,');
write('\n');

write(color);
padding = self.tick ? ' ' : ' ';
write('_|' + padding + '/\\_/\\ ');
write('\n');

write(color);
padding = self.tick ? '_' : '__';
var tail = self.tick ? '~' : '^';
var face;
switch (status) {
case 'pass':
face = '( ^ .^)';
break;
case 'fail':
face = '( o .o)';
break;
default:
face = '( - .-)';
}
write(tail + '|' + padding + face + ' ');
write('\n');

write(color);
padding = self.tick ? ' ' : ' ';
write(padding + '"" "" ');
write('\n');

this.cursorUp(this.numberOfLines);
};
Expand Down Expand Up @@ -3746,7 +3740,7 @@ function XUnit(runner) {
runner.on('pass', function(test){
tests.push(test);
});

runner.on('fail', function(test){
tests.push(test);
});
Expand All @@ -3763,7 +3757,7 @@ function XUnit(runner) {
}, false));

tests.forEach(test);
console.log('</testsuite>');
console.log('</testsuite>');
});
}

Expand Down Expand Up @@ -4219,10 +4213,15 @@ Runner.prototype.checkGlobals = function(test){
var leaks;

// check length - 2 ('errno' and 'location' globals)
if (isNode && 1 == ok.length - globals.length) return
if (isNode && 1 == ok.length - globals.length) return;
else if (2 == ok.length - globals.length) return;


if(this.prevGlobalsLength == globals.length) return;
this.prevGlobalsLength = globals.length;

//console.time('filterLeaksTime');
leaks = filterLeaks(ok, globals);
//console.timeEnd('filterLeaksTime');
this._globals = this._globals.concat(leaks);

if (leaks.length > 1) {
Expand Down Expand Up @@ -4584,10 +4583,21 @@ Runner.prototype.run = function(fn){

function filterLeaks(ok, globals) {
return filter(globals, function(key){
// Opera and IE expose global variables for HTML element IDs (issue #243)

// in firefox
// if runner runs in an iframe, this iframe's window.getInterface method not init at first
// it is assigned in some seconds
if (global.navigator && /^getInterface/.test(key)) return false;

// an iframe could be approached by window[iframeIndex]
// in ie6,7,8 and opera, iframeIndex is enumerable, this could cause leak
if (global.navigator && /^\d+/.test(key)) return false;

if (/^mocha-/.test(key)) return false;

var matched = filter(ok, function(ok){
if (~ok.indexOf('*')) return 0 == key.indexOf(ok.split('*')[0]);
// Opera and IE expose global variables for HTML element IDs (issue #243)
if (/^mocha-/.test(key)) return true;
return key == ok;
});
return matched.length == 0 && (!global.navigator || 'onerror' !== key);
Expand Down Expand Up @@ -5006,7 +5016,7 @@ exports.indexOf = function(arr, obj, start){

/**
* Array#reduce (<=IE8)
*
*
* @param {Array} array
* @param {Function} fn
* @param {Object} initial value
Expand Down