Skip to content

Commit

Permalink
fix: warn if a file isn't found and fail if no files are found
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Mar 26, 2024
1 parent eaf905e commit 6b9e8ea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion src/commands/apex-code-coverage/transformer/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,22 @@ export default class TransformerTransform extends SfCommand<TransformerTransform

const jsonData = fs.readFileSync(jsonFilePath, 'utf-8');
const coverageData = JSON.parse(jsonData) as CoverageData;
const xmlData = await convertToGenericCoverageReport(coverageData, sfdxConfigFile);
const {
xml: xmlData,
warnings,
filesProcessed,
} = await convertToGenericCoverageReport(coverageData, sfdxConfigFile);

// Print warnings if any
if (warnings.length > 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 {
Expand Down
18 changes: 12 additions & 6 deletions src/helpers/convertToGenericCoverageReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ import { CoverageData } from './types.js';
import { getTotalLines } from './getTotalLines.js';
import { findFilePath } from './findFilePath.js';

export async function convertToGenericCoverageReport(data: CoverageData, dxConfigFile: string): Promise<string> {
export async function convertToGenericCoverageReport(
data: CoverageData,
dxConfigFile: string
): Promise<{ xml: string; warnings: string[]; filesProcessed: number }> {
let xml = '<?xml version="1.0"?>\n<coverage version="1">\n';
const warnings: string[] = [];
let filesProcessed: number = 0;

for (const fileName in data) {
if (!Object.hasOwn(data, fileName)) continue;
const fileInfo = data[fileName];
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);
Expand Down Expand Up @@ -49,9 +55,9 @@ export async function convertToGenericCoverageReport(data: CoverageData, dxConfi
xml += `\t\t<lineToCover lineNumber="${coveredLine}" covered="true"/>\n`;
}
}

filesProcessed++;
xml += '\t</file>\n';
}
xml += '</coverage>';
return xml;
return { xml, warnings, filesProcessed };
}

0 comments on commit 6b9e8ea

Please sign in to comment.