Skip to content

Commit

Permalink
fix: get regular expression from a command flag instead of a file
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Apr 5, 2024
1 parent 2091022 commit e3d7d40
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 42 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This plugin requires Git Bash to be installed in your environment.

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.

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.
For example, if the user provides the below regular expression via the `--regular-expression` flag, the plugin will extract all test classes that are found with this expression and return a space-separated string with unique test classes.

```
[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]
Expand Down Expand Up @@ -71,7 +71,7 @@ USAGE
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.
-e, --regular-expression=<value> [default: '[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]'] The regular expression to use when parsing commit messages for Apex Tests.
-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.
Expand All @@ -82,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" --sfdx-configuration "sfdx-project.json" --output "runTests.txt"
$ sf apex-tests-git-delta delta --from "c7603c255" --to "HEAD" --regular-expression "[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]" --sfdx-configuration "sfdx-project.json" --output "runTests.txt"
```
2 changes: 1 addition & 1 deletion messages/delta.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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.
The regular expression to use when parsing commit messages for Apex Tests.

# flags.sfdx-configuration.summary

Expand Down
5 changes: 2 additions & 3 deletions src/commands/apex-tests-git-delta/delta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
summary: messages.getMessage('flags.from.summary'),
required: true,
}),
'regular-expression': Flags.file({
'regular-expression': Flags.string({
char: 'e',
summary: messages.getMessage('flags.regular-expression.summary'),
required: true,
exists: true,
default: 'regex.txt',
default: '[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]',
}),
output: Flags.file({
summary: messages.getMessage('flags.output.summary'),
Expand Down
46 changes: 22 additions & 24 deletions src/service/retrieveCommitMessages.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
'use strict'
'use strict';
import { execSync } from 'node:child_process';
import * as fs from 'node:fs';

export function retrieveCommitMessages(fromCommit: string, toCommit: string, regexFilePath: string): string[] {
const gitLogCommand = `git log --format=%s ${fromCommit}..${toCommit}`;
let commitMessages: string;
try {
commitMessages = execSync(gitLogCommand, { encoding: 'utf-8' });
} catch (err) {
throw Error('The git diff failed to run due to the above error.');
}
export function retrieveCommitMessages(fromCommit: string, toCommit: string, regexPattern: string): string[] {
const gitLogCommand = `git log --format=%s ${fromCommit}..${toCommit}`;
let commitMessages: string;
try {
commitMessages = execSync(gitLogCommand, { encoding: 'utf-8' });
} catch (err) {
throw Error('The git diff failed to run due to the above error.');
}

let regexPattern = '';
try {
regexPattern = fs.readFileSync(regexFilePath, 'utf-8').trim();
} catch (err) {
throw Error(`The regular expression was unable to be extracted from ${regexFilePath}`);
}
let regex: RegExp;
try {
regex = new RegExp(regexPattern, 'g');
} catch (err) {
throw Error(`The regular expression '${regexPattern}' is invalid.`);
}

const regex = new RegExp(regexPattern, 'g');
const matchedMessages: string[] = [];
let match;
while ((match = regex.exec(commitMessages)) !== null) {
if (match[1]) {
matchedMessages.push(match[1]);
}
const matchedMessages: string[] = [];
let match;
while ((match = regex.exec(commitMessages)) !== null) {
if (match[1]) {
matchedMessages.push(match[1]);
}
}

return matchedMessages;
return matchedMessages;
}
5 changes: 2 additions & 3 deletions test/commands/delta/empty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import ApexTestDelta from '../../../src/commands/apex-tests-git-delta/delta.js';
import { createTemporaryCommit } from './createTemporaryCommit.js';
import { regExFile, regExFileContents, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';
import { regExPattern, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';

describe('scan commit messages without the regex and return an empty string.', () => {
const $$ = new TestContext();
Expand All @@ -24,7 +24,6 @@ describe('scan commit messages without the regex and return an empty string.', (
fs.mkdirSync('packaged/classes', { recursive: true });
execSync('git init', { cwd: tempDir });
execSync('git branch -m main');
fs.writeFileSync(regExFile, regExFileContents);
fs.writeFileSync(sfdxConfigFile, sfdxConfigJsonString);
let userName = '';
let userEmail = '';
Expand Down Expand Up @@ -63,7 +62,7 @@ describe('scan commit messages without the regex and return an empty string.', (
});

it('return an empty test string with no warnings.', async () => {
await ApexTestDelta.run(['--from', fromSha, '--to', toSha]);
await ApexTestDelta.run(['--from', fromSha, '--to', toSha, '-e', regExPattern]);
const output = sfCommandStubs.log
.getCalls()
.flatMap((c) => c.args)
Expand Down
3 changes: 1 addition & 2 deletions test/commands/delta/testConstants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

export const regExFile: string = 'regex.txt';
export const regExFileContents: string = '[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]';
export const regExPattern: string = '[Aa][Pp][Ee][Xx]::(.*?)::[Aa][Pp][Ee][Xx]';
export const sfdxConfigFile = 'sfdx-project.json';
const sfdxConfigFileContents = {
packageDirectories: [{ path: 'force-app', default: true }, { path: 'packaged' }],
Expand Down
5 changes: 2 additions & 3 deletions test/commands/delta/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import ApexTestDelta from '../../../src/commands/apex-tests-git-delta/delta.js';
import { createTemporaryCommit } from './createTemporaryCommit.js';
import { regExFile, regExFileContents, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';
import { regExPattern, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';

describe('return the delta tests between git commits', () => {
const $$ = new TestContext();
Expand All @@ -24,7 +24,6 @@ describe('return the delta tests between git commits', () => {
fs.mkdirSync('packaged/classes', { recursive: true });
execSync('git init', { cwd: tempDir });
execSync('git branch -m main');
fs.writeFileSync(regExFile, regExFileContents);
fs.writeFileSync(sfdxConfigFile, sfdxConfigJsonString);
let userName = '';
let userEmail = '';
Expand Down Expand Up @@ -71,7 +70,7 @@ describe('return the delta tests between git commits', () => {
});

it('scan the temporary commits and return the delta test class string without any warnings.', async () => {
await ApexTestDelta.run(['--from', fromSha, '--to', toSha]);
await ApexTestDelta.run(['--from', fromSha, '--to', toSha, '-e', regExPattern]);
const output = sfCommandStubs.log
.getCalls()
.flatMap((c) => c.args)
Expand Down
5 changes: 2 additions & 3 deletions test/commands/delta/warnings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import ApexTestDelta from '../../../src/commands/apex-tests-git-delta/delta.js';
import { createTemporaryCommit } from './createTemporaryCommit.js';
import { regExFile, regExFileContents, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';
import { regExPattern, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';

describe('confirm warnings are generated when files cannot be found in a package directory.', () => {
const $$ = new TestContext();
Expand All @@ -24,7 +24,6 @@ describe('confirm warnings are generated when files cannot be found in a package
fs.mkdirSync('packaged/classes', { recursive: true });
execSync('git init', { cwd: tempDir });
execSync('git branch -m main');
fs.writeFileSync(regExFile, regExFileContents);
fs.writeFileSync(sfdxConfigFile, sfdxConfigJsonString);
let userName = '';
let userEmail = '';
Expand Down Expand Up @@ -67,7 +66,7 @@ describe('confirm warnings are generated when files cannot be found in a package
});

it('confirm warnings are generated and no delta tests are in the log output.', async () => {
await ApexTestDelta.run(['--from', fromSha, '--to', toSha]);
await ApexTestDelta.run(['--from', fromSha, '--to', toSha, '-e', regExPattern]);
const warningsOutput = sfCommandStubs.warn
.getCalls()
.flatMap((c) => c.args)
Expand Down

0 comments on commit e3d7d40

Please sign in to comment.