From 53a951004d169f3df4f4e2f3d542d87302557fd3 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 --- lib/utils.js | 19 ++++++++++++------- test/utils.spec.js | 22 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 1cfbbae781..cceba264ba 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().replace(/\\/g, '\\') + 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/utils.spec.js b/test/utils.spec.js index 501ea7abe8..9ec64ff1f7 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() { @@ -154,6 +155,25 @@ describe('utils', function() { filter(stack.join('\n')).should.equal(expected.join('\n')); }); + + it('should work on Windows', function() { + if (path.sep === '/') { + return this.skip(); + } + 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,1).join('\n')); + }); }); describe('on browser', function() { @@ -163,7 +183,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)'