Skip to content

Commit

Permalink
feat: introduce 3 Apex Test Suite commands (#3435)
Browse files Browse the repository at this point in the history
@W-9632080@
@W-9632072@
@W-9632048@
  • Loading branch information
AnanyaJha authored Jul 23, 2021
1 parent 3e3b954 commit 99d3632
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 5 deletions.
26 changes: 25 additions & 1 deletion packages/salesforcedx-vscode-apex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"Programming Languages"
],
"dependencies": {
"@salesforce/apex-node": "0.2.2",
"@salesforce/apex-node": "0.2.3",
"@salesforce/apex-tmlanguage": "1.4.0",
"@salesforce/core": "2.23.2",
"@salesforce/salesforcedx-utils-vscode": "52.6.0",
Expand Down Expand Up @@ -156,6 +156,18 @@
"command": "sfdx.force.apex.test.run",
"when": "sfdx:project_opened"
},
{
"command": "sfdx.force.apex.test.suite.run",
"when": "sfdx:project_opened"
},
{
"command": "sfdx.force.apex.test.suite.create",
"when": "sfdx:project_opened"
},
{
"command": "sfdx.force.apex.test.suite.add",
"when": "sfdx:project_opened"
},
{
"command": "sfdx.force.test.view.runClassTests",
"when": "false"
Expand Down Expand Up @@ -199,6 +211,18 @@
"command": "sfdx.force.apex.test.run",
"title": "%force_apex_test_run_text%"
},
{
"command": "sfdx.force.apex.test.suite.run",
"title": "%force_apex_test_suite_run_text%"
},
{
"command": "sfdx.force.apex.test.suite.create",
"title": "%force_apex_test_suite_create_text%"
},
{
"command": "sfdx.force.apex.test.suite.add",
"title": "%force_apex_test_suite_build_text%"
},
{
"command": "sfdx.force.test.view.run",
"title": "%run_tests_title%",
Expand Down
5 changes: 4 additions & 1 deletion packages/salesforcedx-vscode-apex/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
"force_apex_test_last_method_run_text": "SFDX: Re-Run Last Run Apex Test Method",
"force_apex_test_class_run_text": "SFDX: Run Apex Test Class",
"force_apex_test_method_run_text": "SFDX: Run Apex Test Method",
"force_apex_test_run_text": "SFDX: Run Apex Tests"
"force_apex_test_run_text": "SFDX: Run Apex Tests",
"force_apex_test_suite_run_text": "SFDX: Run Apex Test Suite",
"force_apex_test_suite_create_text": "SFDX: Create Apex Test Suite",
"force_apex_test_suite_build_text": "SFDX: Add Tests to Apex Test Suite"
}
204 changes: 204 additions & 0 deletions packages/salesforcedx-vscode-apex/src/commands/forceApexTestSuite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* 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 { TestService } from '@salesforce/apex-node';
import {
LibraryCommandletExecutor,
SfdxCommandlet,
SfdxWorkspaceChecker
} from '@salesforce/salesforcedx-utils-vscode/out/src';
import {
CancelResponse,
ContinueResponse,
ParametersGatherer
} from '@salesforce/salesforcedx-utils-vscode/out/src/types';
import { readFileSync } from 'fs';
import { basename } from 'path';
import * as vscode from 'vscode';
import { OUTPUT_CHANNEL } from '../channels';
import { workspaceContext } from '../context';
import { nls } from '../messages';
import {
ApexLibraryTestRunExecutor,
ApexTestQuickPickItem,
TestType
} from './forceApexTestRun';

export type ApexTestSuiteOptions = { suitename: string; tests: string[] };

async function listApexClassItems(): Promise<ApexTestQuickPickItem[]> {
const apexClasses = await vscode.workspace.findFiles('**/*.cls');
const apexClassItems: ApexTestQuickPickItem[] = [];

apexClasses.forEach(apexClass => {
const fileContent = readFileSync(apexClass.fsPath).toString();
if (fileContent && fileContent.toLowerCase().includes('@istest')) {
apexClassItems.push({
label: basename(apexClass.toString()).replace('.cls', ''),
description: apexClass.fsPath,
type: TestType.Class
});
}
});

return apexClassItems;
}

async function listApexTestSuiteItems(): Promise<ApexTestQuickPickItem[]> {
const connection = await workspaceContext.getConnection();
const testService = new TestService(connection);
const testSuites = await testService.retrieveAllSuites();

const quickPickItems = testSuites.map(testSuite => {
return {
label: testSuite.TestSuiteName,
// @ts-ignore
description: testSuite.Id,
type: TestType.Suite
};
});
return quickPickItems;
}

export class TestSuiteSelector
implements ParametersGatherer<ApexTestQuickPickItem> {
public async gather(): Promise<
CancelResponse | ContinueResponse<ApexTestQuickPickItem>
> {
const quickPickItems = await listApexTestSuiteItems();

const testSuiteName = (await vscode.window.showQuickPick(
quickPickItems
)) as ApexTestQuickPickItem;

return testSuiteName
? { type: 'CONTINUE', data: testSuiteName }
: { type: 'CANCEL' };
}
}

export class TestSuiteBuilder
implements ParametersGatherer<ApexTestSuiteOptions> {
public async gather(): Promise<
CancelResponse | ContinueResponse<ApexTestSuiteOptions>
> {
const quickPickItems = await listApexTestSuiteItems();

const testSuiteName = (await vscode.window.showQuickPick(
quickPickItems
)) as ApexTestQuickPickItem;

if (testSuiteName) {
const apexClassItems = await listApexClassItems();

const apexClassSelection = (await vscode.window.showQuickPick(
apexClassItems,
{ canPickMany: true }
)) as ApexTestQuickPickItem[];
const apexClassNames = apexClassSelection?.map(
selection => selection.label
);

return apexClassSelection
? {
type: 'CONTINUE',
data: { suitename: testSuiteName.label, tests: apexClassNames }
}
: { type: 'CANCEL' };
}
return { type: 'CANCEL' };
}
}

export class TestSuiteCreator
implements ParametersGatherer<ApexTestSuiteOptions> {
public async gather(): Promise<
CancelResponse | ContinueResponse<ApexTestSuiteOptions>
> {
const testSuiteInput = {
prompt: 'Enter desired Apex test suite name:'
} as vscode.InputBoxOptions;
const testSuiteName = await vscode.window.showInputBox(testSuiteInput);

if (testSuiteName) {
const apexClassItems = await listApexClassItems();

const apexClassSelection = (await vscode.window.showQuickPick(
apexClassItems,
{ canPickMany: true }
)) as ApexTestQuickPickItem[];
const apexClassNames = apexClassSelection?.map(
selection => selection.label
);

return apexClassSelection
? {
type: 'CONTINUE',
data: { suitename: testSuiteName, tests: apexClassNames }
}
: { type: 'CANCEL' };
}
return { type: 'CANCEL' };
}
}

export class ApexLibraryTestSuiteBuilder extends LibraryCommandletExecutor<
ApexTestSuiteOptions
> {
public static diagnostics = vscode.languages.createDiagnosticCollection(
'apex-errors'
);

constructor() {
super(
nls.localize('force_apex_test_suite_build_text'),
'force_apex_test_suite_build_library',
OUTPUT_CHANNEL
);
}

public async run(
response: ContinueResponse<ApexTestSuiteOptions>
): Promise<boolean> {
const connection = await workspaceContext.getConnection();
const testService = new TestService(connection);
await testService.buildSuite(response.data.suitename, response.data.tests);
return true;
}
}

const workspaceChecker = new SfdxWorkspaceChecker();
const testSuiteSelector = new TestSuiteSelector();
const testSuiteCreator = new TestSuiteCreator();
const testSuiteBuilder = new TestSuiteBuilder();

export async function forceApexTestSuiteAdd() {
const commandlet = new SfdxCommandlet(
workspaceChecker,
testSuiteBuilder,
new ApexLibraryTestSuiteBuilder()
);
await commandlet.run();
}

export async function forceApexTestSuiteCreate() {
const commandlet = new SfdxCommandlet(
workspaceChecker,
testSuiteCreator,
new ApexLibraryTestSuiteBuilder()
);
await commandlet.run();
}

export async function forceApexTestSuiteRun() {
const commandlet = new SfdxCommandlet(
workspaceChecker,
testSuiteSelector,
new ApexLibraryTestRunExecutor()
);
await commandlet.run();
}
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-apex/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export {
export { forceApexLogGet } from './forceApexLogGet';
export { forceApexTestRun } from './forceApexTestRun';
export { forceApexExecute } from './forceApexExecute';
export { forceApexTestSuiteAdd, forceApexTestSuiteCreate, forceApexTestSuiteRun } from './forceApexTestSuite';
22 changes: 20 additions & 2 deletions packages/salesforcedx-vscode-apex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
forceApexTestClassRunCodeActionDelegate,
forceApexTestMethodRunCodeAction,
forceApexTestMethodRunCodeActionDelegate,
forceApexTestRun
forceApexTestRun,
forceApexTestSuiteAdd,
forceApexTestSuiteCreate,
forceApexTestSuiteRun
} from './commands';
import { APEX_EXTENSION_NAME, LSP_ERR } from './constants';
import { workspaceContext } from './context';
Expand Down Expand Up @@ -182,6 +185,18 @@ function registerCommands(
'sfdx.force.apex.test.method.run',
forceApexTestMethodRunCodeAction
);
const forceApexTestSuiteCreateCmd = vscode.commands.registerCommand(
'sfdx.force.apex.test.suite.create',
forceApexTestSuiteCreate
);
const forceApexTestSuiteRunCmd = vscode.commands.registerCommand(
'sfdx.force.apex.test.suite.run',
forceApexTestSuiteRun
);
const forceApexTestSuiteAddCmd = vscode.commands.registerCommand(
'sfdx.force.apex.test.suite.add',
forceApexTestSuiteAdd
);
const forceApexTestRunCmd = vscode.commands.registerCommand(
'sfdx.force.apex.test.run',
forceApexTestRun
Expand Down Expand Up @@ -209,7 +224,10 @@ function registerCommands(
forceApexTestMethodRunCmd,
forceApexTestMethodRunDelegateCmd,
forceApexTestRunCmd,
forceApexToggleColorizerCmd
forceApexToggleColorizerCmd,
forceApexTestSuiteCreateCmd,
forceApexTestSuiteRunCmd,
forceApexTestSuiteAddCmd
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/salesforcedx-vscode-apex/src/messages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ export const messages = {
source_missing_text:
'%s points to a missing folder. For information on how to setup the Salesforce Apex extension, see [Set Your Java Version](%s).',
wrong_java_version_text:
'An unsupported Java version was detected. Download and install [Java 8](https://java.com/en/download/) or [Java 11](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html) to run the extensions. For more information, see [Set Your Java Version](%s).'
'An unsupported Java version was detected. Download and install [Java 8](https://java.com/en/download/) or [Java 11](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html) to run the extensions. For more information, see [Set Your Java Version](%s).',
force_apex_test_suite_build_text: 'SFDX: Build Apex Test Suite'
};
Loading

0 comments on commit 99d3632

Please sign in to comment.