forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathworkspaceTestAdapter.ts
More file actions
181 lines (160 loc) · 7.53 KB
/
workspaceTestAdapter.ts
File metadata and controls
181 lines (160 loc) · 7.53 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as util from 'util';
import { CancellationToken, TestController, TestItem, TestRun, TestRunProfileKind, Uri } from 'vscode';
import { createDeferred, Deferred } from '../../common/utils/async';
import { Testing } from '../../common/utils/localize';
import { traceError } from '../../logging';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { TestProvider } from '../types';
import { createErrorTestItem, expandExcludeSet, getTestCaseNodes } from './common/testItemUtilities';
import { ITestDiscoveryAdapter, ITestExecutionAdapter, ITestResultResolver } from './common/types';
import { IPythonExecutionFactory } from '../../common/process/types';
import { ITestDebugLauncher } from '../common/types';
import { buildErrorNodeOptions } from './common/utils';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { ProjectAdapter } from './common/projectAdapter';
/**
* This class exposes a test-provider-agnostic way of discovering tests.
*
* It gets instantiated by the `PythonTestController` class in charge of reflecting test data in the UI,
* and then instantiates provider-specific adapters under the hood depending on settings.
*
* This class formats the JSON test data returned by the `[Unittest|Pytest]TestDiscoveryAdapter` into test UI elements,
* and uses them to insert/update/remove items in the `TestController` instance behind the testing UI whenever the `PythonTestController` requests a refresh.
*/
export class WorkspaceTestAdapter {
private discovering: Deferred<void> | undefined;
private executing: Deferred<void> | undefined;
constructor(
private testProvider: TestProvider,
private discoveryAdapter: ITestDiscoveryAdapter,
private executionAdapter: ITestExecutionAdapter,
private workspaceUri: Uri,
public resultResolver: ITestResultResolver,
) {}
public async executeTests(
testController: TestController,
runInstance: TestRun,
includes: TestItem[],
executionFactory: IPythonExecutionFactory,
token?: CancellationToken,
profileKind?: boolean | TestRunProfileKind,
debugLauncher?: ITestDebugLauncher,
interpreter?: PythonEnvironment,
excludes?: readonly TestItem[],
project?: ProjectAdapter,
): Promise<void> {
if (this.executing) {
traceError('Test execution already in progress, not starting a new one.');
return this.executing.promise;
}
const deferred = createDeferred<void>();
this.executing = deferred;
const testCaseNodes: TestItem[] = [];
const visitedNodes = new Set<TestItem>();
const rawExcludeSet = excludes?.length ? new Set(excludes) : undefined;
const excludeSet = expandExcludeSet(rawExcludeSet);
const testCaseIds: string[] = [];
try {
// Expand included items to leaf test nodes.
// getTestCaseNodes handles visited tracking and exclusion filtering.
includes.forEach((t) => {
getTestCaseNodes(t, testCaseNodes, visitedNodes, excludeSet);
});
// Collect runIDs for the test nodes to execute.
testCaseNodes.forEach((node) => {
runInstance.started(node);
const runId = this.resultResolver.vsIdToRunId.get(node.id);
if (runId) {
testCaseIds.push(runId);
}
});
if (executionFactory === undefined) {
throw new Error('Execution factory is required for test execution');
}
await this.executionAdapter.runTests(
this.workspaceUri,
testCaseIds,
profileKind,
runInstance,
executionFactory,
debugLauncher,
interpreter,
project,
);
deferred.resolve();
} catch (ex) {
// handle token and telemetry here
sendTelemetryEvent(EventName.UNITTEST_RUN_ALL_FAILED, undefined);
let cancel = token?.isCancellationRequested
? Testing.cancelUnittestExecution
: Testing.errorUnittestExecution;
if (this.testProvider === 'pytest') {
cancel = token?.isCancellationRequested ? Testing.cancelPytestExecution : Testing.errorPytestExecution;
}
traceError(`${cancel}\r\n`, ex);
// Also report on the test view
const message = util.format(`${cancel} ${Testing.seePythonOutput}\r\n`, ex);
const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider);
const errorNode = createErrorTestItem(testController, options);
testController.items.add(errorNode);
deferred.reject(ex as Error);
} finally {
this.executing = undefined;
}
return Promise.resolve();
}
public async discoverTests(
testController: TestController,
executionFactory: IPythonExecutionFactory,
token?: CancellationToken,
interpreter?: PythonEnvironment,
): Promise<void> {
sendTelemetryEvent(EventName.UNITTEST_DISCOVERING, undefined, { tool: this.testProvider });
// Discovery is expensive. If it is already running, use the existing promise.
if (this.discovering) {
traceError('Test discovery already in progress, not starting a new one.');
return this.discovering.promise;
}
const deferred = createDeferred<void>();
this.discovering = deferred;
try {
if (executionFactory === undefined) {
throw new Error('Execution factory is required for test discovery');
}
await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory, token, interpreter);
deferred.resolve();
} catch (ex) {
sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, { tool: this.testProvider, failed: true });
let cancel = token?.isCancellationRequested
? Testing.cancelUnittestDiscovery
: Testing.errorUnittestDiscovery;
if (this.testProvider === 'pytest') {
cancel = token?.isCancellationRequested ? Testing.cancelPytestDiscovery : Testing.errorPytestDiscovery;
}
traceError(`${cancel} for workspace: ${this.workspaceUri} \r\n`, ex);
// Report also on the test view.
const message = util.format(`${cancel} ${Testing.seePythonOutput}\r\n`, ex);
const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider);
const errorNode = createErrorTestItem(testController, options);
testController.items.add(errorNode);
return deferred.reject(ex as Error);
} finally {
// Discovery has finished running, we have the data,
// we don't need the deferred promise anymore.
this.discovering = undefined;
}
sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, { tool: this.testProvider, failed: false });
return Promise.resolve();
}
/**
* Retrieves the current test provider instance.
*
* @returns {TestProvider} The instance of the test provider.
*/
public getTestProvider(): TestProvider {
return this.testProvider;
}
}