From 2c6e6bb022924a5b04775a3076d24f24bb6ecc4b Mon Sep 17 00:00:00 2001
From: Matt Carvin <90224411+mcarvin8@users.noreply.github.com>
Date: Wed, 6 Mar 2024 11:55:53 -0500
Subject: [PATCH] fix: ensure covered lines in XML match JSON total
---
src/helpers/convertToGenericCoverageReport.ts | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/helpers/convertToGenericCoverageReport.ts b/src/helpers/convertToGenericCoverageReport.ts
index 6559bee..bbd5a01 100644
--- a/src/helpers/convertToGenericCoverageReport.ts
+++ b/src/helpers/convertToGenericCoverageReport.ts
@@ -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);
@@ -30,6 +30,7 @@ export function convertToGenericCoverageReport(data: CoverageData, dxDirectory:
for (const uncoveredLine of uncoveredLines) {
xml += `\t\t\n`;
}
+ let totalCoveredLinesInXML = 0;
for (const coveredLine of coveredLines) {
if (coveredLine > totalLines) {
@@ -37,14 +38,17 @@ export function convertToGenericCoverageReport(data: CoverageData, dxDirectory:
if (
!uncoveredLines.includes(randomLineNumber) &&
!coveredLines.includes(randomLineNumber) &&
- !randomLines.includes(randomLineNumber)
+ !randomLines.includes(randomLineNumber) &&
+ totalCoveredLinesInXML < coveredLines.length
) {
xml += `\t\t\n`;
randomLines.push(randomLineNumber);
+ totalCoveredLinesInXML++;
}
}
} else {
xml += `\t\t\n`;
+ totalCoveredLinesInXML++;
}
}
@@ -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;