Skip to content

Commit 2c6e6bb

Browse files
committed
fix: ensure covered lines in XML match JSON total
1 parent dca9768 commit 2c6e6bb

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/helpers/convertToGenericCoverageReport.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export function convertToGenericCoverageReport(data: CoverageData, dxDirectory:
1717
}
1818
// Extract the "uncovered lines" from the JSON data
1919
const uncoveredLines = Object.keys(classInfo.s)
20-
.filter(lineNumber => classInfo.s[lineNumber] === 0)
21-
.map(Number);
20+
.filter(lineNumber => classInfo.s[lineNumber] === 0)
21+
.map(Number);
2222
const coveredLines = Object.keys(classInfo.s)
23-
.filter(lineNumber => classInfo.s[lineNumber] === 1)
24-
.map(Number);
23+
.filter(lineNumber => classInfo.s[lineNumber] === 1)
24+
.map(Number);
2525
const randomLines: number[] = [];
2626
const totalLines = getTotalLines(filePath);
2727

@@ -30,21 +30,25 @@ export function convertToGenericCoverageReport(data: CoverageData, dxDirectory:
3030
for (const uncoveredLine of uncoveredLines) {
3131
xml += `\t\t<lineToCover lineNumber="${uncoveredLine}" covered="false"/>\n`;
3232
}
33+
let totalCoveredLinesInXML = 0;
3334

3435
for (const coveredLine of coveredLines) {
3536
if (coveredLine > totalLines) {
3637
for (let randomLineNumber = 1; randomLineNumber <= totalLines; randomLineNumber++) {
3738
if (
3839
!uncoveredLines.includes(randomLineNumber) &&
3940
!coveredLines.includes(randomLineNumber) &&
40-
!randomLines.includes(randomLineNumber)
41+
!randomLines.includes(randomLineNumber) &&
42+
totalCoveredLinesInXML < coveredLines.length
4143
) {
4244
xml += `\t\t<lineToCover lineNumber="${randomLineNumber}" covered="true"/>\n`;
4345
randomLines.push(randomLineNumber);
46+
totalCoveredLinesInXML++;
4447
}
4548
}
4649
} else {
4750
xml += `\t\t<lineToCover lineNumber="${coveredLine}" covered="true"/>\n`;
51+
totalCoveredLinesInXML++;
4852
}
4953
}
5054

@@ -55,6 +59,7 @@ export function convertToGenericCoverageReport(data: CoverageData, dxDirectory:
5559
return xml;
5660
}
5761

62+
5863
function getTotalLines(filePath: string): number {
5964
const fileContent = fs.readFileSync(filePath, 'utf8');
6065
return fileContent.split(/\r\n|\r|\n/).length;

0 commit comments

Comments
 (0)