Skip to content

Commit

Permalink
refactor: update hook to use -c flag and run after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed May 10, 2024
1 parent 0e90cb8 commit 028e477
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ USAGE
$ sf apex-code-coverage transformer transform -j <value> -x <value> -c <value> [--json]
FLAGS
-j, --coverage-json=<value> Path to the code coverage JSON file created by the Salesforce CLI deployment command.
-j, --coverage-json=<value> Path to the code coverage JSON file created by the Salesforce CLI deployment or test command.
-x, --xml=<value> [default: "coverage.xml"] Path to code coverage XML file that will be created by this plugin.
-c, --command=<value> [default: "deploy"] The type of Salesforce CLI command you are running. Valid options: "deploy" or "test".
GLOBAL FLAGS
--json Format output as json.
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.
This plugin will convert the code coverage JSON file created by the Salesforce CLI during Apex deployments and test runs into an XML accepted by tools like SonarQube.
EXAMPLES
$ sf apex-code-coverage transformer transform -j "coverage.json" -x "coverage.xml" -c "deploy"
Expand Down
4 changes: 2 additions & 2 deletions messages/transformer.transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Transforms the Code Coverage JSON into the Generic Test Coverage Format (XML).

# 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.
This plugin will convert the code coverage JSON file created by the Salesforce CLI during Apex deployments and test runs into an XML accepted by tools like SonarQube.

# examples

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

# flags.coverage-json.summary

Path to the code coverage JSON file created by the Salesforce CLI deployment command.
Path to the code coverage JSON file created by the Salesforce CLI deployment or test command.

# flags.xml.summary

Expand Down
86 changes: 47 additions & 39 deletions src/hooks/postrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,57 @@ import TransformerTransform from '../commands/apex-code-coverage/transformer/tra
import { ConfigFile } from '../helpers/types.js';

export const postrun: Hook<'postrun'> = async function (options) {
let commandType: string;
if (
['project:deploy:validate', 'project:deploy:start', 'project:deploy:report', 'project:deploy:resume'].includes(
options.Command.id
)
) {
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 configPath = resolve(repoRoot, '.apexcodecovtransformer.config.json');

try {
const jsonString: string = await readFile(configPath, 'utf-8');
configFile = JSON.parse(jsonString) as ConfigFile;
} catch (error) {
return;
}

const coverageJson: string = configFile.coverageJsonPath || '.';
const coverageXml: string = configFile.coverageXmlPath || 'coverage.xml';

if (coverageJson.trim() === '.') {
return;
}

const coverageJsonPath = resolve(coverageJson);
const coverageXmlPath = resolve(coverageXml);

if (!existsSync(coverageJsonPath)) {
return;
}

const commandArgs: string[] = [];
commandArgs.push('--coverage-json');
commandArgs.push(coverageJsonPath);
commandArgs.push('--xml');
commandArgs.push(coverageXmlPath);
await TransformerTransform.run(commandArgs);
commandType = 'deploy';
} else if (['apex:run: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 configPath = resolve(repoRoot, '.apexcodecovtransformer.config.json');

try {
const jsonString: string = await readFile(configPath, 'utf-8');
configFile = JSON.parse(jsonString) as ConfigFile;
} catch (error) {
return;
}

const coverageJson: string = configFile.coverageJsonPath || '.';
const coverageXml: string = configFile.coverageXmlPath || 'coverage.xml';

if (coverageJson.trim() === '.') {
return;
}

const coverageJsonPath = resolve(coverageJson);
const coverageXmlPath = resolve(coverageXml);

if (!existsSync(coverageJsonPath)) {
return;
}

const commandArgs: string[] = [];
commandArgs.push('--coverage-json');
commandArgs.push(coverageJsonPath);
commandArgs.push('--xml');
commandArgs.push(coverageXmlPath);
commandArgs.push('--command');
commandArgs.push(commandType);
await TransformerTransform.run(commandArgs);
};

0 comments on commit 028e477

Please sign in to comment.