Skip to content

Commit

Permalink
fix: not covered lines aren't json
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Sep 14, 2023
1 parent b3ecdf3 commit 8555f6f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
33 changes: 19 additions & 14 deletions src/coverageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,32 @@ function mapTestResults(testResults: Failures[] | Successes[]): ApexTestResultDa
});
}

export function prepCoverageForDisplay(codeCoverage: CodeCoverage[]): CodeCoverage[] {
export function prepCoverageForDisplay(codeCoverage: CodeCoverage[]): Array<CodeCoverage & { lineNotCovered: string }> {
const coverage = codeCoverage.sort((a, b) => (a.name.toUpperCase() > b.name.toUpperCase() ? 1 : -1));

coverage.forEach((cov: CodeCoverage & { lineNotCovered: string }) => {
const numLocationsNum = parseInt(cov.numLocations, 10);
const numLocationsNotCovered = parseInt(cov.numLocationsNotCovered, 10);
const color = numLocationsNotCovered > 0 ? chalk.red : chalk.green;

const coverageDecimal = parseFloat(((numLocationsNum - numLocationsNotCovered) / numLocationsNum).toFixed(2));
const pctCovered = numLocationsNum > 0 ? coverageDecimal * 100 : 100;
cov.numLocations = color(`${pctCovered}%`);

cov.lineNotCovered = cov.locationsNotCovered
return coverage.map((cov) => ({
...cov,
numLocations: stylePercentage(calculateCoveragePercent(cov)),
lineNotCovered: cov.locationsNotCovered
? ensureArray(cov.locationsNotCovered)
.map((location) => location.line)
.join(',')
: '';
});
return coverage;
: '',
}));
}

const stylePercentage = (pct: number): string => {
const color = pct < 75 ? chalk.red : pct >= 90 ? chalk.green : chalk.yellow;
return color(`${pct}%`);
};

const calculateCoveragePercent = (cov: CodeCoverage): number => {
const numLocationsNum = parseInt(cov.numLocations, 10);
const numLocationsNotCovered = parseInt(cov.numLocationsNotCovered, 10);
const coverageDecimal = parseFloat(((numLocationsNum - numLocationsNotCovered) / numLocationsNum).toFixed(2));
return numLocationsNum > 0 ? coverageDecimal * 100 : 100;
};

function generateCoveredLines(cov: CodeCoverage): [number[], number[]] {
const numCovered = parseInt(cov.numLocations, 10);
const numUncovered = parseInt(cov.numLocationsNotCovered, 10);
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/deployResultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export class DeployResultFormatter extends ResultFormatter {
coverage.map((cov) => ({
name: cov.name,
numLocations: cov.numLocations,
lineNotCovered: cov.locationsNotCovered,
lineNotCovered: cov.lineNotCovered,
})),
{
name: { header: 'Name' },
Expand Down
3 changes: 1 addition & 2 deletions src/formatters/mdapi/mdDeployResultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,11 @@ export class MdDeployResultFormatter extends ResultFormatter {
this.ux.log('');
this.ux.styledHeader(chalk.blue('Apex Code Coverage'));

// TODO: unsure about locationsNotCovered vs lineNotCovered
this.ux.table(
coverage.map((entry) => ({
name: entry.name,
numLocations: entry.numLocations,
lineNotCovered: entry.locationsNotCovered,
lineNotCovered: entry.lineNotCovered,
})),
{
name: { header: 'Name' },
Expand Down

0 comments on commit 8555f6f

Please sign in to comment.