diff --git a/README.md b/README.md index b76cdbe..1226139 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,11 @@ 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. -The tests are determined by looking at all commit messages in the commit range and extracting them with a regular expression defined in a text file. +The tests are determined by looking at all commit messages in the commit range and extracting them with a regular expression defined in a config file. -For example, if the user creates a file named `regex.txt` in their repository with the below regular expression, the plugin will extract all test classes that are found with this expression and return a space-separated string with unique test classes. +You must add a config file named `.apextestsgitdeltarc` in the root folder of your repository with your regular expression. + +For example, your `.apextestsgitdeltarc` file can contain the regular expression: ``` [Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx] @@ -33,7 +35,7 @@ You could then save the contents of this text file to a variable and use that va ``` sf apex-tests-git-delta delta --from "c7603c25581afe7c443c57e687f2d6abd654ea77" --to "HEAD" --output "runTests.txt" testclasses=$( -t -e --output [--json] + $ sf apex-tests-git-delta delta -f -t --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. --output= [default: runTests.txt] The text file to save the delta test classes to. GLOBAL FLAGS @@ -81,5 +82,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" --output "runTests.txt" + $ sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --output "runTests.txt" ``` diff --git a/messages/delta.md b/messages/delta.md index b797696..ba789a9 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" --output "runTests.txt"` +- `sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --output "runTests.txt"` # flags.from.summary @@ -18,10 +18,6 @@ Commit SHA from where the commit message log is done. This SHA's commit message Commit SHA to where the commit message log is done. -# flags.regular-expression.summary - -The text file containing the Apex Tests regular expression to search for. - # 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 5f29526..f3f99af 100644 --- a/src/commands/apex-tests-git-delta/delta.ts +++ b/src/commands/apex-tests-git-delta/delta.ts @@ -31,13 +31,6 @@ export default class ApexTestDelta extends SfCommand { summary: messages.getMessage('flags.from.summary'), required: true, }), - 'regular-expression': Flags.file({ - char: 'e', - summary: messages.getMessage('flags.regular-expression.summary'), - required: true, - exists: true, - default: 'regex.txt', - }), output: Flags.file({ summary: messages.getMessage('flags.output.summary'), required: true, @@ -50,10 +43,9 @@ export default class ApexTestDelta extends SfCommand { const { flags } = await this.parse(ApexTestDelta); const toGitRef = flags['to']; const fromGitRef = flags['from']; - const regExFile = flags['regular-expression']; const output = flags['output']; - const result = await extractTestClasses(fromGitRef, toGitRef, regExFile); + const result = await extractTestClasses(fromGitRef, toGitRef); 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 f98202e..62d7bf3 100644 --- a/src/service/extractTestClasses.ts +++ b/src/service/extractTestClasses.ts @@ -5,11 +5,10 @@ import { validateClassPaths } from './validateClassPaths.js'; export async function extractTestClasses( fromRef: string, - toRef: string, - regex: string + toRef: string ): Promise<{ validatedClasses: string; warnings: string[] }> { const testClasses: Set = new Set(); - const { repoRoot, matchedMessages } = await retrieveCommitMessages(fromRef, toRef, regex); + const { repoRoot, matchedMessages } = await retrieveCommitMessages(fromRef, toRef); matchedMessages.forEach((message: string) => { // Split the commit message by commas or spaces diff --git a/src/service/retrieveCommitMessages.ts b/src/service/retrieveCommitMessages.ts index 7553cc6..b4119ec 100644 --- a/src/service/retrieveCommitMessages.ts +++ b/src/service/retrieveCommitMessages.ts @@ -1,14 +1,14 @@ 'use strict'; import { promises as fsPromises, readFile, stat, readdir } from 'node:fs'; +import { resolve } from 'node:path'; import git from 'isomorphic-git'; import { getRepoRoot } from './getRepoRoot.js'; export async function retrieveCommitMessages( fromCommit: string, - toCommit: string, - regexFilePath: string + toCommit: string ): Promise<{ repoRoot: string; matchedMessages: string[] }> { const repoRoot = await getRepoRoot(); process.chdir(repoRoot); @@ -40,11 +40,14 @@ export async function retrieveCommitMessages( // Read and compile the regex from the specified file let regex: RegExp; - const regexPattern: string = (await fsPromises.readFile(regexFilePath, 'utf-8')).trim(); + const regexFilePath = resolve(repoRoot, '.apextestsgitdeltarc'); try { + const regexPattern: string = (await fsPromises.readFile(regexFilePath, 'utf-8')).trim(); regex = new RegExp(regexPattern, 'g'); } catch (err) { - throw Error(`The regular expression in '${regexFilePath}' is invalid.`); + throw Error( + `The regular expression in '${regexFilePath}' is invalid or the file wasn't found in the repo root folder.` + ); } // Filter messages that match the regex diff --git a/test/commands/delta/testConstants.ts b/test/commands/delta/testConstants.ts index 9af4841..12d9f42 100644 --- a/test/commands/delta/testConstants.ts +++ b/test/commands/delta/testConstants.ts @@ -1,6 +1,6 @@ 'use strict'; -export const regExFile: string = 'regex.txt'; +export const regExFile: string = '.apextestsgitdeltarc'; export const regExFileContents: string = '[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]'; export const sfdxConfigFile = 'sfdx-project.json'; const sfdxConfigFileContents = {