Skip to content

Commit

Permalink
fix: correctly strip source file prefixes when no webpack.context is …
Browse files Browse the repository at this point in the history
…defined

This now uses the same default logic as webpack for identifying the source root.
Based on https://webpack.js.org/configuration/entry-context/#context, if the context is not specified, the current
directory that the process is running from should be assumed instead.

Align util to mimic this behaviour to ensure paths are correctly de-absoluted during processing.
  • Loading branch information
peitschie committed Dec 21, 2018
1 parent 50f441f commit 3c48bf8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/util.js
Expand Up @@ -24,16 +24,18 @@ function fixPathSeparators(filePath) {

function fixWebpackSourcePaths(sourceMap, webpackConfig) {
let { sourceRoot } = sourceMap;
// As per https://webpack.js.org/configuration/entry-context/#context, if no context is specified, the current
// directory that the process is running from should be assumed instead
const context = (webpackConfig && webpackConfig.context) || process.cwd();
// Fix for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/32
// The sourceRoot is relative to the project directory and not an absolute path, so add the webpack context to it if set
if (
webpackConfig &&
webpackConfig.context &&
sourceMap.sourceRoot &&
!sourceMap.sourceRoot.startsWith(webpackConfig.context) &&
context &&
sourceRoot &&
!sourceRoot.startsWith(context) &&
!path.isAbsolute(sourceRoot)
) {
sourceRoot = path.join(webpackConfig.context, sourceRoot);
sourceRoot = path.join(context, sourceRoot);
}

sourceRoot = fixPathSeparators(sourceRoot);
Expand Down
27 changes: 26 additions & 1 deletion test/util.spec.js
Expand Up @@ -59,6 +59,7 @@ describe('util', () => {
Object.defineProperty(process, 'platform', {
value: 'win32'
});
process.cwd = () => 'C:/development/git/coverage-istanbul-reporter-path';
const input = {
file:
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts',
Expand Down Expand Up @@ -118,6 +119,7 @@ describe('util', () => {
});

it('should remove the sourceRoot from the source path if present', () => {
process.cwd = () => 'C:/development/git/coverage-istanbul-reporter-path';
const input = {
file:
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts',
Expand Down Expand Up @@ -175,6 +177,29 @@ describe('util', () => {
).to.deep.equal(output);
});

it('should use the current directory if webpack context is not set', () => {
Object.defineProperty(process, 'platform', {
value: 'win32'
});
process.cwd = () =>
'C:\\Users\\mattlewis\\Code\\open-source\\karma-coverage-istanbul-reporter\\test\\fixtures\\typescript';
const input = {
file: 'example.ts',
sourceRoot: 'src/',
sources: [
'C:\\Users\\mattlewis\\Code\\open-source\\karma-coverage-istanbul-reporter\\test\\fixtures\\typescript\\src\\example.ts'
]
};

const output = {
file: 'example.ts',
sourceRoot:
'C:\\Users\\mattlewis\\Code\\open-source\\karma-coverage-istanbul-reporter\\test\\fixtures\\typescript\\src\\',
sources: ['example.ts']
};
expect(fixWebpackSourcePaths(input, undefined)).to.deep.equal(output);
});

it('should only add the webpack context to the source root if not already set', () => {
const input = {
file: 'example.ts',
Expand All @@ -197,7 +222,7 @@ describe('util', () => {
).to.deep.equal(output);
});

it('should not the webpack context to the source root if the source root is absolute', () => {
it('should not add the webpack context to the source root if the source root is absolute', () => {
const input = {
file: 'example.ts',
sourceRoot:
Expand Down

0 comments on commit 3c48bf8

Please sign in to comment.