Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new validate flag): run tests only for source changes #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/sfp-cli/messages/validate.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"orgInfoFlagDescription": "Display info about the org that is used for validation",
"installDepsFlagDescription":"Install dependencies during fast feedback",
"disableSourcePackageOverride": "Disables overriding unlocked package installation as source package installation during validate",
"disableParallelTestingFlagDescription": "Disable test execution in parallel, this will execute apex tests in serial"

"disableParallelTestingFlagDescription": "Disable test execution in parallel, this will execute apex tests in serial",
"runTestAgainstSourceFlagDescription": "Enable impact testing, this will only run tests for the components that are impacted by the source changes"
}
3 changes: 2 additions & 1 deletion packages/sfp-cli/messages/validateAgainstOrg.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"disableSourcePackageOverride": "Disables overriding unlocked package installation as source package installation during validate",
"disableParallelTestingFlagDescription": "Disable test execution in parallel, this will execute apex tests in serial",
"installDepsFlagDescription":"Install dependencies during fast feedback",
"orgInfoFlagDescription": "Display info about the org that is used for validation"
"orgInfoFlagDescription": "Display info about the org that is used for validation",
"runTestAgainstSourceFlagDescription": "Enable impact testing, this will only run tests for the components that are impacted by the source changes"
}
10 changes: 8 additions & 2 deletions packages/sfp-cli/src/commands/orchestrator/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ export default class Validate extends SfpCommand {
description: messages.getMessage('disableArtifactUpdateFlagDescription'),
default: false,
}),
runtestagainstsource: Flags.boolean({
description: messages.getMessage('runTestAgainstSourceFlagDescription'),
default: false,
dependsOn: ['basebranch'],
}),
logsgroupsymbol,
loglevel
};
Expand Down Expand Up @@ -129,7 +134,7 @@ export default class Validate extends SfpCommand {
SFPLogger.log(
COLOR_HEADER(`Dependency Validation: ${this.flags.enabledependencyvalidation ? 'true' : 'false'}`)
);


SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO);

Expand Down Expand Up @@ -157,6 +162,7 @@ export default class Validate extends SfpCommand {
disableSourcePackageOverride : this.flags.disablesourcepkgoverride,
disableParallelTestExecution: this.flags.disableparalleltesting,
installExternalDependencies: this.flags.installdeps,
runTestOnlyAgainstSource: this.flags.runtestagainstsource,
};

setReleaseConfigForReleaseBasedModes(this.flags.releaseconfig,validateProps);
Expand Down Expand Up @@ -218,4 +224,4 @@ export default class Validate extends SfpCommand {
}
}
}
}
}
12 changes: 9 additions & 3 deletions packages/sfp-cli/src/commands/orchestrator/validateAgainstOrg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export default class ValidateAgainstOrg extends SfpCommand {
description: messages.getMessage('disableParallelTestingFlagDescription'),
default: false,
}),
runtestagainstsource: Flags.boolean({
description: messages.getMessage('runTestAgainstSourceFlagDescription'),
default: false,
dependsOn: ['basebranch'],
}),
loglevel
};

Expand Down Expand Up @@ -93,11 +98,11 @@ export default class ValidateAgainstOrg extends SfpCommand {
if (this.flags.mode != ValidationMode.FAST_FEEDBACK) {
SFPLogger.log(COLOR_HEADER(`Coverage Percentage: ${this.flags.coveragepercent}`));
}


SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO);


let validateResult: ValidateResult;
try {
let validateProps: ValidateProps = {
Expand All @@ -117,6 +122,7 @@ export default class ValidateAgainstOrg extends SfpCommand {
disableParallelTestExecution: this.flags.disableparalleltesting,
orgInfo: this.flags.orginfo,
installExternalDependencies: this.flags.installdeps,
runTestOnlyAgainstSource: this.flags.runtestagainstsource
};


Expand All @@ -132,7 +138,7 @@ export default class ValidateAgainstOrg extends SfpCommand {
} catch (error) {
if (error instanceof ValidateError) {
validateResult = error.data;
}
}

SFPStatsSender.logCount('validate.failed', tags);

Expand Down
35 changes: 30 additions & 5 deletions packages/sfp-cli/src/impl/validate/ValidateImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { mapInstalledArtifactstoPkgAndCommits } from "../../utils/FetchArtifacts
import { ApexTestValidator } from "./ApexTestValidator";
import OrgInfoDisplayer from "../../ui/OrgInfoDisplayer";
import FileOutputHandler from "../../outputs/FileOutputHandler";
import Package from '../../commands/impact/package';


export enum ValidateAgainst {
Expand Down Expand Up @@ -86,6 +87,8 @@ export interface ValidateProps {
orgInfo?: boolean;
disableSourcePackageOverride?: boolean;
disableParallelTestExecution?: boolean;
runTestOnlyAgainstSource?: boolean;
impactPackagesAgainstSource?: string[];
}

export default class ValidateImpl implements PostDeployHook, PreDeployHook {
Expand All @@ -100,6 +103,7 @@ export default class ValidateImpl implements PostDeployHook, PreDeployHook {

let deploymentResult: DeploymentResult;
let scratchOrgUsername: string;

try {

if (this.props.validateAgainst === ValidateAgainst.PROVIDED_ORG) {
Expand Down Expand Up @@ -152,6 +156,16 @@ export default class ValidateImpl implements PostDeployHook, PreDeployHook {
let builtSfpPackages = await this.buildChangedSourcePackages(
packagesInstalledInOrgMappedToCommits,
);
//fetch only changes against the source branch when flag is activated
if(this.props.runTestOnlyAgainstSource){
SFPLogger.log(
`👆 Running testclasses only against changes from source branch. Fetching packages...`,
LoggerLevel.INFO,
new ConsoleLogger(),
);
this.props.impactPackagesAgainstSource = await Package.run(['--basebranch',this.props.baseBranch]);
}

deploymentResult = await this.deploySourcePackages(scratchOrgUsername);

if (deploymentResult.failed.length > 0 || deploymentResult.error)
Expand Down Expand Up @@ -381,15 +395,15 @@ export default class ValidateImpl implements PostDeployHook, PreDeployHook {

FileOutputHandler.getInstance().appendOutput(`validation-error.md`,`### 💣 Deployment Failed 💣`);
let firstPackageFailedToValdiate = deploymentResult.failed[0];
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,`Package validation failed for **${firstPackageFailedToValdiate.sfpPackage.packageName}** due to`);
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,"");
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,deploymentResult.error);
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,`Package validation failed for **${firstPackageFailedToValdiate.sfpPackage.packageName}** due to`);
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,"");
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,deploymentResult.error);

FileOutputHandler.getInstance().appendOutput(`validation-error.md`,`Package that are not validated:`);
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,`Package that are not validated:`);
deploymentResult.failed.map(
(packageInfo, index) => {
if (index!=0)
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,`**${packageInfo.sfpPackage.packageName}**`);
FileOutputHandler.getInstance().appendOutput(`validation-error.md`,`**${packageInfo.sfpPackage.packageName}**`);
}
);
}
Expand Down Expand Up @@ -712,6 +726,17 @@ export default class ValidateImpl implements PostDeployHook, PreDeployHook {
deployedPackages?: SfpPackage[],
devhubUserName?: string,
): Promise<{ isToFailDeployment: boolean; message?: string }> {
//Run tests only against source changes
if(this.props.runTestOnlyAgainstSource && !this.props.impactPackagesAgainstSource?.includes(sfpPackage.packageName)){
SFPLogger.log(
COLOR_TRACE(
`Package ${sfpPackage.packageName} contains no source changes. Skip running tests`,
),
LoggerLevel.TRACE,
this.logger,
);
return { isToFailDeployment: false };
}
//Trigger Tests after installation of each package
if (sfpPackage.packageType && sfpPackage.packageType != PackageType.Data) {
if (
Expand Down
Loading