Skip to content

Commit

Permalink
fix: ensure covered lines in XML match JSON total
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Mar 6, 2024
1 parent dca9768 commit 2c6e6bb
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/helpers/convertToGenericCoverageReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export function convertToGenericCoverageReport(data: CoverageData, dxDirectory:
}
// Extract the "uncovered lines" from the JSON data
const uncoveredLines = Object.keys(classInfo.s)
.filter(lineNumber => classInfo.s[lineNumber] === 0)
.map(Number);
.filter(lineNumber => classInfo.s[lineNumber] === 0)
.map(Number);
const coveredLines = Object.keys(classInfo.s)
.filter(lineNumber => classInfo.s[lineNumber] === 1)
.map(Number);
.filter(lineNumber => classInfo.s[lineNumber] === 1)
.map(Number);
const randomLines: number[] = [];
const totalLines = getTotalLines(filePath);

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

for (const coveredLine of coveredLines) {
if (coveredLine > totalLines) {
for (let randomLineNumber = 1; randomLineNumber <= totalLines; randomLineNumber++) {
if (
!uncoveredLines.includes(randomLineNumber) &&
!coveredLines.includes(randomLineNumber) &&
!randomLines.includes(randomLineNumber)
!randomLines.includes(randomLineNumber) &&
totalCoveredLinesInXML < coveredLines.length
) {
xml += `\t\t<lineToCover lineNumber="${randomLineNumber}" covered="true"/>\n`;
randomLines.push(randomLineNumber);
totalCoveredLinesInXML++;
}
}
} else {
xml += `\t\t<lineToCover lineNumber="${coveredLine}" covered="true"/>\n`;
totalCoveredLinesInXML++;
}
}

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


function getTotalLines(filePath: string): number {
const fileContent = fs.readFileSync(filePath, 'utf8');
return fileContent.split(/\r\n|\r|\n/).length;
Expand Down

0 comments on commit 2c6e6bb

Please sign in to comment.