Skip to content

Commit

Permalink
ensure windows compat with stack trace filter; closes #2502
Browse files Browse the repository at this point in the history
- also removed component-related cruft
- skip stack tests by OS
  • Loading branch information
boneskull committed Sep 27, 2016
1 parent 8ef51a5 commit 8644fd0
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 59 deletions.
19 changes: 12 additions & 7 deletions lib/utils.js
Expand Up @@ -9,7 +9,8 @@ var basename = require('path').basename;
var debug = require('debug')('mocha:watch');
var exists = require('fs').existsSync || require('path').existsSync;
var glob = require('glob');
var join = require('path').join;
var path = require('path');
var join = path.join;
var readdirSync = require('fs').readdirSync;
var statSync = require('fs').statSync;
var watchFile = require('fs').watchFile;
Expand Down Expand Up @@ -720,16 +721,20 @@ exports.getError = function(err) {
*/
exports.stackTraceFilter = function() {
// TODO: Replace with `process.browser`
var slash = '/';
var is = typeof document === 'undefined' ? { node: true } : { browser: true };
var cwd = is.node
? process.cwd() + slash
: (typeof location === 'undefined' ? window.location : location).href.replace(/\/[^\/]*$/, '/');
var slash = path.sep;
var cwd;
if (is.node) {
cwd = process.cwd() + slash;
} else {
cwd = (typeof location === 'undefined' ? window.location : location).href.replace(/\/[^\/]*$/, '/');
slash = '/';
}

function isMochaInternal(line) {
return (~line.indexOf('node_modules' + slash + 'mocha' + slash))
|| (~line.indexOf('components' + slash + 'mochajs' + slash))
|| (~line.indexOf('components' + slash + 'mocha' + slash))
|| (~line.indexOf('node_modules' + slash + 'mocha.js'))
|| (~line.indexOf('bower_components' + slash + 'mocha.js'))
|| (~line.indexOf(slash + 'mocha.js'));
}

Expand Down
6 changes: 6 additions & 0 deletions test/runner.spec.js
Expand Up @@ -358,6 +358,12 @@ describe('Runner', function() {
});

describe('stackTrace', function() {
before(function() {
if (require('path').sep !== '/') {
this.skip();
}
});

var stack = [
'AssertionError: foo bar',
'at EventEmitter.<anonymous> (/usr/local/dev/test.js:16:12)',
Expand Down
163 changes: 111 additions & 52 deletions test/utils.spec.js
@@ -1,5 +1,6 @@
var mocha = require('..');
var utils = mocha.utils;
var path = require('path');
var JSON = require('json3');

describe('utils', function() {
Expand Down Expand Up @@ -97,62 +98,120 @@ describe('utils', function() {
describe('.stackTraceFilter()', function() {
describe('on node', function() {
var filter = utils.stackTraceFilter();
it('should get a stack-trace as a string and prettify it', function() {
var stack = [ 'AssertionError: foo bar'
, 'at EventEmitter.<anonymous> (/usr/local/dev/test.js:16:12)'
, 'at Context.<anonymous> (/usr/local/dev/test.js:19:5)'
, 'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)'
, 'Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)'
, '/usr/local/lib/node_modules/mocha/lib/runner.js:452:12'
, 'next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)'
, '/usr/local/lib/node_modules/mocha/lib/runner.js:309:7'
, 'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)'
, 'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)'
, 'at processImmediate [as _immediateCallback] (timers.js:321:17)'];
filter(stack.join('\n')).should.equal(stack.slice(0,3).join('\n'));

stack = [ 'AssertionError: bar baz'
, 'at /usr/local/dev/some-test-file.js:25:8'
, 'at tryCatcher (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/util.js:24:31)'
, 'at Promise._resolveFromResolver (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:439:31)'
, 'at new Promise (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:53:37)'
, 'at yourFunction (/usr/local/dev/own/tmp/test1.js:24:13)'
, 'at Context.<anonymous> (/usr/local/dev/some-test-file:30:4)'
, 'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)'
, 'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)'
, 'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)'
, 'at processImmediate [as _immediateCallback] (timers.js:321:17)'];

filter(stack.join('\n')).should.equal(stack.slice(0,7).join('\n'));
});
describe('on POSIX OS', function () {
before(function () {
if (path.sep !== '/') {
this.skip();
}
});

it('does not ignore other bower_components and components', function() {
var stack = ['Error: failed'
, 'at assert (index.html:11:26)'
, 'at Context.<anonymous> (test.js:17:18)'
, 'at bower_components/should/should.js:4827:7'
, 'at next (file:///.../bower_components/should/should.js:4766:23)'
, 'at components/should/5.0.0/should.js:4827:7'
, 'at next (file:///.../components/should/5.0.0/should.js:4766:23)'
, 'at file:///.../bower_components/mocha/mocha.js:4794:5'
, 'at timeslice (.../components/mocha/mocha.js:6218:27)'
, 'at Test.require.register.Runnable.run (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4463:15)'
, 'at Runner.require.register.Runner.runTest (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4892:10)'
, 'at file:///.../components/mochajs/mocha/2.1.0/mocha.js:4970:12'
, 'at next (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4817:14)'];
filter(stack.join('\n')).should.equal(stack.slice(0,7).join('\n'));
});
it('should get a stack-trace as a string and prettify it', function () {
var stack = [
'AssertionError: foo bar',
'at EventEmitter.<anonymous> (/usr/local/dev/test.js:16:12)',
'at Context.<anonymous> (/usr/local/dev/test.js:19:5)',
'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)',
'Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)',
'/usr/local/lib/node_modules/mocha/lib/runner.js:452:12',
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)',
'/usr/local/lib/node_modules/mocha/lib/runner.js:309:7',
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)',
'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)',
'at processImmediate [as _immediateCallback] (timers.js:321:17)'
];
filter(stack.join('\n'))
.should
.equal(stack.slice(0, 3)
.join('\n'));

stack = [
'AssertionError: bar baz',
'at /usr/local/dev/some-test-file.js:25:8',
'at tryCatcher (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/util.js:24:31)',
'at Promise._resolveFromResolver (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:439:31)',
'at new Promise (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:53:37)',
'at yourFunction (/usr/local/dev/own/tmp/test1.js:24:13)',
'at Context.<anonymous> (/usr/local/dev/some-test-file:30:4)',
'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)',
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)',
'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)',
'at processImmediate [as _immediateCallback] (timers.js:321:17)'
];

filter(stack.join('\n'))
.should
.equal(stack.slice(0, 7)
.join('\n'));
});

it('does not ignore other bower_components and components',
function () {
var stack = [
'Error: failed',
'at assert (index.html:11:26)',
'at Context.<anonymous> (test.js:17:18)',
'at bower_components/should/should.js:4827:7',
'at next (file:///.../bower_components/should/should.js:4766:23)',
'at components/should/5.0.0/should.js:4827:7',
'at next (file:///.../components/should/5.0.0/should.js:4766:23)',
'at file:///.../bower_components/mocha/mocha.js:4794:5',
'at timeslice (.../components/mocha/mocha.js:6218:27)',
'at Test.require.register.Runnable.run (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4463:15)',
'at Runner.require.register.Runner.runTest (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4892:10)',
'at file:///.../components/mochajs/mocha/2.1.0/mocha.js:4970:12',
'at next (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4817:14)'
];
filter(stack.join('\n'))
.should
.equal(stack.slice(0, 7)
.join('\n'));
});

it('should replace absolute with relative paths', function() {
var stack = ['Error: ' + process.cwd() + '/bla.js has a problem'
, 'at foo (' + process.cwd() + '/foo/index.js:13:226)'
, 'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'];
it('should replace absolute with relative paths', function () {
var stack = [
'Error: ' + process.cwd() + '/bla.js has a problem',
'at foo (' + process.cwd() + '/foo/index.js:13:226)',
'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'
];

var expected = [
'Error: ' + process.cwd() + '/bla.js has a problem',
'at foo (foo/index.js:13:226)',
'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'
];

filter(stack.join('\n'))
.should
.equal(expected.join('\n'));
});
});

var expected = ['Error: ' + process.cwd() + '/bla.js has a problem'
, 'at foo (foo/index.js:13:226)'
, 'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'];
describe('on Windows', function() {
before(function () {
if (path.sep === '/') {
this.skip();
}
});

filter(stack.join('\n')).should.equal(expected.join('\n'));
it('should work on Windows', function () {
var stack = [
'Error: failed',
'at Context.<anonymous> (C:\\Users\\ishida\\src\\test\\test\\mytest.js:5:9)',
'at callFn (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:326:21)',
'at Test.Runnable.run (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:319:7)',
'at Runner.runTest (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:422:10)',
'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:528:12',
'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:342:14)',
'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:352:7',
'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:284:14)',
'at Immediate._onImmediate (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:320:5)'
];
filter(stack.join('\n'))
.should
.equal(stack.slice(0, 2)
.join('\n'));
});
});
});

Expand All @@ -163,7 +222,7 @@ describe('utils', function() {
global.location = { href: 'localhost:3000/foo/bar/index.html' };
filter = utils.stackTraceFilter();
});
it('does not strip out other bower_components and components', function() {
it('does not strip out other bower_components', function() {
var stack = ['Error: failed'
, 'at assert (index.html:11:26)'
, 'at Context.<anonymous> (test.js:17:18)'
Expand Down

0 comments on commit 8644fd0

Please sign in to comment.