From 010ee34fa97c6f3c36ebb0ab1c740610de9bd7eb Mon Sep 17 00:00:00 2001 From: Matt Carvin <90224411+mcarvin8@users.noreply.github.com> Date: Tue, 23 Apr 2024 12:34:58 -0400 Subject: [PATCH] fix: remove `--sfdx-configuration` flag and find `sfdx-project.json` dynamically --- README.md | 17 +++++++++-------- messages/delta.md | 6 +----- src/commands/apex-tests-git-delta/delta.ts | 10 +--------- src/service/extractTestClasses.ts | 5 ++--- src/service/getPackageDirectories.ts | 8 +++++--- src/service/validateClassPaths.ts | 3 +-- 6 files changed, 19 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 3a05a3b..d095b46 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![NPM](https://img.shields.io/npm/v/apex-tests-git-delta.svg?label=apex-tests-git-delta)](https://www.npmjs.com/package/apex-tests-git-delta) [![Downloads/week](https://img.shields.io/npm/dw/apex-tests-git-delta.svg)](https://npmjs.org/package/apex-tests-git-delta) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/mcarvin8/apex-tests-git-delta/main/LICENSE.md) -The `apex-tests-git-delta` is a simple Salesforce CLI plugin to take 2 commit SHAs in a Salesforce Git repository and return the delta Apex tests to run against when executing a delta deployment. +The `apex-tests-git-delta` is a Salesforce CLI plugin to take 2 commit SHAs in a Salesforce DX git repository and return the delta Apex tests to run against when executing a delta deployment. This plugin requires [git](https://git-scm.com/downloads) to be installed and that it can be called using the command `git`. @@ -17,9 +17,9 @@ For example, if the user creates a file named `regex.txt` in their repository wi Commit messages that follow the above regular expression can look like: ``` -fix: required updates to account trigger and opportunity trigger handler Apex::AccountTriggerHandlerTest OpportunityTriggerHandlerTest::Apex` -chore: add sandbox refresh class Apex::PrepareMySandboxTest::Apex` -fix: fix quoting issues Apex::QuoteControllerTest::Apex` +fix: required updates to account trigger and opportunity trigger handler Apex::AccountTriggerHandlerTest OpportunityTriggerHandlerTest::Apex +chore: add sandbox refresh class Apex::PrepareMySandboxTest::Apex +fix: fix quoting issues Apex::QuoteControllerTest::Apex ``` The 3 commit messages above will be parsed to retrieve all test classes found using the regular expression. Test classes can be separated by commas, spaces, or both in the commit message. This plugin will separate all tests by a single space and sort them alphabetically when creating the final output. @@ -60,19 +60,20 @@ The `apex-tests-git-delta` has 1 command: - `sf apex-tests-git-delta delta` -Recommend running this command in your project's root directory. +This command needs to be ran somewhere inside your Salesforce DX git repository, whether it's the root folder (recommended) or a subfolder. + +This command will determine the root folder of the repo and look for the `sfdx-project.json` file in the root folder. ## `sf apex-tests-git-delta delta` ``` USAGE - $ sf apex-tests-git-delta delta -f -t -e -c --output [--json] + $ sf apex-tests-git-delta delta -f -t -e --output [--json] FLAGS -f, --from= Commit SHA from where the commit message log is done. This SHA's commit message will not be included in the results. -t, --to= [default: HEAD] Commit SHA to where the commit message log is done. -e, --regular-expression= [default: regex.txt] The text file containing the Apex Tests regular expression to search for. - -c, --sfdx-configuration= [default: sfdx-project.json] Path to your project's Salesforce DX configuration file. --output= [default: runTests.txt] The text file to save the delta test classes to. GLOBAL FLAGS @@ -82,5 +83,5 @@ DESCRIPTION Given 2 git commits, this plugin will parse all of the commit messages between this range and return the delta Apex test class string. This can be used to execute delta deployments. EXAMPLES - $ sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --regular-expression "regex.txt" --sfdx-configuration "sfdx-project.json" --output "runTests.txt" + $ sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --regular-expression "regex.txt" --output "runTests.txt" ``` diff --git a/messages/delta.md b/messages/delta.md index 3363998..b797696 100644 --- a/messages/delta.md +++ b/messages/delta.md @@ -8,7 +8,7 @@ Given 2 git commits, this plugin will parse all of the commit messages between t # examples -- `sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --regular-expression "regex.txt" --sfdx-configuration "sfdx-project.json" --output "runTests.txt"` +- `sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --regular-expression "regex.txt" --output "runTests.txt"` # flags.from.summary @@ -22,10 +22,6 @@ Commit SHA to where the commit message log is done. The text file containing the Apex Tests regular expression to search for. -# flags.sfdx-configuration.summary - -Path to your project's Salesforce DX configuration file. - # flags.output.summary The text file to save the delta test classes to. diff --git a/src/commands/apex-tests-git-delta/delta.ts b/src/commands/apex-tests-git-delta/delta.ts index dfeb358..5f29526 100644 --- a/src/commands/apex-tests-git-delta/delta.ts +++ b/src/commands/apex-tests-git-delta/delta.ts @@ -44,13 +44,6 @@ export default class ApexTestDelta extends SfCommand { exists: false, default: 'runTests.txt', }), - 'sfdx-configuration': Flags.file({ - summary: messages.getMessage('flags.sfdx-configuration.summary'), - char: 'c', - required: true, - exists: true, - default: 'sfdx-project.json', - }), }; public async run(): Promise { @@ -59,9 +52,8 @@ export default class ApexTestDelta extends SfCommand { const fromGitRef = flags['from']; const regExFile = flags['regular-expression']; const output = flags['output']; - const sfdxConfigFile = flags['sfdx-configuration']; - const result = await extractTestClasses(fromGitRef, toGitRef, regExFile, sfdxConfigFile); + const result = await extractTestClasses(fromGitRef, toGitRef, regExFile); const tests = result.validatedClasses; const warnings = result.warnings; await writeFile(output, tests); diff --git a/src/service/extractTestClasses.ts b/src/service/extractTestClasses.ts index fa4f2fa..21bd4ed 100644 --- a/src/service/extractTestClasses.ts +++ b/src/service/extractTestClasses.ts @@ -8,8 +8,7 @@ import { validateClassPaths } from './validateClassPaths.js'; export async function extractTestClasses( fromRef: string, toRef: string, - regex: string, - sfdxConfigFile: string + regex: string ): Promise<{ validatedClasses: string; warnings: string[] }> { const options: Partial = { baseDir: process.cwd(), @@ -39,7 +38,7 @@ export async function extractTestClasses( let validatedClasses: string = ''; const result = unvalidatedClasses.length > 0 - ? await validateClassPaths(unvalidatedClasses, sfdxConfigFile, toRef, git) + ? await validateClassPaths(unvalidatedClasses, toRef, git) : { validatedClasses: new Set(), warnings: [] }; let sortedClasses: string[] = []; if (result.validatedClasses.size > 0) { diff --git a/src/service/getPackageDirectories.ts b/src/service/getPackageDirectories.ts index eeac1f1..4265373 100644 --- a/src/service/getPackageDirectories.ts +++ b/src/service/getPackageDirectories.ts @@ -4,17 +4,19 @@ import { existsSync } from 'node:fs'; import { readFile } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; +import { SimpleGit } from 'simple-git'; interface SfdxProject { packageDirectories: Array<{ path: string }>; } export async function getPackageDirectories( - dxConfigFile: string + git: SimpleGit ): Promise<{ repoRoot: string; packageDirectories: string[] }> { - const dxConfigFilePath = resolve(dxConfigFile); + const rootFolder = (await git.revparse('--show-toplevel')).trim(); + const dxConfigFilePath = resolve(rootFolder, 'sfdx-project.json'); if (!existsSync(dxConfigFilePath)) { - throw Error(`Salesforce DX Config File does not exist in this path: ${dxConfigFile}`); + throw Error(`Cannot find sfdx-project.json in the root folder: ${rootFolder}`); } const sfdxProjectRaw: string = await readFile(dxConfigFilePath, 'utf-8'); diff --git a/src/service/validateClassPaths.ts b/src/service/validateClassPaths.ts index a1bbe94..f40c58b 100644 --- a/src/service/validateClassPaths.ts +++ b/src/service/validateClassPaths.ts @@ -7,11 +7,10 @@ import { getPackageDirectories } from './getPackageDirectories.js'; export async function validateClassPaths( unvalidatedClasses: string[], - dxConfigFile: string, toCommitHash: string, git: SimpleGit ): Promise<{ validatedClasses: Set; warnings: string[] }> { - const { repoRoot, packageDirectories } = await getPackageDirectories(dxConfigFile); + const { repoRoot, packageDirectories } = await getPackageDirectories(git); await git.cwd(repoRoot); const warnings: string[] = [];