Skip to content

Commit ea6d9e9

Browse files
committed
feat: define a standard config file for the regular expression
1 parent eede089 commit ea6d9e9

File tree

6 files changed

+19
-28
lines changed

6 files changed

+19
-28
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
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.
66

7-
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.
7+
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.
88

9-
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.
9+
You must add a config file named `.apextestsgitdeltarc` in the root folder of your repository with your regular expression.
10+
11+
For example, your `.apextestsgitdeltarc` file can contain the regular expression:
1012

1113
```
1214
[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
3335
```
3436
sf apex-tests-git-delta delta --from "c7603c25581afe7c443c57e687f2d6abd654ea77" --to "HEAD" --output "runTests.txt"
3537
testclasses=$(<runTests.txt)
36-
sf project deploy start -x manifest/package.xml -l RunSpecifiedTests -t $testclasses
38+
sf project deploy start -x package/package.xml -l RunSpecifiedTests -t $testclasses
3739
```
3840

3941
**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.
@@ -66,12 +68,11 @@ This command will determine the root folder of the repo and look for the `sfdx-p
6668

6769
```
6870
USAGE
69-
$ sf apex-tests-git-delta delta -f <value> -t <value> -e <value> --output <value> [--json]
71+
$ sf apex-tests-git-delta delta -f <value> -t <value> --output <value> [--json]
7072
7173
FLAGS
7274
-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.
7375
-t, --to=<value> [default: HEAD] Commit SHA to where the commit message log is done.
74-
-e, --regular-expression=<value> [default: regex.txt] The text file containing the Apex Tests regular expression to search for.
7576
--output=<value> [default: runTests.txt] The text file to save the delta test classes to.
7677
7778
GLOBAL FLAGS
@@ -81,5 +82,5 @@ DESCRIPTION
8182
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.
8283
8384
EXAMPLES
84-
$ sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --regular-expression "regex.txt" --output "runTests.txt"
85+
$ sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --output "runTests.txt"
8586
```

messages/delta.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Given 2 git commits, this plugin will parse all of the commit messages between t
88

99
# examples
1010

11-
- `sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --regular-expression "regex.txt" --output "runTests.txt"`
11+
- `sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --output "runTests.txt"`
1212

1313
# flags.from.summary
1414

@@ -18,10 +18,6 @@ Commit SHA from where the commit message log is done. This SHA's commit message
1818

1919
Commit SHA to where the commit message log is done.
2020

21-
# flags.regular-expression.summary
22-
23-
The text file containing the Apex Tests regular expression to search for.
24-
2521
# flags.output.summary
2622

2723
The text file to save the delta test classes to.

src/commands/apex-tests-git-delta/delta.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
3131
summary: messages.getMessage('flags.from.summary'),
3232
required: true,
3333
}),
34-
'regular-expression': Flags.file({
35-
char: 'e',
36-
summary: messages.getMessage('flags.regular-expression.summary'),
37-
required: true,
38-
exists: true,
39-
default: 'regex.txt',
40-
}),
4134
output: Flags.file({
4235
summary: messages.getMessage('flags.output.summary'),
4336
required: true,
@@ -50,10 +43,9 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
5043
const { flags } = await this.parse(ApexTestDelta);
5144
const toGitRef = flags['to'];
5245
const fromGitRef = flags['from'];
53-
const regExFile = flags['regular-expression'];
5446
const output = flags['output'];
5547

56-
const result = await extractTestClasses(fromGitRef, toGitRef, regExFile);
48+
const result = await extractTestClasses(fromGitRef, toGitRef);
5749
const tests = result.validatedClasses;
5850
const warnings = result.warnings;
5951
await writeFile(output, tests);

src/service/extractTestClasses.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import { validateClassPaths } from './validateClassPaths.js';
55

66
export async function extractTestClasses(
77
fromRef: string,
8-
toRef: string,
9-
regex: string
8+
toRef: string
109
): Promise<{ validatedClasses: string; warnings: string[] }> {
1110
const testClasses: Set<string> = new Set();
12-
const { repoRoot, matchedMessages } = await retrieveCommitMessages(fromRef, toRef, regex);
11+
const { repoRoot, matchedMessages } = await retrieveCommitMessages(fromRef, toRef);
1312

1413
matchedMessages.forEach((message: string) => {
1514
// Split the commit message by commas or spaces

src/service/retrieveCommitMessages.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22

33
import { promises as fsPromises, readFile, stat, readdir } from 'node:fs';
4+
import { resolve } from 'node:path';
45
import git from 'isomorphic-git';
56

67
import { getRepoRoot } from './getRepoRoot.js';
78

89
export async function retrieveCommitMessages(
910
fromCommit: string,
10-
toCommit: string,
11-
regexFilePath: string
11+
toCommit: string
1212
): Promise<{ repoRoot: string; matchedMessages: string[] }> {
1313
const repoRoot = await getRepoRoot();
1414
process.chdir(repoRoot);
@@ -40,11 +40,14 @@ export async function retrieveCommitMessages(
4040

4141
// Read and compile the regex from the specified file
4242
let regex: RegExp;
43-
const regexPattern: string = (await fsPromises.readFile(regexFilePath, 'utf-8')).trim();
43+
const regexFilePath = resolve(repoRoot, '.apextestsgitdeltarc');
4444
try {
45+
const regexPattern: string = (await fsPromises.readFile(regexFilePath, 'utf-8')).trim();
4546
regex = new RegExp(regexPattern, 'g');
4647
} catch (err) {
47-
throw Error(`The regular expression in '${regexFilePath}' is invalid.`);
48+
throw Error(
49+
`The regular expression in '${regexFilePath}' is invalid or the file wasn't found in the repo root folder.`
50+
);
4851
}
4952

5053
// Filter messages that match the regex

test/commands/delta/testConstants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
export const regExFile: string = 'regex.txt';
3+
export const regExFile: string = '.apextestsgitdeltarc';
44
export const regExFileContents: string = '[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]';
55
export const sfdxConfigFile = 'sfdx-project.json';
66
const sfdxConfigFileContents = {

0 commit comments

Comments
 (0)