Skip to content

Commit

Permalink
fix: include apex get test command in the hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Jun 10, 2024
1 parent 92a4c41 commit 34718f0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ This plugin supports code coverage metrics created for Apex Classes and Apex Tri

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. This will create a coverage JSON in this relative path - `coverage/coverage/coverage.json`.
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. This will create a coverage JSON in this relative path - `coverage/coverage/coverage.json`.

```
sf project deploy [start/validate] -x manifest/package.xml -l RunSpecifiedTests -t {testclasses} --verbose --coverage-formatters json --results-dir coverage
sf project deploy [start/validate] --coverage-formatters json --results-dir "coverage"
```

To create the code coverage JSON when running tests directly in the org, append `-c -r json` to the `sf apex run test` command.
To create the code coverage JSON when running tests directly in the org, append `--code-coverage --result-format json --output-dir "coverage"` to the `sf apex run test` or `sf apex get test` command. This will create the code coverage JSON in the a folder named "coverage".

```
sf apex run test -c -r json
sf apex run test --code-coverage --result-format json --output-dir "coverage"
sf apex get test --test-run-id <test run id> --code-coverage --result-format json --output-dir "coverage"
```

The code coverage JSONs created by the Salesforce CLI aren't accepted by SonarQube automatically for git-based Salesforce repositories and needs to be converted using this plugin.
Expand Down Expand Up @@ -65,7 +66,7 @@ EXAMPLES

A post-run hook has been configured if you elect to use it.

The post-run hook will automatically transform the code coverage JSON file into a generic test coverage report XML after every Salesforce CLI deployment (`sf project deploy start`, `sf project deploy validate`, `sf project deploy report`, `sf project deploy resume` commands) and test run (`sf apex run test` command) if the JSON is found.
The post-run hook will automatically transform the code coverage JSON file into a generic test coverage report XML after every Salesforce CLI deployment (`sf project deploy start`, `sf project deploy validate`, `sf project deploy report`, `sf project deploy resume` commands) and test run (`sf apex run test` and `sf apex get test` commands) if the JSON is found.

The hook requires you to create this file in the root of your repo: `.apexcodecovtransformer.config.json`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apex-code-coverage-transformer",
"description": "Transforms the Apex code coverage JSON created during Salesforce deployments into the Generic Test Coverage Format (XML).",
"description": "Transforms the Apex code coverage JSON created during Salesforce deployments and test runs into the Generic Test Coverage Format (XML) for SonarQube.",
"version": "1.7.3",
"dependencies": {
"@oclif/core": "^3.18.1",
Expand Down
11 changes: 2 additions & 9 deletions src/helpers/getPackageDirectories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@
import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';

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

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 repoRoot = await getRepoRoot();
const dxConfigPath = resolve(repoRoot, 'sfdx-project.json');
if (!existsSync(dxConfigPath)) {
throw Error(`Salesforce DX Config File does not exist in this path: ${dxConfigPath}`);
Expand Down
15 changes: 15 additions & 0 deletions src/helpers/getRepoRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';

export async function getRepoRoot(): Promise<string> {
const gitOptions: Partial<SimpleGitOptions> = {
baseDir: process.cwd(),
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: true,
};

const git: SimpleGit = simpleGit(gitOptions);
const repoRoot = (await git.revparse('--show-toplevel')).trim();
return repoRoot;
}
15 changes: 4 additions & 11 deletions src/hooks/postrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { Hook } from '@oclif/core';
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';

import TransformerTransform from '../commands/apex-code-coverage/transformer/transform.js';
import { ConfigFile } from '../helpers/types.js';
import { getRepoRoot } from '../helpers/getRepoRoot.js';

export const postrun: Hook<'postrun'> = async function (options) {
let commandType: string;
Expand All @@ -16,21 +17,13 @@ export const postrun: Hook<'postrun'> = async function (options) {
)
) {
commandType = 'deploy';
} else if (['apex:run:test'].includes(options.Command.id)) {
} else if (['apex:run:test', 'apex:get:test'].includes(options.Command.id)) {
commandType = 'test';
} else {
return;
}
let configFile: ConfigFile;
const gitOptions: Partial<SimpleGitOptions> = {
baseDir: process.cwd(),
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: true,
};

const git: SimpleGit = simpleGit(gitOptions);
const repoRoot = (await git.revparse('--show-toplevel')).trim();
const repoRoot = await getRepoRoot();
const configPath = resolve(repoRoot, '.apexcodecovtransformer.config.json');

try {
Expand Down

0 comments on commit 34718f0

Please sign in to comment.