Skip to content

Commit e1b5c9a

Browse files
committed
ensure windows compat with stack trace filter; closes #2502
- also removed component-related cruft - skip stack tests by OS
1 parent 8ef51a5 commit e1b5c9a

File tree

3 files changed

+136
-59
lines changed

3 files changed

+136
-59
lines changed

lib/utils.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var basename = require('path').basename;
99
var debug = require('debug')('mocha:watch');
1010
var exists = require('fs').existsSync || require('path').existsSync;
1111
var glob = require('glob');
12-
var join = require('path').join;
12+
var path = require('path');
13+
var join = path.join;
1314
var readdirSync = require('fs').readdirSync;
1415
var statSync = require('fs').statSync;
1516
var watchFile = require('fs').watchFile;
@@ -720,16 +721,20 @@ exports.getError = function(err) {
720721
*/
721722
exports.stackTraceFilter = function() {
722723
// TODO: Replace with `process.browser`
723-
var slash = '/';
724724
var is = typeof document === 'undefined' ? { node: true } : { browser: true };
725-
var cwd = is.node
726-
? process.cwd() + slash
727-
: (typeof location === 'undefined' ? window.location : location).href.replace(/\/[^\/]*$/, '/');
725+
var slash = path.sep;
726+
var cwd;
727+
if (is.node) {
728+
cwd = process.cwd() + slash;
729+
} else {
730+
cwd = (typeof location === 'undefined' ? window.location : location).href.replace(/\/[^\/]*$/, '/');
731+
slash = '/';
732+
}
728733

729734
function isMochaInternal(line) {
730735
return (~line.indexOf('node_modules' + slash + 'mocha' + slash))
731-
|| (~line.indexOf('components' + slash + 'mochajs' + slash))
732-
|| (~line.indexOf('components' + slash + 'mocha' + slash))
736+
|| (~line.indexOf('node_modules' + slash + 'mocha.js'))
737+
|| (~line.indexOf('bower_components' + slash + 'mocha.js'))
733738
|| (~line.indexOf(slash + 'mocha.js'));
734739
}
735740

test/runner.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var Suite = mocha.Suite;
33
var Runner = mocha.Runner;
44
var Test = mocha.Test;
55
var Hook = mocha.Hook;
6+
var path = require('path');
67

78
function noop() {}
89

@@ -374,6 +375,12 @@ describe('Runner', function() {
374375

375376
describe('shortStackTrace', function() {
376377
it('should prettify the stack-trace', function(done) {
378+
before(function() {
379+
if (path.sep !== '/') {
380+
this.skip();
381+
}
382+
});
383+
377384
var hook = new Hook();
378385
var err = new Error();
379386
// Fake stack-trace
@@ -388,6 +395,12 @@ describe('Runner', function() {
388395
});
389396

390397
describe('longStackTrace', function() {
398+
before(function() {
399+
if (path.sep !== '/') {
400+
this.skip();
401+
}
402+
});
403+
391404
it('should display the full stack-trace', function(done) {
392405
var hook = new Hook();
393406
var err = new Error();

test/utils.spec.js

+111-52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var mocha = require('..');
22
var utils = mocha.utils;
3+
var path = require('path');
34
var JSON = require('json3');
45

56
describe('utils', function() {
@@ -97,62 +98,120 @@ describe('utils', function() {
9798
describe('.stackTraceFilter()', function() {
9899
describe('on node', function() {
99100
var filter = utils.stackTraceFilter();
100-
it('should get a stack-trace as a string and prettify it', function() {
101-
var stack = [ 'AssertionError: foo bar'
102-
, 'at EventEmitter.<anonymous> (/usr/local/dev/test.js:16:12)'
103-
, 'at Context.<anonymous> (/usr/local/dev/test.js:19:5)'
104-
, 'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)'
105-
, 'Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)'
106-
, '/usr/local/lib/node_modules/mocha/lib/runner.js:452:12'
107-
, 'next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)'
108-
, '/usr/local/lib/node_modules/mocha/lib/runner.js:309:7'
109-
, 'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)'
110-
, 'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)'
111-
, 'at processImmediate [as _immediateCallback] (timers.js:321:17)'];
112-
filter(stack.join('\n')).should.equal(stack.slice(0,3).join('\n'));
113-
114-
stack = [ 'AssertionError: bar baz'
115-
, 'at /usr/local/dev/some-test-file.js:25:8'
116-
, 'at tryCatcher (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/util.js:24:31)'
117-
, 'at Promise._resolveFromResolver (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:439:31)'
118-
, 'at new Promise (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:53:37)'
119-
, 'at yourFunction (/usr/local/dev/own/tmp/test1.js:24:13)'
120-
, 'at Context.<anonymous> (/usr/local/dev/some-test-file:30:4)'
121-
, 'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)'
122-
, 'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)'
123-
, 'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)'
124-
, 'at processImmediate [as _immediateCallback] (timers.js:321:17)'];
125101

126-
filter(stack.join('\n')).should.equal(stack.slice(0,7).join('\n'));
127-
});
102+
describe('on POSIX OS', function () {
103+
before(function () {
104+
if (path.sep !== '/') {
105+
this.skip();
106+
}
107+
});
128108

129-
it('does not ignore other bower_components and components', function() {
130-
var stack = ['Error: failed'
131-
, 'at assert (index.html:11:26)'
132-
, 'at Context.<anonymous> (test.js:17:18)'
133-
, 'at bower_components/should/should.js:4827:7'
134-
, 'at next (file:///.../bower_components/should/should.js:4766:23)'
135-
, 'at components/should/5.0.0/should.js:4827:7'
136-
, 'at next (file:///.../components/should/5.0.0/should.js:4766:23)'
137-
, 'at file:///.../bower_components/mocha/mocha.js:4794:5'
138-
, 'at timeslice (.../components/mocha/mocha.js:6218:27)'
139-
, 'at Test.require.register.Runnable.run (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4463:15)'
140-
, 'at Runner.require.register.Runner.runTest (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4892:10)'
141-
, 'at file:///.../components/mochajs/mocha/2.1.0/mocha.js:4970:12'
142-
, 'at next (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4817:14)'];
143-
filter(stack.join('\n')).should.equal(stack.slice(0,7).join('\n'));
144-
});
109+
it('should get a stack-trace as a string and prettify it', function () {
110+
var stack = [
111+
'AssertionError: foo bar',
112+
'at EventEmitter.<anonymous> (/usr/local/dev/test.js:16:12)',
113+
'at Context.<anonymous> (/usr/local/dev/test.js:19:5)',
114+
'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)',
115+
'Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)',
116+
'/usr/local/lib/node_modules/mocha/lib/runner.js:452:12',
117+
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)',
118+
'/usr/local/lib/node_modules/mocha/lib/runner.js:309:7',
119+
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)',
120+
'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)',
121+
'at processImmediate [as _immediateCallback] (timers.js:321:17)'
122+
];
123+
filter(stack.join('\n'))
124+
.should
125+
.equal(stack.slice(0, 3)
126+
.join('\n'));
127+
128+
stack = [
129+
'AssertionError: bar baz',
130+
'at /usr/local/dev/some-test-file.js:25:8',
131+
'at tryCatcher (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/util.js:24:31)',
132+
'at Promise._resolveFromResolver (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:439:31)',
133+
'at new Promise (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:53:37)',
134+
'at yourFunction (/usr/local/dev/own/tmp/test1.js:24:13)',
135+
'at Context.<anonymous> (/usr/local/dev/some-test-file:30:4)',
136+
'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)',
137+
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)',
138+
'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)',
139+
'at processImmediate [as _immediateCallback] (timers.js:321:17)'
140+
];
141+
142+
filter(stack.join('\n'))
143+
.should
144+
.equal(stack.slice(0, 7)
145+
.join('\n'));
146+
});
147+
148+
it('does not ignore other bower_components and components',
149+
function () {
150+
var stack = [
151+
'Error: failed',
152+
'at assert (index.html:11:26)',
153+
'at Context.<anonymous> (test.js:17:18)',
154+
'at bower_components/should/should.js:4827:7',
155+
'at next (file:///.../bower_components/should/should.js:4766:23)',
156+
'at components/should/5.0.0/should.js:4827:7',
157+
'at next (file:///.../components/should/5.0.0/should.js:4766:23)',
158+
'at file:///.../bower_components/mocha/mocha.js:4794:5',
159+
'at timeslice (.../components/mocha/mocha.js:6218:27)',
160+
'at Test.require.register.Runnable.run (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4463:15)',
161+
'at Runner.require.register.Runner.runTest (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4892:10)',
162+
'at file:///.../components/mochajs/mocha/2.1.0/mocha.js:4970:12',
163+
'at next (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4817:14)'
164+
];
165+
filter(stack.join('\n'))
166+
.should
167+
.equal(stack.slice(0, 7)
168+
.join('\n'));
169+
});
145170

146-
it('should replace absolute with relative paths', function() {
147-
var stack = ['Error: ' + process.cwd() + '/bla.js has a problem'
148-
, 'at foo (' + process.cwd() + '/foo/index.js:13:226)'
149-
, 'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'];
171+
it('should replace absolute with relative paths', function () {
172+
var stack = [
173+
'Error: ' + process.cwd() + '/bla.js has a problem',
174+
'at foo (' + process.cwd() + '/foo/index.js:13:226)',
175+
'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'
176+
];
177+
178+
var expected = [
179+
'Error: ' + process.cwd() + '/bla.js has a problem',
180+
'at foo (foo/index.js:13:226)',
181+
'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'
182+
];
183+
184+
filter(stack.join('\n'))
185+
.should
186+
.equal(expected.join('\n'));
187+
});
188+
});
150189

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

155-
filter(stack.join('\n')).should.equal(expected.join('\n'));
197+
it('should work on Windows', function () {
198+
var stack = [
199+
'Error: failed',
200+
'at Context.<anonymous> (C:\\Users\\ishida\\src\\test\\test\\mytest.js:5:9)',
201+
'at callFn (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:326:21)',
202+
'at Test.Runnable.run (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:319:7)',
203+
'at Runner.runTest (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:422:10)',
204+
'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:528:12',
205+
'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:342:14)',
206+
'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:352:7',
207+
'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:284:14)',
208+
'at Immediate._onImmediate (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:320:5)'
209+
];
210+
filter(stack.join('\n'))
211+
.should
212+
.equal(stack.slice(0, 2)
213+
.join('\n'));
214+
});
156215
});
157216
});
158217

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

0 commit comments

Comments
 (0)