Skip to content

Commit

Permalink
feat: define a standard config file for the regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Aug 13, 2024
1 parent eede089 commit ea6d9e9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 28 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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=$(<runTests.txt)
sf project deploy start -x manifest/package.xml -l RunSpecifiedTests -t $testclasses
sf project deploy start -x package/package.xml -l RunSpecifiedTests -t $testclasses
```

**NOTE:** The test classes will only be added to the output if they are found in one of your package directories as listed in the `sfdx-project.json` in the `--to` commit's file-tree. If the test class name was not found in any package directory, a warning will be printed to the terminal. The plugin will not fail if no test classes are included in the final output. The output and text file will simply be empty if no delta test classes were found in any commit message or no test classes were validated against a package directory.
Expand Down Expand Up @@ -66,12 +68,11 @@ This command will determine the root folder of the repo and look for the `sfdx-p

```
USAGE
$ sf apex-tests-git-delta delta -f <value> -t <value> -e <value> --output <value> [--json]
$ sf apex-tests-git-delta delta -f <value> -t <value> --output <value> [--json]
FLAGS
-f, --from=<value> Commit SHA from where the commit message log is done. This SHA's commit message will not be included in the results.
-t, --to=<value> [default: HEAD] Commit SHA to where the commit message log is done.
-e, --regular-expression=<value> [default: regex.txt] The text file containing the Apex Tests regular expression to search for.
--output=<value> [default: runTests.txt] The text file to save the delta test classes to.
GLOBAL FLAGS
Expand All @@ -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"
```
6 changes: 1 addition & 5 deletions messages/delta.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
10 changes: 1 addition & 9 deletions src/commands/apex-tests-git-delta/delta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
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,
Expand All @@ -50,10 +43,9 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
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);
Expand Down
5 changes: 2 additions & 3 deletions src/service/extractTestClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = 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
Expand Down
11 changes: 7 additions & 4 deletions src/service/retrieveCommitMessages.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/commands/delta/testConstants.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down

0 comments on commit ea6d9e9

Please sign in to comment.