From 8644fd0f9319922c3922ed4b9a3a76c727942a3e Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Mon, 26 Sep 2016 21:47:58 -0700 Subject: [PATCH] ensure windows compat with stack trace filter; closes #2502 - also removed component-related cruft - skip stack tests by OS --- lib/utils.js | 19 ++++-- test/runner.spec.js | 6 ++ test/utils.spec.js | 163 ++++++++++++++++++++++++++++++-------------- 3 files changed, 129 insertions(+), 59 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 1cfbbae781..bb2c99c48f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; @@ -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')); } diff --git a/test/runner.spec.js b/test/runner.spec.js index ffe4d70e8c..d33dcdc8d4 100644 --- a/test/runner.spec.js +++ b/test/runner.spec.js @@ -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. (/usr/local/dev/test.js:16:12)', diff --git a/test/utils.spec.js b/test/utils.spec.js index 501ea7abe8..4683809ae6 100644 --- a/test/utils.spec.js +++ b/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() { @@ -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. (/usr/local/dev/test.js:16:12)' - , 'at Context. (/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. (/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. (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. (/usr/local/dev/test.js:16:12)', + 'at Context. (/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. (/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. (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. (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')); + }); }); }); @@ -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. (test.js:17:18)'