Skip to content

Commit

Permalink
Merge pull request #12 from mcarvin8/fix/path-resolution
Browse files Browse the repository at this point in the history
fix: fix path resolution when running in non-root directories
  • Loading branch information
mcarvin8 authored Apr 22, 2024
2 parents 6b739c4 + c16fe7d commit 924be08
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 28 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The `apex-code-coverage-transformer` is a Salesforce CLI plugin to transform the

This plugin supports code coverage metrics created for Apex Classes and Apex Triggers. This also supports multiple package directories as listed in your project's `sfdx-project.json` configuration, assuming unique file-names are used in your package directories.

This plugin is intended for users who deploy their Apex codebase from a git-based repository and use SonarQube for code quality. This plugin will work if you run local tests or run all tests in an org, including tests that originate from installed managed and unlocked packages. SonarQube relies on file-paths to map code coverage to the files in their file explorer interface. Since files from managed and unlocked packages aren't retrieved into git-based Salesforce repositories, these files cannot be included in your SonarQube scans. If your Apex code coverage JSON output includes managed/unlocked package files, they will not be added to the coverage XML created by this plugin. A warning will be printed for each file not found in a package directory in your git repository. See [Errors and Warnings](https://github.com/mcarvin8/apex-code-coverage-transformer?tab=readme-ov-file#errors-and-warnings) for more information.

To create the code coverage JSON during a Salesforce CLI deployment/validation, append `--coverage-formatters json --results-dir coverage` to the `sf project deploy` command:

```
Expand All @@ -14,7 +16,7 @@ sf project deploy [start/validate] -x manifest/package.xml -l RunSpecifiedTests

This will create a coverage JSON in this relative path - `coverage/coverage/coverage.json`

This JSON isn't accepted by SonarQube automatically and needs to be converted using this plugin.
This JSON isn't accepted by SonarQube automatically for git-based Salesforce repositories and needs to be converted using this plugin.

**Disclaimer**: Due to existing bugs with how the Salesforce CLI reports covered lines (see [5511](https://github.com/forcedotcom/salesforcedx-vscode/issues/5511) and [1568](https://github.com/forcedotcom/cli/issues/1568)), to add support for covered lines in this plugin, I had to add a function to re-number out-of-range covered lines the CLI may report (ex: line 100 in a 98-line Apex Class is reported back as covered by the Salesforce CLI deploy command). Salesforce's coverage result may also include extra lines as covered (ex: 120 lines are included in the coverage report for a 100 line file), so the coverage percentage may vary based on how many lines the API returns in the coverage report. Once Salesforce fixes the API to correctly return covered lines in the deploy command, this function will be removed.

Expand All @@ -30,7 +32,7 @@ The `apex-code-coverage-transformer` has 1 command:

- `sf apex-code-coverage transformer transform`

Recommend running this command in the same directory that your `sfdx-project.json` file is located in. This command will use the `packageDirectories` in the JSON file to set the file-paths in the coverage file.
I recommend running this command in the repository's root folder where your `sfdx-project.json` file is located, but the command will work if you supply a different path for the `--sfdx-configuration`/`-c` flag. This command will use the parent directory of the `sfdx-project.json` file found via the `-c` flag to locate the package directories listed.

## `sf apex-code-coverage transformer transform`

Expand Down Expand Up @@ -61,7 +63,7 @@ Any file in the coverage JSON that isn't found in any package directory will res
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.
Files not found in any package directory will not be added to the coverage XML. This includes Apex classes that originate from installed managed and unlocked packages when running all tests in your org.

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:

Expand Down
9 changes: 3 additions & 6 deletions src/commands/apex-code-coverage/transformer/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ export default class TransformerTransform extends SfCommand<TransformerTransform

public async run(): Promise<TransformerTransformResult> {
const { flags } = await this.parse(TransformerTransform);
let jsonFilePath = flags['coverage-json'];
let xmlFilePath = flags['xml'];
let sfdxConfigFile = flags['sfdx-configuration'];
jsonFilePath = resolve(jsonFilePath);
xmlFilePath = resolve(xmlFilePath);
sfdxConfigFile = resolve(sfdxConfigFile);
const jsonFilePath = resolve(flags['coverage-json']);
const xmlFilePath = resolve(flags['xml']);
const sfdxConfigFile = resolve(flags['sfdx-configuration']);

const jsonData = await readFile(jsonFilePath, 'utf-8');
const coverageData = JSON.parse(jsonData) as CoverageData;
Expand Down
9 changes: 5 additions & 4 deletions src/helpers/convertToGenericCoverageReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { create } from 'xmlbuilder2';
import { CoverageData, CoverageObject, FileObject } from './types.js';
import { findFilePath } from './findFilePath.js';
import { setCoveredLines } from './setCoveredLines.js';
import { normalizePathToUnix } from './normalizePathToUnix.js';

export async function convertToGenericCoverageReport(
data: CoverageData,
Expand All @@ -19,8 +20,8 @@ export async function convertToGenericCoverageReport(
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) {
const { repoRoot, relativeFilePath } = await findFilePath(formattedFileName, dxConfigFile);
if (relativeFilePath === undefined) {
warnings.push(`The file name ${formattedFileName} was not found in any package directory.`);
continue;
}
Expand All @@ -32,15 +33,15 @@ export async function convertToGenericCoverageReport(
.map(Number);

const fileObj: FileObject = {
'@path': filePath,
'@path': normalizePathToUnix(relativeFilePath),
lineToCover: uncoveredLines.map((lineNumber: number) => ({
'@lineNumber': lineNumber,
'@covered': 'false',
})),
};

// this function is only needed until Salesforce fixes the API to correctly return covered lines
await setCoveredLines(coveredLines, uncoveredLines, filePath, fileObj);
await setCoveredLines(coveredLines, uncoveredLines, repoRoot, relativeFilePath, fileObj);
filesProcessed++;
coverageObj.coverage.file.push(fileObj);
}
Expand Down
31 changes: 20 additions & 11 deletions src/helpers/findFilePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
/* eslint-disable no-await-in-loop */

import { readdir, stat } from 'node:fs/promises';
import { join } from 'node:path/posix';
import { join, relative } from 'node:path';

import { getPackageDirectories } from './getPackageDirectories.js';

export async function findFilePath(fileName: string, dxConfigFile: string): Promise<string | undefined> {
const packageDirectories = await getPackageDirectories(dxConfigFile);
export async function findFilePath(
fileName: string,
dxConfigFile: string
): Promise<{ repoRoot: string; relativeFilePath: string | undefined }> {
const { repoRoot, packageDirectories } = await getPackageDirectories(dxConfigFile);

let filePath: string | undefined;
let relativeFilePath: string | undefined;
for (const directory of packageDirectories) {
filePath = await findFilePathinDirectory(fileName, directory);
if (filePath !== undefined) {
relativeFilePath = await findFilePathinDirectory(fileName, directory, repoRoot);
if (relativeFilePath !== undefined) {
break;
}
}
return filePath;
return { repoRoot, relativeFilePath };
}

async function searchRecursively(fileName: string, dxDirectory: string): Promise<string | undefined> {
Expand All @@ -36,19 +39,25 @@ async function searchRecursively(fileName: string, dxDirectory: string): Promise
return undefined;
}

async function findFilePathinDirectory(fileName: string, dxDirectory: string): Promise<string | undefined> {
async function findFilePathinDirectory(
fileName: string,
dxDirectory: string,
repoRoot: string
): Promise<string | undefined> {
const fileExtension = fileName.split('.').slice(1).join('.');
let relativeFilePath: string | undefined;

if (fileExtension) {
// If file extension is defined, search recursively with that extension
relativeFilePath = await searchRecursively(fileName, dxDirectory);
const absoluteFilePath = await searchRecursively(fileName, dxDirectory);
if (absoluteFilePath !== undefined) relativeFilePath = relative(repoRoot, absoluteFilePath);
} else {
// If file extension is not defined, test each extension option
const fileExts: string[] = ['cls', 'trigger'];
for (const ext of fileExts) {
relativeFilePath = await searchRecursively(`${fileName}.${ext}`, dxDirectory);
if (relativeFilePath !== undefined) {
const absoluteFilePath = await searchRecursively(`${fileName}.${ext}`, dxDirectory);
if (absoluteFilePath !== undefined) {
relativeFilePath = relative(repoRoot, absoluteFilePath);
break;
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/helpers/getPackageDirectories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@

import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';

import { SfdxProject } from './types.js';

export async function getPackageDirectories(dxConfigFile: string): Promise<string[]> {
export async function getPackageDirectories(
dxConfigFile: string
): Promise<{ repoRoot: string; packageDirectories: string[] }> {
if (!existsSync(dxConfigFile)) {
throw Error(`Salesforce DX Config File does not exist in this path: ${dxConfigFile}`);
}

const sfdxProjectRaw: string = await readFile(dxConfigFile, 'utf-8');
const sfdxProject: SfdxProject = JSON.parse(sfdxProjectRaw) as SfdxProject;
const packageDirectories = sfdxProject.packageDirectories.map((directory) => directory.path);
return packageDirectories;
const repoRoot = dirname(dxConfigFile);
const packageDirectories = sfdxProject.packageDirectories.map((directory) => resolve(repoRoot, directory.path));
return { repoRoot, packageDirectories };
}
5 changes: 5 additions & 0 deletions src/helpers/normalizePathToUnix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

export function normalizePathToUnix(path: string): string {
return path.replace(/\\/g, '/');
}
5 changes: 4 additions & 1 deletion src/helpers/setCoveredLines.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
'use strict';

import { join } from 'node:path';

import { getTotalLines } from './getTotalLines.js';
import { FileObject } from './types.js';

export async function setCoveredLines(
coveredLines: number[],
uncoveredLines: number[],
repoRoot: string,
filePath: string,
fileObj: FileObject
): Promise<void> {
const randomLines: number[] = [];
const totalLines = await getTotalLines(filePath);
const totalLines = await getTotalLines(join(repoRoot, filePath));
for (const coveredLine of coveredLines) {
if (coveredLine > totalLines) {
for (let randomLineNumber = 1; randomLineNumber <= totalLines; randomLineNumber++) {
Expand Down

0 comments on commit 924be08

Please sign in to comment.