diff --git a/src/coverageUtils.ts b/src/coverageUtils.ts index 7bcf7f829..e220dd090 100644 --- a/src/coverageUtils.ts +++ b/src/coverageUtils.ts @@ -39,27 +39,32 @@ function mapTestResults(testResults: Failures[] | Successes[]): ApexTestResultDa }); } -export function prepCoverageForDisplay(codeCoverage: CodeCoverage[]): CodeCoverage[] { +export function prepCoverageForDisplay(codeCoverage: CodeCoverage[]): Array { 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); diff --git a/src/formatters/deployResultFormatter.ts b/src/formatters/deployResultFormatter.ts index c4d9b1230..0591bbfc3 100644 --- a/src/formatters/deployResultFormatter.ts +++ b/src/formatters/deployResultFormatter.ts @@ -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' }, diff --git a/src/formatters/mdapi/mdDeployResultFormatter.ts b/src/formatters/mdapi/mdDeployResultFormatter.ts index 92a6d2afe..56c1a473e 100644 --- a/src/formatters/mdapi/mdDeployResultFormatter.ts +++ b/src/formatters/mdapi/mdDeployResultFormatter.ts @@ -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' },