Skip to content

Commit

Permalink
fix: handle source roots being undefined on windows
Browse files Browse the repository at this point in the history
Fixes #55
  • Loading branch information
mattlewis92 committed Sep 8, 2018
1 parent 897653b commit 8eba911
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/util.js
Expand Up @@ -16,7 +16,7 @@ function fixWebpackFilePath(filePath) {
function fixPathSeparators(filePath) {
const isWin = process.platform.startsWith('win');
// Workaround for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/9
if (isWin) {
if (isWin && filePath) {
return filePath.replace(/\//g, '\\');
}
return filePath;
Expand All @@ -38,9 +38,8 @@ function fixWebpackSourcePaths(sourceMap, webpackConfig) {

sourceRoot = fixPathSeparators(sourceRoot);

return Object.assign({}, sourceMap, {
const result = Object.assign({}, sourceMap, {
file: fixPathSeparators(sourceMap.file),
sourceRoot,
sources: (sourceMap.sources || []).map(source => {
source = fixWebpackFilePath(source);
if (sourceRoot && source.startsWith(sourceRoot)) {
Expand All @@ -49,6 +48,12 @@ function fixWebpackSourcePaths(sourceMap, webpackConfig) {
return source;
})
});

if (sourceRoot) {
result.sourceRoot = sourceRoot;
}

return result;
}

function isAbsolute(file) {
Expand Down
23 changes: 23 additions & 0 deletions test/util.spec.js
Expand Up @@ -78,6 +78,29 @@ describe('util', () => {
expect(fixWebpackSourcePaths(input)).to.deep.equal(output);
});

it('should handle undefined source roots', () => {
Object.defineProperty(process, 'platform', {
value: 'win32'
});
const input = {
file:
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts',
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',
sources: [
'C:\\development\\git\\coverage-istanbul-reporter-path\\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',
Expand Down

0 comments on commit 8eba911

Please sign in to comment.