-
Notifications
You must be signed in to change notification settings - Fork 405
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: run apex tests using the library #2828
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a1d1b0
feat: introduce library for sidebar and code action
AnanyaJha 15b4eef
feat: use library for command palette
AnanyaJha 9bc23b7
chore: add output and localize text
AnanyaJha 23c01eb
chore: add output and localize text pt2
AnanyaJha 8a7caa8
fix: use common notification service
AnanyaJha e3ef774
test: fix old tests and add new tests
AnanyaJha 4d317c3
test: testing windows issue
AnanyaJha 75b8038
test: fixing cov setting
AnanyaJha c17866a
chore: fix import
AnanyaJha 37501a4
chore: update to new version of library
AnanyaJha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,19 @@ | |
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { | ||
HumanReporter, | ||
TestItem, | ||
TestLevel, | ||
TestService | ||
} from '@salesforce/apex-node'; | ||
import { | ||
Command, | ||
SfdxCommandBuilder, | ||
TestRunner | ||
} from '@salesforce/salesforcedx-utils-vscode/out/src/cli'; | ||
import { notificationService } from '@salesforce/salesforcedx-utils-vscode/out/src/commands'; | ||
import { workspaceContext } from '@salesforce/salesforcedx-utils-vscode/out/src/context'; | ||
import * as vscode from 'vscode'; | ||
import { nls } from '../messages'; | ||
import { forceApexTestRunCacheService, isEmpty } from '../testRunCache'; | ||
|
@@ -22,22 +29,81 @@ const sfdxCoreSettings = sfdxCoreExports.sfdxCoreSettings; | |
const SfdxCommandlet = sfdxCoreExports.SfdxCommandlet; | ||
const SfdxWorkspaceChecker = sfdxCoreExports.SfdxWorkspaceChecker; | ||
const SfdxCommandletExecutor = sfdxCoreExports.SfdxCommandletExecutor; | ||
const notificationService = sfdxCoreExports.notificationService; | ||
const LibraryCommandletExecutor = sfdxCoreExports.LibraryCommandletExecutor; | ||
const channelService = sfdxCoreExports.channelService; | ||
|
||
export class ApexLibraryTestRunExecutor extends LibraryCommandletExecutor<{ | ||
outputDir: string; | ||
tests: string[]; | ||
codeCoverage: boolean; | ||
}> { | ||
private tests: string[]; | ||
private codeCoverage: boolean = false; | ||
private outputDir: string; | ||
protected executionName = nls.localize('apex_test_run_text'); | ||
protected logName = 'force_apex_execute_library'; | ||
|
||
public static diagnostics = vscode.languages.createDiagnosticCollection( | ||
'apex-errors' | ||
); | ||
|
||
constructor(tests: string[], outputDir: string, codeCoverage: boolean) { | ||
super(); | ||
this.tests = tests; | ||
this.outputDir = outputDir; | ||
this.codeCoverage = codeCoverage; | ||
} | ||
|
||
private buildTestItem(testNames: string[]): TestItem[] { | ||
const tItems = testNames.map(item => { | ||
if (item.indexOf('.') > 0) { | ||
const splitItemData = item.split('.'); | ||
return { | ||
className: splitItemData[0], | ||
testMethods: [splitItemData[1]] | ||
} as TestItem; | ||
} | ||
|
||
return { className: item } as TestItem; | ||
}); | ||
return tItems; | ||
} | ||
|
||
protected async run(): Promise<boolean> { | ||
const connection = await workspaceContext.getConnection(); | ||
const testService = new TestService(connection); | ||
const result = await testService.runTestAsynchronous( | ||
{ | ||
tests: this.buildTestItem(this.tests), | ||
testLevel: TestLevel.RunSpecifiedTests | ||
}, | ||
this.codeCoverage | ||
); | ||
await testService.writeResultFiles( | ||
result, | ||
{ resultFormat: 'json', dirPath: this.outputDir }, | ||
this.codeCoverage | ||
); | ||
const humanOutput = new HumanReporter().format(result, this.codeCoverage); | ||
channelService.appendLine(humanOutput); | ||
return true; | ||
} | ||
} | ||
|
||
// build force:apex:test:run w/ given test class or test method | ||
export class ForceApexTestRunCodeActionExecutor extends SfdxCommandletExecutor<{}> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reworked this executor a bit so that it could be used for the sidebar as well |
||
protected test: string; | ||
protected tests: string; | ||
protected shouldGetCodeCoverage: boolean = false; | ||
protected builder: SfdxCommandBuilder = new SfdxCommandBuilder(); | ||
private outputToJson: string; | ||
|
||
public constructor( | ||
test: string, | ||
tests: string[], | ||
shouldGetCodeCoverage: boolean, | ||
outputToJson: string | ||
) { | ||
super(); | ||
this.test = test || ''; | ||
this.tests = tests.join(',') || ''; | ||
this.shouldGetCodeCoverage = shouldGetCodeCoverage; | ||
this.outputToJson = outputToJson; | ||
} | ||
|
@@ -48,7 +114,7 @@ export class ForceApexTestRunCodeActionExecutor extends SfdxCommandletExecutor<{ | |
nls.localize('force_apex_test_run_codeAction_description_text') | ||
) | ||
.withArg('force:apex:test:run') | ||
.withFlag('--tests', this.test) | ||
.withFlag('--tests', this.tests) | ||
.withFlag('--resultformat', 'human') | ||
.withFlag('--outputdir', this.outputToJson) | ||
.withFlag('--loglevel', 'error') | ||
|
@@ -62,13 +128,20 @@ export class ForceApexTestRunCodeActionExecutor extends SfdxCommandletExecutor<{ | |
} | ||
} | ||
|
||
async function forceApexTestRunCodeAction(test: string) { | ||
const getCodeCoverage = sfdxCoreSettings.getRetrieveTestCodeCoverage(); | ||
async function forceApexTestRunCodeAction(tests: string[]) { | ||
const outputToJson = getTempFolder(); | ||
const getCodeCoverage = sfdxCoreSettings.getRetrieveTestCodeCoverage(); | ||
const testRunExecutor = sfdxCoreSettings.getApexLibrary() | ||
? new ApexLibraryTestRunExecutor(tests, outputToJson, getCodeCoverage) | ||
: new ForceApexTestRunCodeActionExecutor( | ||
tests, | ||
getCodeCoverage, | ||
outputToJson | ||
); | ||
const commandlet = new SfdxCommandlet( | ||
new SfdxWorkspaceChecker(), | ||
new EmptyParametersGatherer(), | ||
new ForceApexTestRunCodeActionExecutor(test, getCodeCoverage, outputToJson) | ||
testRunExecutor | ||
); | ||
await commandlet.run(); | ||
} | ||
|
@@ -122,7 +195,7 @@ export async function forceApexTestClassRunCodeAction(testClass: string) { | |
return; | ||
} | ||
|
||
await forceApexTestRunCodeAction(testClass); | ||
await forceApexTestRunCodeAction([testClass]); | ||
} | ||
|
||
// T E S T M E T H O D | ||
|
@@ -163,5 +236,5 @@ export async function forceApexTestMethodRunCodeAction(testMethod: string) { | |
return; | ||
} | ||
|
||
await forceApexTestRunCodeAction(testMethod); | ||
await forceApexTestRunCodeAction([testMethod]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the
LibraryCommandletExecutor
currently lives in the core package, it uses the defaultSalesforce CLI
channel from the channel services. If we switch to using the newchannelService
we have in the utils package right now, the experience is wonky because theStarting command.../Ending command...
text appears in the defaultSalesforce CLI
channel, while the output from the test run appears in the new channel that we've created. And since the command palette portion of thetest:run
functionality still lives in the core package as well, all the output from that command is displayed via theSalesforce CLI
channel tooIn a follow up PR, I think we should migrate the LibraryCommandletExecutor to the utils package, the Apex commands (test:run command) to the apex extension, and then switch this command to use the new channelService so it doesn't cause customers any confusion on which channel they should be looking at for apex test results
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is part of the work we'll be doing in January to address some perf issues in the Apex extension.