Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Merge branch 'improve-global-leaks'

Travis Jeffery4be2afc7cef0
diff --cc History.md
index 4564f0fb,9bad8355..a1a325bd
--- a/History.md
+++ b/History.md
@@@ -1,71 -1,3 +1,74 @@@
+??? / ???
+==================
+
+ * add: --sort (#813)
+ * fix: diffs show whitespace differences (#976)
+ * update: diff depedency to 1.0.7
++ * fix: improve global leaks (#783)
++ * fix: firefox window.getInterface leak
++ * fix: accessing iframe via window[iframeIndex] leak
+ * update: glob dependency to 3.2.3 (#927)
+ * fix: reporter pending css selector (#970)
+
+1.12.1 / 2013-08-29
+==================
+
+ * remove test.js from .gitignore
+ * update included version of ms.js
+
+1.12.0 / 2013-07-01
+==================
+
+ * add: prevent diffs for differing types. Closes #900
+ * add `Mocha.process` hack for phantomjs
+ * fix: use compilers with requires
+ * fix regexps in diffs. Closes #890
+ * fix xunit NaN on failure. Closes #894
+ * fix: strip tab indentation in `clean` utility method
+ * fix: textmate bundle installation
+
+1.11.0 / 2013-06-12
+==================
+
+ * add --prof support
+ * add --harmony support
+ * add --harmony-generators support
+ * add "Uncaught " prefix to uncaught exceptions
+ * add web workers support
+ * add `suite.skip()`
+ * change to output # of pending / passing even on failures. Closes #872
+ * fix: prevent hooks from being called if we are bailing
+ * fix `this.timeout(0)`
+
+1.10.0 / 2013-05-21
+==================
+
+ * add add better globbing support for windows via `glob` module
+ * add support to pass through flags such as --debug-brk=1234. Closes #852
+ * add test.only, test.skip to qunit interface
+ * change to always use word-based diffs for now. Closes #733
+ * change `mocha init` tests.html to index.html
+ * fix `process` global leak in the browser
+ * fix: use resolve() instead of join() for --require
+ * fix: filterLeaks() condition to not consider indices in global object as leaks
+ * fix: restrict mocha.css styling to #mocha id
+ * fix: save timer references to avoid Sinon interfering in the browser build.
+
+1.9.0 / 2013-04-03
+==================
+
+ * add improved setImmediate implementation
+ * replace --ignore-leaks with --check-leaks
+ * change default of ignoreLeaks to true. Closes #791
+ * remove scrolling for HTML reporter
+ * fix retina support
+ * fix tmbundle, restrict to js scope
+
+1.8.2 / 2013-03-11
+==================
+
+ * add `setImmediate` support for 0.10.x
+ * fix mocha -w spinner on windows
1.8.1 / 2013-01-09
==================
diff --cc lib/runner.js
index c42e4497,6bc69118..dffc3c05
--- a/lib/runner.js
+++ b/lib/runner.js
@@@ -165,10 -158,15 +165,13 @@@ Runner.prototype.checkGlobals = functio
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) {
@@@ -534,12 -528,21 +537,23 @@@ 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)
-
+ // Firefox and Chrome exposes iframes as index inside the window object
+ if (/^d+/.test(key)) return false;
++
+ // 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;
-
++
++ // Opera and IE expose global variables for HTML element IDs (issue #243)
+ 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);
diff --cc mocha.js
index 1ca585d6,77335e2f..0a04e204
--- a/mocha.js
+++ b/mocha.js
@@@ -57,23 -57,23 +57,24 @@@ module.exports = function(type)
}); // module: browser/debug.js
require.register("browser/diff.js", function(module, exports, require){
--/* See license.txt for terms of usage */
++/* See LICENSE file for terms of use */
/*
* Text diff implementation.
-- *
++ *
* This library supports the following APIS:
* JsDiff.diffChars: Character by character diff
* JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace
* JsDiff.diffLines: Line based diff
-- *
++ *
* JsDiff.diffCss: Diff targeted at CSS content
-- *
++ *
* These methods are based on the implementation proposed in
* "An O(ND) Difference Algorithm and its Variations" (Myers, 1986).
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927
*/
var JsDiff = (function() {
++ /*jshint maxparams: 5*/
function clonePath(path) {
return { newPos: path.newPos, components: path.components.slice(0) };
}
@@@ -88,22 -88,22 +89,21 @@@
}
function escapeHTML(s) {
var n = s;
-- n = n.replace(/&/g, "&");
-- n = n.replace(/</g, "&lt;");
-- n = n.replace(/>/g, "&gt;");
-- n = n.replace(/"/g, "&quot;");
++ n = n.replace(/&/g, '&amp;');
++ n = n.replace(/</g, '&lt;');
++ n = n.replace(/>/g, '&gt;');
++ n = n.replace(/"/g, '&quot;');
return n;
}
--
-- var fbDiff = function(ignoreWhitespace) {
++ var Diff = function(ignoreWhitespace) {
this.ignoreWhitespace = ignoreWhitespace;
};
-- fbDiff.prototype = {
++ Diff.prototype = {
diff: function(oldString, newString) {
// Handle the identity case (this is due to unrolling editLength == 0
-- if (newString == oldString) {
++ if (newString === oldString) {
return [{ value: newString }];
}
if (!newString) {
@@@ -186,7 -186,7 +186,7 @@@
while (newPos+1 < newLen && oldPos+1 < oldLen && this.equals(newString[newPos+1], oldString[oldPos+1])) {
newPos++;
oldPos++;
--
++
this.pushComponent(basePath.components, newString[newPos], undefined, undefined);
}
basePath.newPos = newPos;
@@@ -198,7 -198,7 +198,7 @@@
if (this.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right)) {
return true;
} else {
-- return left == right;
++ return left === right;
}
},
join: function(left, right) {
@@@ -208,27 -208,27 +208,31 @@@
return value;
}
};
--
-- var CharDiff = new fbDiff();
--
-- var WordDiff = new fbDiff(true);
-- WordDiff.tokenize = function(value) {
++
++ var CharDiff = new Diff();
++
++ var WordDiff = new Diff(true);
++ var WordWithSpaceDiff = new Diff();
++ WordDiff.tokenize = WordWithSpaceDiff.tokenize = function(value) {
return removeEmpty(value.split(/(\s+|\b)/));
};
--
-- var CssDiff = new fbDiff(true);
++
++ var CssDiff = new Diff(true);
CssDiff.tokenize = function(value) {
return removeEmpty(value.split(/([{}:;,]|\s+)/));
};
--
-- var LineDiff = new fbDiff();
++
++ var LineDiff = new Diff();
LineDiff.tokenize = function(value) {
return value.split(/^/m);
};
--
++
return {
++ Diff: Diff,
++
diffChars: function(oldStr, newStr) { return CharDiff.diff(oldStr, newStr); },
diffWords: function(oldStr, newStr) { return WordDiff.diff(oldStr, newStr); },
++ diffWordsWithSpace: function(oldStr, newStr) { return WordWithSpaceDiff.diff(oldStr, newStr); },
diffLines: function(oldStr, newStr) { return LineDiff.diff(oldStr, newStr); },
diffCss: function(oldStr, newStr) { return CssDiff.diff(oldStr, newStr); },
@@@ -236,16 -236,16 +240,16 @@@
createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) {
var ret = [];
-- ret.push("Index: " + fileName);
-- ret.push("===================================================================");
-- ret.push("--- " + fileName + (typeof oldHeader === "undefined" ? "" : "\t" + oldHeader));
-- ret.push("+++ " + fileName + (typeof newHeader === "undefined" ? "" : "\t" + newHeader));
++ ret.push('Index: ' + fileName);
++ ret.push('===================================================================');
++ ret.push('--- ' + fileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader));
++ ret.push('+++ ' + fileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader));
var diff = LineDiff.diff(oldStr, newStr);
if (!diff[diff.length-1].value) {
diff.pop(); // Remove trailing newline add
}
-- diff.push({value: "", lines: []}); // Append an empty value to make cleanup easier
++ diff.push({value: '', lines: []}); // Append an empty value to make cleanup easier
function contextLines(lines) {
return lines.map(function(entry) { return ' ' + entry; });
@@@ -253,7 -253,7 +257,7 @@@
function eofNL(curRange, i, current) {
var last = diff[diff.length-2],
isLast = i === diff.length-2,
-- isLastOfType = i === diff.length-3 && (current.added === !last.added || current.removed === !last.removed);
++ isLastOfType = i === diff.length-3 && (current.added !== last.added || current.removed !== last.removed);
// Figure out if this is the last line for the given file and missing NL
if (!/\n$/.test(current.value) && (isLast || isLastOfType)) {
@@@ -265,7 -265,7 +269,7 @@@
oldLine = 1, newLine = 1;
for (var i = 0; i < diff.length; i++) {
var current = diff[i],
-- lines = current.lines || current.value.replace(/\n$/, "").split("\n");
++ lines = current.lines || current.value.replace(/\n$/, '').split('\n');
current.lines = lines;
if (current.added || current.removed) {
@@@ -273,14 -273,14 +277,14 @@@
var prev = diff[i-1];
oldRangeStart = oldLine;
newRangeStart = newLine;
--
++
if (prev) {
curRange = contextLines(prev.lines.slice(-4));
oldRangeStart -= curRange.length;
newRangeStart -= curRange.length;
}
}
-- curRange.push.apply(curRange, lines.map(function(entry) { return (current.added?"+":"-") + entry; }));
++ curRange.push.apply(curRange, lines.map(function(entry) { return (current.added?'+':'-') + entry; }));
eofNL(curRange, i, current);
if (current.added) {
@@@ -298,9 -298,9 +302,9 @@@
// end the range and output
var contextSize = Math.min(lines.length, 4);
ret.push(
-- "@@ -" + oldRangeStart + "," + (oldLine-oldRangeStart+contextSize)
-- + " +" + newRangeStart + "," + (newLine-newRangeStart+contextSize)
-- + " @@");
++ '@@ -' + oldRangeStart + ',' + (oldLine-oldRangeStart+contextSize)
++ + ' +' + newRangeStart + ',' + (newLine-newRangeStart+contextSize)
++ + ' @@');
ret.push.apply(ret, curRange);
ret.push.apply(ret, contextLines(lines.slice(0, contextSize)));
if (lines.length <= 4) {
@@@ -318,30 -318,30 +322,93 @@@
return ret.join('\n') + '\n';
},
++ applyPatch: function(oldStr, uniDiff) {
++ var diffstr = uniDiff.split('\n');
++ var diff = [];
++ var remEOFNL = false,
++ addEOFNL = false;
++
++ for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) {
++ if(diffstr[i][0] === '@') {
++ var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/);
++ diff.unshift({
++ start:meh[3],
++ oldlength:meh[2],
++ oldlines:[],
++ newlength:meh[4],
++ newlines:[]
++ });
++ } else if(diffstr[i][0] === '+') {
++ diff[0].newlines.push(diffstr[i].substr(1));
++ } else if(diffstr[i][0] === '-') {
++ diff[0].oldlines.push(diffstr[i].substr(1));
++ } else if(diffstr[i][0] === ' ') {
++ diff[0].newlines.push(diffstr[i].substr(1));
++ diff[0].oldlines.push(diffstr[i].substr(1));
++ } else if(diffstr[i][0] === '\\') {
++ if (diffstr[i-1][0] === '+') {
++ remEOFNL = true;
++ } else if(diffstr[i-1][0] === '-') {
++ addEOFNL = true;
++ }
++ }
++ }
++
++ var str = oldStr.split('\n');
++ for (var i = diff.length - 1; i >= 0; i--) {
++ var d = diff[i];
++ for (var j = 0; j < d.oldlength; j++) {
++ if(str[d.start-1+j] !== d.oldlines[j]) {
++ return false;
++ }
++ }
++ Array.prototype.splice.apply(str,[d.start-1,+d.oldlength].concat(d.newlines));
++ }
++
++ if (remEOFNL) {
++ while (!str[str.length-1]) {
++ str.pop();
++ }
++ } else if (addEOFNL) {
++ str.push('');
++ }
++ return str.join('\n');
++ },
++
convertChangesToXML: function(changes){
var ret = [];
for ( var i = 0; i < changes.length; i++) {
var change = changes[i];
if (change.added) {
-- ret.push("<ins>");
++ ret.push('<ins>');
} else if (change.removed) {
-- ret.push("<del>");
++ ret.push('<del>');
}
ret.push(escapeHTML(change.value));
if (change.added) {
-- ret.push("</ins>");
++ ret.push('</ins>');
} else if (change.removed) {
-- ret.push("</del>");
++ ret.push('</del>');
}
}
-- return ret.join("");
++ return ret.join('');
++ },
++
++ // See: http://code.google.com/p/google-diff-match-patch/wiki/API
++ convertChangesToDMP: function(changes){
++ var ret = [], change;
++ for ( var i = 0; i < changes.length; i++) {
++ change = changes[i];
++ ret.push([(change.added ? 1 : change.removed ? -1 : 0), change.value]);
++ }
++ return ret;
}
};
})();
--if (typeof module !== "undefined") {
++if (typeof module !== 'undefined') {
module.exports = JsDiff;
}
@@@ -1365,7 -1311,7 +1432,7 @@@ function Mocha(options)
this.ui(options.ui);
this.bail(options.bail);
this.reporter(options.reporter);
- if (typeof options.timeout === 'number') this.timeout(options.timeout);
- if (options.timeout) this.timeout(options.timeout);
++ if (null != options.timeout) this.timeout(options.timeout);
if (options.slow) this.slow(options.slow);
}
@@@ -1906,7 -1815,10 +1973,7 @@@ exports.list = function(failures)
// actual / expected diff
if ('string' == typeof actual && 'string' == typeof expected) {
- msg = errorDiff(err, 'Words', escape);
- var len = Math.max(actual.length, expected.length);
-
- if (len < 20) msg = errorDiff(err, 'Chars', escape);
- else msg = errorDiff(err, 'Words', escape);
++ msg = errorDiff(err, 'WordsWithSpace', escape);
// linenos
var lines = msg.split('\n');
@@@ -4329,10 -4213,15 +4396,15 @@@ Runner.prototype.checkGlobals = functio
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) {
@@@ -4698,12 -4583,21 +4770,23 @@@ 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)
-
+ // Firefox and Chrome exposes iframes as index inside the window object
+ if (/^d+/.test(key)) return false;
++
+ // 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;
-
++
++ // Opera and IE expose global variables for HTML element IDs (issue #243)
+ 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);