Skip to content

Commit

Permalink
fix: use correct source file path separators on windows
Browse files Browse the repository at this point in the history
Fixes #47
  • Loading branch information
mattlewis92 committed Sep 1, 2018
1 parent a835e22 commit 938e93c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
19 changes: 12 additions & 7 deletions src/util.js
Expand Up @@ -2,8 +2,6 @@ const path = require('path');
const minimatch = require('minimatch');

function fixWebpackFilePath(filePath) {
const isWin = process.platform.startsWith('win');

if (filePath.indexOf('!') !== -1) {
filePath = filePath.split('!').pop();
}
Expand All @@ -12,11 +10,15 @@ function fixWebpackFilePath(filePath) {
filePath = filePath.split('?')[0];
}

return filePath;
}

function fixPathSeparators(filePath) {
const isWin = process.platform.startsWith('win');
// Workaround for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/9
if (isWin) {
filePath = filePath.replace(/\\/g, '/');
return filePath.replace(/\//g, '\\');
}

return filePath;
}

Expand All @@ -34,12 +36,15 @@ function fixWebpackSourcePaths(sourceMap, webpackConfig) {
sourceRoot = path.join(webpackConfig.context, sourceRoot);
}

sourceRoot = fixPathSeparators(sourceRoot);

return Object.assign({}, sourceMap, {
sourceRoot: sourceRoot, // eslint-disable-line object-shorthand
file: fixPathSeparators(sourceMap.file),
sourceRoot,
sources: (sourceMap.sources || []).map(source => {
source = fixWebpackFilePath(source);
if (sourceMap.sourceRoot && source.startsWith(sourceMap.sourceRoot)) {
source = source.replace(sourceMap.sourceRoot, '');
if (sourceRoot && source.startsWith(sourceRoot)) {
source = source.replace(sourceRoot, '');
}
return source;
})
Expand Down
18 changes: 8 additions & 10 deletions test/util.spec.js
Expand Up @@ -62,34 +62,32 @@ describe('util', () => {
const input = {
file:
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts',
sourceRoot: '',
sourceRoot: 'C:/development/git/coverage-istanbul-reporter-path/',
sources: [
'C:\\development\\git\\coverage-istanbul-reporter-path\\client\\modules\\app\\app.component.ts'
] // eslint-disable-line unicorn/escape-case
};

const output = {
file:
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts',
sourceRoot: '',
sources: [
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts'
]
'C:\\development\\git\\coverage-istanbul-reporter-path\\client\\modules\\app\\app.component.ts',
sourceRoot: 'C:\\development\\git\\coverage-istanbul-reporter-path\\',
sources: ['client\\modules\\app\\app.component.ts']
};

expect(fixWebpackSourcePaths(input)).to.deep.equal(output);
});

it('should not correct path separators on non windows systems', () => {
const input = {
file: '\\foo\\bar',
sourceRoot: '',
file: '/foo/bar',
sourceRoot: '/foo',
sources: ['\\foo\\bar']
};

const output = {
file: '\\foo\\bar',
sourceRoot: '',
file: '/foo/bar',
sourceRoot: '/foo',
sources: ['\\foo\\bar']
};

Expand Down

0 comments on commit 938e93c

Please sign in to comment.