From 253958105394c5a87035d054d7b7eb2400aab073 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Thu, 14 Sep 2023 09:10:34 -0700 Subject: [PATCH] refactor: share table printing function (dry) --- src/formatters/codeCoverageTable.ts | 42 +++++++++++++++++++ src/formatters/deployResultFormatter.ts | 23 +--------- .../mdapi/mdDeployResultFormatter.ts | 24 +---------- 3 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 src/formatters/codeCoverageTable.ts diff --git a/src/formatters/codeCoverageTable.ts b/src/formatters/codeCoverageTable.ts new file mode 100644 index 000000000..6b277be68 --- /dev/null +++ b/src/formatters/codeCoverageTable.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ +import { ensureArray } from '@salesforce/kit'; +import { Ux } from '@salesforce/sf-plugins-core'; +import { CodeCoverage } from '@salesforce/source-deploy-retrieve'; +import chalk = require('chalk'); +import { prepCoverageForDisplay } from '../coverageUtils'; + +/** + * prints a table of formatted code coverage results if there are any + * This is a no-op if tests didn't run or there is no coverage + * + * @param coverageFromMdapiResult + * @param ux + */ +export const maybePrintCodeCoverageTable = (coverageFromMdapiResult: CodeCoverage | CodeCoverage[], ux: Ux): void => { + const codeCoverage = ensureArray(coverageFromMdapiResult); + + if (codeCoverage.length) { + const coverage = prepCoverageForDisplay(codeCoverage); + + ux.log(''); + ux.styledHeader(chalk.blue('Apex Code Coverage')); + + ux.table( + coverage.map((entry) => ({ + name: entry.name, + numLocations: entry.numLocations, + lineNotCovered: entry.lineNotCovered, + })), + { + name: { header: 'Name' }, + numLocations: { header: '% Covered' }, + lineNotCovered: { header: 'Uncovered Lines' }, + } + ); + } +}; diff --git a/src/formatters/deployResultFormatter.ts b/src/formatters/deployResultFormatter.ts index 0591bbfc3..4ce2f7226 100644 --- a/src/formatters/deployResultFormatter.ts +++ b/src/formatters/deployResultFormatter.ts @@ -22,9 +22,9 @@ import { Successes, } from '@salesforce/source-deploy-retrieve'; import { Ux } from '@salesforce/sf-plugins-core'; -import { prepCoverageForDisplay } from '../coverageUtils'; import { ResultFormatter, ResultFormatterOptions } from './resultFormatter'; import { MdDeployResult } from './mdapi/mdDeployResultFormatter'; +import { maybePrintCodeCoverageTable } from './codeCoverageTable'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/plugin-source', 'deploy'); @@ -304,26 +304,7 @@ export class DeployResultFormatter extends ResultFormatter { } ); } - const codeCoverage = ensureArray(this.result?.response?.details?.runTestResult?.codeCoverage); - - if (codeCoverage.length) { - const coverage = prepCoverageForDisplay(codeCoverage); - - this.ux.log(''); - this.ux.styledHeader(chalk.blue('Apex Code Coverage')); - this.ux.table( - coverage.map((cov) => ({ - name: cov.name, - numLocations: cov.numLocations, - lineNotCovered: cov.lineNotCovered, - })), - { - name: { header: 'Name' }, - numLocations: { header: '% Covered' }, - lineNotCovered: { header: 'Uncovered Lines' }, - } - ); - } + maybePrintCodeCoverageTable(this.result.response.details?.runTestResult?.codeCoverage, this.ux); } protected verboseTestTime(): void { diff --git a/src/formatters/mdapi/mdDeployResultFormatter.ts b/src/formatters/mdapi/mdDeployResultFormatter.ts index 56c1a473e..455ca466a 100644 --- a/src/formatters/mdapi/mdDeployResultFormatter.ts +++ b/src/formatters/mdapi/mdDeployResultFormatter.ts @@ -19,7 +19,7 @@ import { import { ensureArray } from '@salesforce/kit'; import { Ux } from '@salesforce/sf-plugins-core'; import { CoverageResultsFileInfo, ResultFormatter, ResultFormatterOptions } from '../resultFormatter'; -import { prepCoverageForDisplay } from '../../coverageUtils'; +import { maybePrintCodeCoverageTable } from '../codeCoverageTable'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/plugin-source', 'md.deploy'); @@ -212,27 +212,7 @@ export class MdDeployResultFormatter extends ResultFormatter { } ); } - const codeCoverage = ensureArray(this.result?.response?.details?.runTestResult?.codeCoverage); - - if (codeCoverage.length) { - const coverage = prepCoverageForDisplay(codeCoverage); - - this.ux.log(''); - this.ux.styledHeader(chalk.blue('Apex Code Coverage')); - - this.ux.table( - coverage.map((entry) => ({ - name: entry.name, - numLocations: entry.numLocations, - lineNotCovered: entry.lineNotCovered, - })), - { - name: { header: 'Name' }, - numLocations: { header: '% Covered' }, - lineNotCovered: { header: 'Uncovered Lines' }, - } - ); - } + maybePrintCodeCoverageTable(this.result.response.details?.runTestResult?.codeCoverage, this.ux); } protected verboseTestTime(): void {