Skip to content

Commit

Permalink
fix: remove --sfdx-configuration flag and get sfdx-project.json p…
Browse files Browse the repository at this point in the history
…ath using `simple-git`
  • Loading branch information
mcarvin8 committed Apr 23, 2024
1 parent b780b21 commit 87d92f3
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 38 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

The `apex-code-coverage-transformer` is a Salesforce CLI plugin to transform the Apex Code Coverage JSON files created during deployments into the Generic Test Coverage Format (XML). This format is accepted by static code analysis tools like SonarQube.

This plugin requires [git](https://git-scm.com/downloads) to be installed and that it can be called using the command `git`.

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.
Expand Down Expand Up @@ -32,18 +34,17 @@ The `apex-code-coverage-transformer` has 1 command:

- `sf apex-code-coverage transformer transform`

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.
This command needs to be ran somewhere inside your Salesforce DX git repository, whether in the root folder (recommended) or in a subfolder. This plugin will determine the root folder of this repository and read the `sfdx-project.json` file in the root folder. All package directories listed in the `sfdx-project.json` file will be processed when running this plugin.

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

```
USAGE
$ sf apex-code-coverage transformer transform -j <value> -x <value> -c <value> [--json]
$ sf apex-code-coverage transformer transform -j <value> -x <value> [--json]
FLAGS
-j, --coverage-json=<value> Path to the code coverage JSON file created by the Salesforce CLI deployment command.
-x, --xml=<value> [default: coverage.xml] Path to code coverage XML file that will be created by this plugin.
-c, --sfdx-configuration=<value> [default: 'sfdx-project.json'] Path to your project's Salesforce DX configuration file.
GLOBAL FLAGS
--json Format output as json.
Expand All @@ -52,7 +53,7 @@ DESCRIPTION
This plugin will convert the code coverage JSON file created by the Salesforce CLI during Apex deployments into an XML accepted by tools like SonarQube.
EXAMPLES
$ sf apex-code-coverage transformer transform -j "coverage.json" -x "coverage.xml" -c "sfdx-project.json"
$ sf apex-code-coverage transformer transform -j "coverage.json" -x "coverage.xml"
```

## Errors and Warnings
Expand All @@ -73,7 +74,7 @@ Warning: The file name AccountProfile 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:
If the `sfdx-project.json` file was not found in your repository's root folder, the plugin will fail with:

```
Error (1): Salesforce DX Config File does not exist in this path: {filePath}
Expand Down
6 changes: 1 addition & 5 deletions messages/transformer.transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ This plugin will convert the code coverage JSON file created by the Salesforce C

# examples

- `sf apex-code-coverage transformer transform -j "coverage.json" -x "coverage.xml" -c "sfdx-project.json"`

# flags.sfdx-configuration.summary

Path to your project's Salesforce DX configuration file.
- `sf apex-code-coverage transformer transform -j "coverage.json" -x "coverage.xml"`

# flags.coverage-json.summary

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@oclif/core": "^3.18.1",
"@salesforce/core": "^6.4.7",
"@salesforce/sf-plugins-core": "^7.1.3",
"simple-git": "^3.24.0",
"xmlbuilder2": "^3.1.1"
},
"devDependencies": {
Expand Down Expand Up @@ -49,7 +50,8 @@
"json",
"sonarqube",
"apex",
"coverage"
"coverage",
"git"
],
"license": "MIT",
"oclif": {
Expand Down
14 changes: 1 addition & 13 deletions src/commands/apex-code-coverage/transformer/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ export default class TransformerTransform extends SfCommand<TransformerTransform
public static readonly examples = messages.getMessages('examples');

public static readonly flags = {
'sfdx-configuration': Flags.file({
summary: messages.getMessage('flags.sfdx-configuration.summary'),
char: 'c',
required: true,
exists: true,
default: 'sfdx-project.json',
}),
'coverage-json': Flags.file({
summary: messages.getMessage('flags.coverage-json.summary'),
char: 'j',
Expand All @@ -47,15 +40,10 @@ export default class TransformerTransform extends SfCommand<TransformerTransform
const { flags } = await this.parse(TransformerTransform);
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;
const {
xml: xmlData,
warnings,
filesProcessed,
} = await convertToGenericCoverageReport(coverageData, sfdxConfigFile);
const { xml: xmlData, warnings, filesProcessed } = await convertToGenericCoverageReport(coverageData);

// Print warnings if any
if (warnings.length > 0) {
Expand Down
5 changes: 2 additions & 3 deletions src/helpers/convertToGenericCoverageReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { setCoveredLines } from './setCoveredLines.js';
import { normalizePathToUnix } from './normalizePathToUnix.js';

export async function convertToGenericCoverageReport(
data: CoverageData,
dxConfigFile: string
data: CoverageData
): Promise<{ xml: string; warnings: string[]; filesProcessed: number }> {
const coverageObj: CoverageObject = { coverage: { '@version': '1', file: [] } };
const warnings: string[] = [];
Expand All @@ -20,7 +19,7 @@ export async function convertToGenericCoverageReport(
if (!Object.hasOwn(data, fileName)) continue;
const fileInfo = data[fileName];
const formattedFileName = fileName.replace('no-map/', '');
const { repoRoot, relativeFilePath } = await findFilePath(formattedFileName, dxConfigFile);
const { repoRoot, relativeFilePath } = await findFilePath(formattedFileName);
if (relativeFilePath === undefined) {
warnings.push(`The file name ${formattedFileName} was not found in any package directory.`);
continue;
Expand Down
5 changes: 2 additions & 3 deletions src/helpers/findFilePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { join, relative } from 'node:path';
import { getPackageDirectories } from './getPackageDirectories.js';

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

let relativeFilePath: string | undefined;
for (const directory of packageDirectories) {
Expand Down
23 changes: 15 additions & 8 deletions src/helpers/getPackageDirectories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@

import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { resolve } from 'node:path';
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';

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

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}`);
export async function getPackageDirectories(): Promise<{ repoRoot: string; packageDirectories: string[] }> {
const options: Partial<SimpleGitOptions> = {
baseDir: process.cwd(),
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: true,
};
const git: SimpleGit = simpleGit(options);
const repoRoot = (await git.revparse('--show-toplevel')).trim();
const dxConfigPath = resolve(repoRoot, 'sfdx-project.json');
if (!existsSync(dxConfigPath)) {
throw Error(`Salesforce DX Config File does not exist in this path: ${dxConfigPath}`);
}

const sfdxProjectRaw: string = await readFile(dxConfigFile, 'utf-8');
const sfdxProjectRaw: string = await readFile(dxConfigPath, 'utf-8');
const sfdxProject: SfdxProject = JSON.parse(sfdxProjectRaw) as SfdxProject;
const repoRoot = dirname(dxConfigFile);
const packageDirectories = sfdxProject.packageDirectories.map((directory) => resolve(repoRoot, directory.path));
return { repoRoot, packageDirectories };
}
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,18 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@kwsites/file-exists@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
dependencies:
debug "^4.1.1"

"@kwsites/promise-deferred@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz"
Expand Down Expand Up @@ -10634,6 +10646,15 @@ sigstore@^2.2.0:
"@sigstore/tuf" "^2.3.0"
"@sigstore/verify" "^0.1.0"

simple-git@^3.24.0:
version "3.24.0"
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.24.0.tgz#33a8c88dc6fa74e53eaf3d6bfc27d0182a49ec00"
integrity sha512-QqAKee9Twv+3k8IFOFfPB2hnk6as6Y6ACUpwCtQvRYBAes23Wv3SZlHVobAzqcE8gfsisCvPw3HGW3HYM+VYYw==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"
debug "^4.3.4"

simple-swizzle@^0.2.2:
version "0.2.2"
resolved "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz"
Expand Down

0 comments on commit 87d92f3

Please sign in to comment.