Skip to content

Commit

Permalink
fix: remove --sfdx-configuration flag and find sfdx-project.json
Browse files Browse the repository at this point in the history
…dynamically
  • Loading branch information
mcarvin8 committed Apr 23, 2024
1 parent 5e32938 commit db8c77a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 30 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand All @@ -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.
Expand Down Expand Up @@ -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 <value> -t <value> -e <value> -c <value> --output <value> [--json]
$ sf apex-tests-git-delta delta -f <value> -t <value> -e <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.
-c, --sfdx-configuration=<value> [default: sfdx-project.json] Path to your project's Salesforce DX configuration file.
--output=<value> [default: runTests.txt] The text file to save the delta test classes to.
GLOBAL FLAGS
Expand All @@ -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"
```
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" --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

Expand All @@ -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.
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 @@ -44,13 +44,6 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
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<TestDeltaResult> {
Expand All @@ -59,9 +52,8 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
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);
Expand Down
5 changes: 2 additions & 3 deletions src/service/extractTestClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleGitOptions> = {
baseDir: process.cwd(),
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions src/service/getPackageDirectories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions src/service/validateClassPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import { getPackageDirectories } from './getPackageDirectories.js';

export async function validateClassPaths(
unvalidatedClasses: string[],
dxConfigFile: string,
toCommitHash: string,
git: SimpleGit
): Promise<{ validatedClasses: Set<string>; warnings: string[] }> {
const { repoRoot, packageDirectories } = await getPackageDirectories(dxConfigFile);
const { repoRoot, packageDirectories } = await getPackageDirectories(git);
await git.cwd(repoRoot);
const warnings: string[] = [];

Expand Down

0 comments on commit db8c77a

Please sign in to comment.