-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.ts
242 lines (221 loc) · 8.5 KB
/
test.ts
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2020, 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 { CancellationTokenSource, TestLevel, TestResult, TestRunIdResult, TestService } from '@salesforce/apex-node';
import {
arrayWithDeprecation,
Flags,
loglevel,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
SfCommand,
Ux,
} from '@salesforce/sf-plugins-core';
import { Messages, SfError } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { RunResult, TestReporter } from '../../../reporters/index.js';
import { codeCoverageFlag, resultFormatFlag } from '../../../flags.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-apex', 'runtest');
export const TestLevelValues = ['RunLocalTests', 'RunAllTestsInOrg', 'RunSpecifiedTests'];
export type RunCommandResult = RunResult | TestRunIdResult;
const exclusiveTestSpecifiers = ['class-names', 'suite-names', 'tests'];
export default class Test extends SfCommand<RunCommandResult> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:apex:test:run'];
public static readonly flags = {
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
'code-coverage': codeCoverageFlag,
'output-dir': Flags.directory({
aliases: ['outputdir', 'output-directory'],
deprecateAliases: true,
char: 'd',
summary: messages.getMessage('flags.output-dir.summary'),
}),
'test-level': Flags.string({
deprecateAliases: true,
aliases: ['testlevel'],
char: 'l',
summary: messages.getMessage('flags.test-level.summary'),
description: messages.getMessage('flags.test-level.description'),
options: TestLevelValues,
}),
'class-names': arrayWithDeprecation({
deprecateAliases: true,
aliases: ['classnames'],
char: 'n',
summary: messages.getMessage('flags.class-names.summary'),
description: messages.getMessage('flags.class-names.description'),
exclusive: exclusiveTestSpecifiers.filter((specifier) => specifier !== 'class-names'),
}),
'result-format': resultFormatFlag,
'suite-names': arrayWithDeprecation({
deprecateAliases: true,
aliases: ['suitenames'],
char: 's',
summary: messages.getMessage('flags.suite-names.summary'),
description: messages.getMessage('flags.suite-names.description'),
exclusive: exclusiveTestSpecifiers.filter((specifier) => specifier !== 'suite-names'),
}),
tests: arrayWithDeprecation({
char: 't',
summary: messages.getMessage('flags.tests.summary'),
description: messages.getMessage('flags.tests.description'),
exclusive: exclusiveTestSpecifiers.filter((specifier) => specifier !== 'tests'),
}),
// we want to pass `undefined` to the API
// eslint-disable-next-line sf-plugin/flag-min-max-default
wait: Flags.duration({
unit: 'minutes',
char: 'w',
summary: messages.getMessage('flags.wait.summary'),
min: 0,
}),
synchronous: Flags.boolean({
char: 'y',
summary: messages.getMessage('flags.synchronous.summary'),
}),
'detailed-coverage': Flags.boolean({
deprecateAliases: true,
aliases: ['detailedcoverage'],
char: 'v',
summary: messages.getMessage('flags.detailed-coverage.summary'),
dependsOn: ['code-coverage'],
}),
concise: Flags.boolean({
summary: messages.getMessage('flags.concise.summary'),
}),
};
protected cancellationTokenSource = new CancellationTokenSource();
public async run(): Promise<RunCommandResult> {
const { flags } = await this.parse(Test);
const testLevel = await validateFlags(
flags['class-names'],
flags['suite-names'],
flags.tests,
flags.synchronous,
flags['test-level'] as TestLevel
);
// graceful shutdown
const exitHandler = async (): Promise<void> => {
await this.cancellationTokenSource.asyncCancel();
process.exit();
};
// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on('SIGINT', exitHandler);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on('SIGTERM', exitHandler);
const conn = flags['target-org'].getConnection(flags['api-version']);
const testService = new TestService(conn);
// NOTE: This is a *bug*. Synchronous test runs should throw an error when multiple test classes are specified
// This was re-introduced due to https://github.com/forcedotcom/salesforcedx-vscode/issues/3154
// Address with W-9163533
const result =
flags.synchronous && testLevel === TestLevel.RunSpecifiedTests
? await this.runTest(testService, flags, testLevel)
: await this.runTestAsynchronous(testService, flags, testLevel);
if (this.cancellationTokenSource.token.isCancellationRequested) {
throw new SfError('Cancelled');
}
if ('summary' in result) {
const testReporter = new TestReporter(new Ux({ jsonEnabled: this.jsonEnabled() }), conn);
return testReporter.report(result, flags);
} else {
// Tests were ran asynchronously or the --wait timed out.
// Log the proper 'apex get test' command for the user to run later
this.log(messages.getMessage('runTestReportCommand', [this.config.bin, result.testRunId, conn.getUsername()]));
this.info(messages.getMessage('runTestSyncInstructions'));
if (flags['output-dir']) {
// testService writes a file with just the test run id in it to test-run-id.txt
// github.com/forcedotcom/salesforcedx-apex/blob/c986abfabee3edf12f396f1d2e43720988fa3911/src/tests/testService.ts#L245-L246
await testService.writeResultFiles(result, { dirPath: flags['output-dir'] }, flags['code-coverage']);
}
return result;
}
}
private async runTest(
testService: TestService,
flags: {
tests?: string[];
'class-names'?: string[];
'code-coverage'?: boolean;
},
testLevel: TestLevel
): Promise<TestResult> {
const payload = {
...(await testService.buildSyncPayload(testLevel, flags.tests?.join(','), flags['class-names']?.join(','))),
skipCodeCoverage: !flags['code-coverage'],
};
return testService.runTestSynchronous(
payload,
flags['code-coverage'],
this.cancellationTokenSource.token
) as Promise<TestResult>;
}
private async runTestAsynchronous(
testService: TestService,
flags: {
tests?: string[];
'class-names'?: string[];
'suite-names'?: string[];
'code-coverage'?: boolean;
synchronous?: boolean;
'result-format'?: string;
json?: boolean;
wait?: Duration;
},
testLevel: TestLevel
): Promise<TestRunIdResult> {
const payload = {
...(await testService.buildAsyncPayload(
testLevel,
flags.tests?.join(','),
flags['class-names']?.join(','),
flags['suite-names']?.join(',')
)),
skipCodeCoverage: !flags['code-coverage'],
};
// cast as TestRunIdResult because we're building an async payload which will return an async result
return (await testService.runTestAsynchronous(
payload,
flags['code-coverage'],
flags.wait && flags.wait.minutes > 0 ? false : !(flags.synchronous && !this.jsonEnabled()),
undefined,
this.cancellationTokenSource.token,
flags.wait
)) as TestRunIdResult;
}
}
const validateFlags = async (
classNames?: string[],
suiteNames?: string[],
tests?: string[],
synchronous?: boolean,
testLevel?: TestLevel
): Promise<TestLevel> => {
if (synchronous && (Boolean(suiteNames) || (classNames?.length && classNames.length > 1))) {
return Promise.reject(new Error(messages.getMessage('syncClassErr')));
}
if (
(Boolean(tests) || Boolean(classNames) || suiteNames) &&
testLevel &&
testLevel.toString() !== 'RunSpecifiedTests'
) {
return Promise.reject(new Error(messages.getMessage('testLevelErr')));
}
if (testLevel) {
return testLevel;
}
if (Boolean(classNames) || Boolean(suiteNames) || tests) {
return TestLevel.RunSpecifiedTests;
}
return TestLevel.RunLocalTests;
};