Skip to content

Commit d303e4e

Browse files
committed
fix(coverage): support absolute Windows paths
closes #1272
1 parent 50e87d6 commit d303e4e

2 files changed

Lines changed: 112 additions & 8 deletions

File tree

‎src/app/application.ts‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,6 +2813,50 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
28132813
logger.info("Process unit test coverage report");
28142814
return new Promise((resolve, reject) => {
28152815
let covDat, covFileNames;
2816+
const normalizeCoveragePath = (filePath: string): string => {
2817+
return path
2818+
.normalize(filePath)
2819+
.replace(/\\/g, "/")
2820+
.replace(/^[A-Za-z]:\//, "/")
2821+
.replace(/\/+/g, "/")
2822+
.toLowerCase();
2823+
};
2824+
const getSourceRelativePath = (
2825+
normalizedFilePath: string,
2826+
): string => {
2827+
const sourceRootIndex = normalizedFilePath.lastIndexOf("/src/");
2828+
if (sourceRootIndex === -1) {
2829+
return normalizedFilePath;
2830+
}
2831+
return normalizedFilePath.slice(sourceRootIndex + 1);
2832+
};
2833+
const isCoveragePathMatch = (
2834+
sourceFilePath: string,
2835+
coverageFilePath: string,
2836+
): boolean => {
2837+
const normalizedSourcePath = normalizeCoveragePath(sourceFilePath);
2838+
const normalizedCoveragePath =
2839+
normalizeCoveragePath(coverageFilePath);
2840+
2841+
if (
2842+
normalizedSourcePath === normalizedCoveragePath ||
2843+
normalizedSourcePath.endsWith(normalizedCoveragePath) ||
2844+
normalizedCoveragePath.endsWith(normalizedSourcePath)
2845+
) {
2846+
return true;
2847+
}
2848+
2849+
const sourceRelativePath =
2850+
getSourceRelativePath(normalizedSourcePath);
2851+
const coverageRelativePath =
2852+
getSourceRelativePath(normalizedCoveragePath);
2853+
2854+
return (
2855+
sourceRelativePath === coverageRelativePath ||
2856+
sourceRelativePath.endsWith(coverageRelativePath) ||
2857+
coverageRelativePath.endsWith(sourceRelativePath)
2858+
);
2859+
};
28162860

28172861
let coverageData: CoverageData =
28182862
Configuration.mainData.coverageData;
@@ -2865,13 +2909,7 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
28652909
out = { name: fileName, filePath: fileName };
28662910
} else {
28672911
const findMatch = _.filter(covFileNames, (el) => {
2868-
const normalizedFilename = path
2869-
.normalize(fileName)
2870-
.replace(/\\/g, "/");
2871-
return (
2872-
el.includes(fileName) ||
2873-
normalizedFilename.includes(el)
2874-
);
2912+
return isCoveragePathMatch(el, fileName);
28752913
});
28762914
if (findMatch.length > 0) {
28772915
out = _.clone(covDat[findMatch[0]]);

‎test/src/cli/cli-unit-test.spec.ts‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai';
2-
import { temporaryDir, shell, pkg, exists, exec, read, shellAsync } from '../helpers';
2+
import { temporaryDir, shell, pkg, exists, exec, read, shellAsync, fs } from '../helpers';
33

44
const tmp = temporaryDir();
55

@@ -163,4 +163,70 @@ describe('CLI Unit Test Report', () => {
163163
);
164164
});
165165
});
166+
167+
describe('Windows absolute path in JSON', () => {
168+
let stdoutString = undefined,
169+
unitTestFile;
170+
before(function (done) {
171+
tmp.create(tmpFolder);
172+
tmp.copy('./test/fixtures/todomvc-ng2/', tmpFolder);
173+
174+
const winCoverageSummary = JSON.parse(
175+
read(`${tmpFolder}/coverage-summary-win.json`)
176+
);
177+
const absoluteWinCoverageSummary = {};
178+
const absolutePrefix = 'G:\\GitHubCleanup\\SoftwareEngineeringPortfolio\\Portfolio\\';
179+
180+
Object.keys(winCoverageSummary).forEach(key => {
181+
if (key === 'total') {
182+
absoluteWinCoverageSummary[key] = winCoverageSummary[key];
183+
} else {
184+
absoluteWinCoverageSummary[`${absolutePrefix}${key}`] =
185+
winCoverageSummary[key];
186+
}
187+
});
188+
189+
fs.writeFileSync(
190+
`${tmpFolder}/coverage-summary-win-absolute.json`,
191+
JSON.stringify(absoluteWinCoverageSummary)
192+
);
193+
194+
let ls = shell(
195+
'node',
196+
[
197+
'../bin/index-cli.js',
198+
'-p',
199+
'./src/tsconfig.json',
200+
'--unitTestCoverage',
201+
'./coverage-summary-win-absolute.json',
202+
'-d',
203+
'documentation'
204+
],
205+
{ cwd: tmpFolder }
206+
);
207+
208+
if (ls.stderr.toString() !== '') {
209+
console.error(`shell error: ${ls.stderr.toString()}`);
210+
done('error');
211+
}
212+
stdoutString = ls.stdout.toString();
213+
unitTestFile = read(`${distFolder}/unit-test.html`);
214+
done();
215+
});
216+
after(() => tmp.clean(tmpFolder));
217+
218+
it('should have unit test page', () => {
219+
expect(unitTestFile).to.contain('Unit test coverage');
220+
expect(unitTestFile).to.contain('<span class="coverage-count">(22/26)</span>');
221+
});
222+
223+
it('should resolve links for Windows absolute paths', () => {
224+
expect(unitTestFile).to.contain(
225+
'<a href="./components/AppComponent.html">G:\\GitHubCleanup\\SoftwareEngineeringPortfolio\\Portfolio\\src\\app\\app.component.ts</a>'
226+
);
227+
expect(unitTestFile).to.contain(
228+
'<a href="./components/AboutComponent.html">G:\\GitHubCleanup\\SoftwareEngineeringPortfolio\\Portfolio\\src\\app\\about\\about.component.ts</a>'
229+
);
230+
});
231+
});
166232
});

0 commit comments

Comments
 (0)