From 6b9e8ea80f9101429f82eed7fb3539c71dc613f4 Mon Sep 17 00:00:00 2001 From: Matt Carvin <90224411+mcarvin8@users.noreply.github.com> Date: Tue, 26 Mar 2024 13:25:21 -0400 Subject: [PATCH] fix: warn if a file isn't found and fail if no files are found --- README.md | 31 +++++++++++++++++++ .../transformer/transform.ts | 17 +++++++++- src/helpers/convertToGenericCoverageReport.ts | 18 +++++++---- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 28abda8..e16f17a 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,37 @@ EXAMPLES $ apex-code-coverage transformer transform -j "test.json" -x "coverage.xml" -c "sfdx-project.json" ``` +## Errors and Warnings + +Any file in the coverage JSON that isn't found in any package directory will result in this warning: + +``` +Warning: The file name AccountTrigger was not found in any package directory. +``` + +Files not found in any package directory will not be added to the coverage XML. + +If none of the files listed in the coverage JSON were found in a package directory, the plugin will fail with this error in addition to the above warnings: + +``` +Warning: The file name AccountTrigger was not found in any package directory. +Warning: The file name AccountProfile was not found in any package directory. +Warning: The file name Get_Info was not found in any package directory. +Error (1): None of the files listed in the coverage JSON were processed. +``` + +If the `sfdx-project.json` file was not found, the plugin will fail with: + +``` +Error (1): Salesforce DX Config File does not exist in this path: {filePath} +``` + +Any ENOENT failures indicate that the plugin had issues finding one of the package directories in the `sfdx-project.json` file: + +``` +Error (1): ENOENT: no such file or directory: {packageDirPath} +``` + ## Example This [code coverage JSON file](https://raw.githubusercontent.com/mcarvin8/apex-code-coverage-transformer/main/coverage_no_file_exts.json) created by the Salesforce CLI will be transformed into: diff --git a/src/commands/apex-code-coverage/transformer/transform.ts b/src/commands/apex-code-coverage/transformer/transform.ts index ebc9af7..af511a2 100644 --- a/src/commands/apex-code-coverage/transformer/transform.ts +++ b/src/commands/apex-code-coverage/transformer/transform.ts @@ -54,7 +54,22 @@ export default class TransformerTransform extends SfCommand 0) { + warnings.forEach((warning) => { + this.warn(warning); + }); + } + + if (filesProcessed === 0) { + this.error('None of the files listed in the coverage JSON were processed.'); + } // Write the XML data to the XML file try { diff --git a/src/helpers/convertToGenericCoverageReport.ts b/src/helpers/convertToGenericCoverageReport.ts index b5e0254..17de5fd 100644 --- a/src/helpers/convertToGenericCoverageReport.ts +++ b/src/helpers/convertToGenericCoverageReport.ts @@ -5,8 +5,13 @@ import { CoverageData } from './types.js'; import { getTotalLines } from './getTotalLines.js'; import { findFilePath } from './findFilePath.js'; -export async function convertToGenericCoverageReport(data: CoverageData, dxConfigFile: string): Promise { +export async function convertToGenericCoverageReport( + data: CoverageData, + dxConfigFile: string +): Promise<{ xml: string; warnings: string[]; filesProcessed: number }> { let xml = '\n\n'; + const warnings: string[] = []; + let filesProcessed: number = 0; for (const fileName in data) { if (!Object.hasOwn(data, fileName)) continue; @@ -14,14 +19,15 @@ export async function convertToGenericCoverageReport(data: CoverageData, dxConfi const formattedFileName = fileName.replace('no-map/', ''); const filePath = await findFilePath(formattedFileName, dxConfigFile); if (filePath === undefined) { - throw Error(`The file name ${formattedFileName} was not found in any package directory.`); + warnings.push(`The file name ${formattedFileName} was not found in any package directory.`); + continue; } // Extract the "uncovered lines" from the JSON data const uncoveredLines = Object.keys(fileInfo.s) - .filter(lineNumber => fileInfo.s[lineNumber] === 0) + .filter((lineNumber) => fileInfo.s[lineNumber] === 0) .map(Number); const coveredLines = Object.keys(fileInfo.s) - .filter(lineNumber => fileInfo.s[lineNumber] === 1) + .filter((lineNumber) => fileInfo.s[lineNumber] === 1) .map(Number); const randomLines: number[] = []; const totalLines = getTotalLines(filePath); @@ -49,9 +55,9 @@ export async function convertToGenericCoverageReport(data: CoverageData, dxConfi xml += `\t\t\n`; } } - + filesProcessed++; xml += '\t\n'; } xml += ''; - return xml; + return { xml, warnings, filesProcessed }; }