Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

fix(namespace): fix namespaces on reported class names #1365

Merged
merged 3 commits into from
Jul 24, 2023
Merged
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
6 changes: 3 additions & 3 deletions decision records/validate/001-automated-apex-testing-retry.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ During validate stage, sfpowerscripts triggers apex testing for each package and
- For Source Package, each individual apex class have more than 75% of coverage or more
- For Unlocked Packages, each package has an overall coverage of more than 75% of coverage or more

These tests are by default triggered asynchronously (in parallel), with a request to calculate coverage as well. Most projects however find it really difficult to get all the test of package to execute synchronously. After confirming with Salesforce Product team, tests are always triggered synchronously during Package Validation (build) or during deployment (source packages). Executing tests synchronously is extremely consuming on packages with larger number of test classes.
These tests are by default triggered asynchronously (in parallel), with a request to calculate coverage as well. Most projects however find it really difficult to get all the test of package to execute synchronously. After confirming with Salesforce Product team, tests are always triggered synchronously during Package Validation (build) or during deployment (source packages). Executing tests synchronously is extremely time consuming on packages with larger number of test classes.

There are also situations where bulk of the apex test in a package can be executed asynchronously with a few test cases that need to be triggered in synchronous mode.

A recently surfaced issue (#836), have uncovered coverage calculation becoming erratic randomly. This is attributed to the fact that asynchronous tests may trigger parallel compilation of classes under test and code coverage calculation is skipped.

## Decision

sfpowerscripts will collect all the test classes that failed in an asynchronous run due to 'UNABLE_TO_LOCK_ROW' or 'Your request exceeded the time limit for processing' and trigger these tests in synchronous mode.
sfpowerscripts will collect all the test classes that failed in an asynchronous run due to 'UNABLE_TO_LOCK_ROW', 'Your request exceeded the time limit for processing' or an 'Internal Salesforce Error' and trigger these tests in synchronous mode.

sfpowerscripts will also figure out any tests classes that were not able to contribute to code coverage and execute them synchronously. As the current test api has limitation on how tests could be triggered synchronously (only one test class is allowed), sfpowerscripts will change the mode of the org to 'Disable Parallel Testing' by changing the apex setting as mentioned [ here](https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_apexsettings.htm). sfpowerscripts will utilize jsforce update (http://jsforce.github.io/jsforce/doc/Metadata.html) to update this setting on the fly.
Once the setting is succesfully toggled, it will proceed to execute these tests using asynchronous payload, which is equivalent to triggering test classes synchronously. The coverage results are then converged and new coverage value is calculated
Once the setting is successfully toggled, it will proceed to execute these tests using asynchronous payload, which is equivalent to triggering test classes synchronously. The coverage results are then converged and new coverage value is calculated

The retry is only attempted once and provided there is no other failures in the first run other than the issues mentioned above
38 changes: 33 additions & 5 deletions packages/core/src/apextest/TriggerApexTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export default class TriggerApexTests {
{ retries: 2, minTimeout: 3000 }
);

testResult = this.fixBadNamespaceClassFullNames(testResult);

//Collect Failed Tests only if Parallel
testResult = await this.triggerSecondRunInSerialForParallelFailedTests(
testResult,
Expand Down Expand Up @@ -334,7 +336,8 @@ export default class TriggerApexTests {
//Check for messages
if (
test.message.includes(`Your request exceeded the time limit for processing`) ||
test.message.includes(`UNABLE_TO_LOCK_ROW`)
test.message.includes(`UNABLE_TO_LOCK_ROW`) ||
test.message.includes(`Internal Salesforce Error`)
) {
if (!testToBeTriggered.includes(test.apexClass.fullName)) {
parallelFailedTestClasses.push(test.apexClass.fullName);
Expand Down Expand Up @@ -403,11 +406,15 @@ export default class TriggerApexTests {
{ retries: 2, minTimeout: 3000 }
);

secondRuntestRunResult = this.fixBadNamespaceClassFullNames(secondRuntestRunResult);

//Fetch Test Results
const secondTestResult = await testService.reportAsyncResults(
secondRuntestRunResult.summary.testRunId,
true,
this.cancellationTokenSource.token
const secondTestResult = this.fixBadNamespaceClassFullNames(
await testService.reportAsyncResults(
secondRuntestRunResult.summary.testRunId,
true,
this.cancellationTokenSource.token
)
);

this.writeTestOutput(secondTestResult);
Expand Down Expand Up @@ -447,6 +454,27 @@ export default class TriggerApexTests {
return modifiedTestResult;
}

private fixBadNamespaceClassFullNames(testResult: any): any {
let modifiedTestResult = _.cloneDeep(testResult);

modifiedTestResult.tests = modifiedTestResult.tests.map((test) => {
return {
...test,
...{
fullName: test.fullName.replace('__', '.'),
apexClass: {
...test.apexClass,
...{
fullName: test.apexClass.fullName.replace('__', '.'),
},
},
},
};
});

return modifiedTestResult;
}

private combineTestResult(testResult: TestResult, testResultSecondRun?: TestResult) {
testResult.summary.failing = 0;
testResult.summary.passing = 0;
Expand Down