karma-runner/karma

Stacktrace rewrite (sourceMap support) fails for absolute Windows paths

Open

#1097 opened on Jun 10, 2014

View on GitHub
 (1 comment) (0 reactions) (0 assignees)JavaScript (11,918 stars) (1,703 forks)batch import
help wantedtype: backlogtype: bugwindows

Description

The regex for attaching source locations in lib/reporter.js#L25 fails to match absolute Windows path.

The problem is that the regular expression matches drive letters in path such as C:/foo,js as as the path name. This leads to the stack trace below not being rewritten.

Error: intentional
    at http://localhost:9876/absoluteC:/foo.js?da39a3ee5e6b4b0d3255bfef95601890afd80709:17

We could make the used regular expression less restrictive by removing the \\: part from the path segment to fix the issue.

var URL_REGEXP = new RegExp('http:\\/\\/[^\\/]*\\/' +
                              '(base|absolute)' + // prefix
                              '([^\\?\\s\\]*)' + // path
                              '(\\?\\w*)?' +      // sha
                              '(\\:(\\d+))?' +    // line
                              '(\\:(\\d+))?' +    // column
                              '', 'g');`

This however this breaks the existing test case.

An additional test case for test/unit/reporter.spec.coffee that must must pass on Windows systems could be the following:

      it 'should rewrite absolute stack trace (Windows)', (done) ->

        formatError = m.createErrorFormatter '/some/base', emitter, MockSourceMapConsumer
        servedFiles = [new File('C:/b.js')]
        servedFiles[0].sourceMap = 'SOURCE MAP b.js'

        emitter.emit 'file_list_modified', q(served: servedFiles)

        scheduleNextTick ->
          ERROR = 'at http://localhost:123/base/C:/b.js:2:6'
          expect(formatError ERROR).to.equal 'at /some/base/b.js:2:6 <- C:/original/b.js:4:8\n'
          done()

Maybe I am mistaken but I believe a simple regex fix will not solve the issue across all platforms.

Give me the OK to rewrite the URL matching with some more sophisticated logic and I am happy to provide a pull request.

Contributor guide