-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathapexTestSuite.ts
More file actions
199 lines (179 loc) · 6.99 KB
/
Copy pathapexTestSuite.ts
File metadata and controls
199 lines (179 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (c) 2025, 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 { sfProjectPreconditionChecker } from '@salesforce/effect-ext-utils';
import * as vscode from 'vscode';
import { OUTPUT_CHANNEL } from '../channels';
import { getConnection } from '../coreExtensionUtils';
import { nls } from '../messages';
import { MessageKey } from '../messages/i18n';
import { getApexTestingRuntime } from '../services/extensionProvider';
import { discoverTests } from '../testDiscovery/testDiscovery';
import {
CancelResponse,
ContinueResponse,
LibraryCommandletExecutor,
ParametersGatherer,
SfCommandlet
} from '../utils/commandletHelpers';
import { ApexTestQuickPickItem } from '../utils/fileHelpers';
import { getFullClassName, isFlowTest } from '../utils/testUtils';
import { getTestController } from '../views/testController';
import { ApexLibraryTestRunExecutor } from './apexTestRun';
type ApexTestSuiteOptions = { suitename: string; tests: string[] };
const listApexClassItems = async (): Promise<ApexTestQuickPickItem[]> => {
const result = await getApexTestingRuntime().runPromise(discoverTests());
return result.classes
.filter(cls => !isFlowTest(cls))
.map(
(cls): ApexTestQuickPickItem => ({
label: cls.name,
description: cls.namespacePrefix ?? '',
type: 'Class',
fullClassName: getFullClassName(cls)
})
)
.toSorted((a, b): number => {
const byLabel = a.label.localeCompare(b.label);
return byLabel !== 0 ? byLabel : (a.fullClassName ?? '').localeCompare(b.fullClassName ?? '');
});
};
const listApexTestSuiteItems = async (): Promise<ApexTestQuickPickItem[]> => {
const connection = await getConnection();
const testService = new TestService(connection);
return (await testService.retrieveAllSuites()).map(testSuite => ({
label: testSuite.TestSuiteName,
description: testSuite.id,
type: 'Suite'
}));
};
class TestSuiteSelector implements ParametersGatherer<ApexTestQuickPickItem> {
public async gather(): Promise<CancelResponse | ContinueResponse<ApexTestQuickPickItem>> {
const quickPickItems = await listApexTestSuiteItems();
const testSuiteName = await vscode.window.showQuickPick<ApexTestQuickPickItem>(quickPickItems);
return testSuiteName ? { type: 'CONTINUE', data: testSuiteName } : { type: 'CANCEL' };
}
}
class TestSuiteBuilder implements ParametersGatherer<ApexTestSuiteOptions> {
public async gather(): Promise<CancelResponse | ContinueResponse<ApexTestSuiteOptions>> {
const quickPickItems = await listApexTestSuiteItems();
const testSuiteName = await vscode.window.showQuickPick<ApexTestQuickPickItem>(quickPickItems);
if (testSuiteName) {
const apexClassItems = await listApexClassItems();
const apexClassSelection = await vscode.window.showQuickPick<ApexTestQuickPickItem>(apexClassItems, {
canPickMany: true
});
if (!apexClassSelection || apexClassSelection.length === 0) {
return { type: 'CANCEL' };
}
const apexClassNames = apexClassSelection.map(
selection => selection.fullClassName ?? selection.label
);
return {
type: 'CONTINUE',
data: { suitename: testSuiteName.label, tests: apexClassNames }
};
}
return { type: 'CANCEL' };
}
}
class TestSuiteCreator implements ParametersGatherer<ApexTestSuiteOptions> {
public async gather(): Promise<CancelResponse | ContinueResponse<ApexTestSuiteOptions>> {
const testSuiteInput: vscode.InputBoxOptions = {
prompt: 'Enter desired Apex test suite name:'
};
const testSuiteName = await vscode.window.showInputBox(testSuiteInput);
if (testSuiteName) {
let apexClassItems: ApexTestQuickPickItem[];
try {
apexClassItems = await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: nls.localize('retrieving_tests_message'),
cancellable: true
},
async (_progress, token) => {
const items = await listApexClassItems();
if (token.isCancellationRequested) {
throw new vscode.CancellationError();
}
return items;
}
);
} catch (e) {
if (e instanceof vscode.CancellationError) {
return { type: 'CANCEL' };
}
throw e;
}
const apexClassSelection = await vscode.window.showQuickPick<ApexTestQuickPickItem>(apexClassItems, {
canPickMany: true
});
if (!apexClassSelection || apexClassSelection.length === 0) {
return { type: 'CANCEL' };
}
const apexClassNames = apexClassSelection.map(
selection => selection.fullClassName ?? selection.label
);
return {
type: 'CONTINUE',
data: { suitename: testSuiteName, tests: apexClassNames }
};
}
return { type: 'CANCEL' };
}
}
class ApexLibraryTestSuiteBuilder extends LibraryCommandletExecutor<ApexTestSuiteOptions> {
public static diagnostics = vscode.languages.createDiagnosticCollection('apex-testing-errors');
constructor(notificationMessageKey: MessageKey) {
super(nls.localize(notificationMessageKey), 'apex_test_suite_build_library', OUTPUT_CHANNEL);
}
public async run(response: ContinueResponse<ApexTestSuiteOptions>): Promise<boolean> {
const connection = await getConnection();
const testService = new TestService(connection);
await testService.buildSuite(response.data.suitename, response.data.tests);
return true;
}
}
export const apexTestSuiteAdd = async () => {
const commandlet = new SfCommandlet(
sfProjectPreconditionChecker,
new TestSuiteBuilder(),
new ApexLibraryTestSuiteBuilder('apex_test_suite_add_text')
);
const didRun = await commandlet.run();
if (didRun) {
// Clear all suite children so they re-query from org instead of using stale local files
const testController = getTestController();
testController.clearAllSuiteChildren();
// Refresh to update the tree with latest suite data from org
void testController.refresh();
}
};
export const apexTestSuiteCreate = async () => {
const commandlet = new SfCommandlet(
sfProjectPreconditionChecker,
new TestSuiteCreator(),
new ApexLibraryTestSuiteBuilder('apex_test_suite_create_text')
);
const didRun = await commandlet.run();
if (didRun) {
// Clear all suite children so they re-query from org instead of using stale local files
const testController = getTestController();
testController.clearAllSuiteChildren();
// Refresh to update the tree with the newly created suite
void testController.refresh();
}
};
export const apexTestSuiteRun = async () => {
const commandlet = new SfCommandlet(
sfProjectPreconditionChecker,
new TestSuiteSelector(),
new ApexLibraryTestRunExecutor()
);
await commandlet.run();
};