Skip to content

Commit

Permalink
refactor: move covered line adjustment to a temp function
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Mar 27, 2024
1 parent 848fba6 commit 40934fb
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/helpers/convertToGenericCoverageReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,42 @@ export async function convertToGenericCoverageReport(
const coveredLines = Object.keys(fileInfo.s)
.filter((lineNumber) => fileInfo.s[lineNumber] === 1)
.map(Number);
const randomLines: number[] = [];
const totalLines = getTotalLines(filePath);

xml += `\t<file path="${filePath}">\n`;

for (const uncoveredLine of uncoveredLines) {
xml += `\t\t<lineToCover lineNumber="${uncoveredLine}" covered="false"/>\n`;
}

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)
) {
xml += `\t\t<lineToCover lineNumber="${randomLineNumber}" covered="true"/>\n`;
randomLines.push(randomLineNumber);
break;
}
}
} else {
xml += `\t\t<lineToCover lineNumber="${coveredLine}" covered="true"/>\n`;
}
}
// this function is only needed until Salesforce fixes the API to correctly return covered lines
xml += setCoveredLines(coveredLines, uncoveredLines, filePath);
filesProcessed++;
xml += '\t</file>\n';
}
xml += '</coverage>';
return { xml, warnings, filesProcessed };
}

function setCoveredLines(coveredLines: number[], uncoveredLines: number[], filePath: string): string {
let formattedCoveredLines: string = '';
const randomLines: number[] = [];
const totalLines = getTotalLines(filePath);
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)
) {
formattedCoveredLines += `\t\t<lineToCover lineNumber="${randomLineNumber}" covered="true"/>\n`;
randomLines.push(randomLineNumber);
break;
}
}
} else {
formattedCoveredLines += `\t\t<lineToCover lineNumber="${coveredLine}" covered="true"/>\n`;
}
}
return formattedCoveredLines;
}

0 comments on commit 40934fb

Please sign in to comment.