From 028e4775a9df3f7362e3e827cf85a99ca0368034 Mon Sep 17 00:00:00 2001 From: Matt Carvin <90224411+mcarvin8@users.noreply.github.com> Date: Fri, 10 May 2024 12:23:18 -0400 Subject: [PATCH] refactor: update hook to use `-c` flag and run after tests --- README.md | 4 +- messages/transformer.transform.md | 4 +- src/hooks/postrun.ts | 86 +++++++++++++++++-------------- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index f93de68..fa4d825 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ USAGE $ sf apex-code-coverage transformer transform -j -x -c [--json] FLAGS - -j, --coverage-json= Path to the code coverage JSON file created by the Salesforce CLI deployment command. + -j, --coverage-json= Path to the code coverage JSON file created by the Salesforce CLI deployment or test command. -x, --xml= [default: "coverage.xml"] Path to code coverage XML file that will be created by this plugin. -c, --command= [default: "deploy"] The type of Salesforce CLI command you are running. Valid options: "deploy" or "test". @@ -55,7 +55,7 @@ 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" diff --git a/messages/transformer.transform.md b/messages/transformer.transform.md index 48e9298..5c8b1ad 100644 --- a/messages/transformer.transform.md +++ b/messages/transformer.transform.md @@ -4,7 +4,7 @@ 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 @@ -12,7 +12,7 @@ This plugin will convert the code coverage JSON file created by the Salesforce C # 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 diff --git a/src/hooks/postrun.ts b/src/hooks/postrun.ts index 3265124..7d6b10c 100644 --- a/src/hooks/postrun.ts +++ b/src/hooks/postrun.ts @@ -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 = { - 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 = { + 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); };