forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresultResolver.ts
More file actions
339 lines (310 loc) · 16.1 KB
/
resultResolver.ts
File metadata and controls
339 lines (310 loc) · 16.1 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
CancellationToken,
TestController,
TestItem,
Uri,
TestMessage,
Location,
TestRun,
MarkdownString,
TestCoverageCount,
FileCoverage,
FileCoverageDetail,
StatementCoverage,
Range,
} from 'vscode';
import * as util from 'util';
import {
CoveragePayload,
DiscoveredTestPayload,
ExecutionTestPayload,
FileCoverageMetrics,
ITestResultResolver,
} from './types';
import { TestProvider } from '../../types';
import { traceError, traceVerbose } from '../../../logging';
import { Testing } from '../../../common/utils/localize';
import { clearAllChildren, createErrorTestItem, getTestCaseNodes } from './testItemUtilities';
import { sendTelemetryEvent } from '../../../telemetry';
import { EventName } from '../../../telemetry/constants';
import { splitLines } from '../../../common/stringUtils';
import { buildErrorNodeOptions, populateTestTree, splitTestNameWithRegex } from './utils';
export class PythonResultResolver implements ITestResultResolver {
testController: TestController;
testProvider: TestProvider;
public runIdToTestItem: Map<string, TestItem>;
public runIdToVSid: Map<string, string>;
public vsIdToRunId: Map<string, string>;
public subTestStats: Map<string, { passed: number; failed: number }> = new Map();
public detailedCoverageMap = new Map<string, FileCoverageDetail[]>();
constructor(testController: TestController, testProvider: TestProvider, private workspaceUri: Uri) {
this.testController = testController;
this.testProvider = testProvider;
this.runIdToTestItem = new Map<string, TestItem>();
this.runIdToVSid = new Map<string, string>();
this.vsIdToRunId = new Map<string, string>();
}
public resolveDiscovery(payload: DiscoveredTestPayload, token?: CancellationToken): void {
if (!payload) {
// No test data is available
} else {
this._resolveDiscovery(payload as DiscoveredTestPayload, token);
}
}
public _resolveDiscovery(payload: DiscoveredTestPayload, token?: CancellationToken): void {
const workspacePath = this.workspaceUri.fsPath;
const rawTestData = payload as DiscoveredTestPayload;
// Check if there were any errors in the discovery process.
if (rawTestData.status === 'error') {
const testingErrorConst =
this.testProvider === 'pytest' ? Testing.errorPytestDiscovery : Testing.errorUnittestDiscovery;
const { error } = rawTestData;
traceError(testingErrorConst, 'for workspace: ', workspacePath, '\r\n', error?.join('\r\n\r\n') ?? '');
let errorNode = this.testController.items.get(`DiscoveryError:${workspacePath}`);
const message = util.format(
`${testingErrorConst} ${Testing.seePythonOutput}\r\n`,
error?.join('\r\n\r\n') ?? '',
);
if (errorNode === undefined) {
const options = buildErrorNodeOptions(this.workspaceUri, message, this.testProvider);
errorNode = createErrorTestItem(this.testController, options);
this.testController.items.add(errorNode);
}
const errorNodeLabel: MarkdownString = new MarkdownString(
`[Show output](command:python.viewOutput) to view error logs`,
);
errorNodeLabel.isTrusted = true;
errorNode.error = errorNodeLabel;
} else {
// remove error node only if no errors exist.
this.testController.items.delete(`DiscoveryError:${workspacePath}`);
}
if (rawTestData.tests || rawTestData.tests === null) {
// if any tests exist, they should be populated in the test tree, regardless of whether there were errors or not.
// parse and insert test data.
// If the test root for this folder exists: Workspace refresh, update its children.
// Otherwise, it is a freshly discovered workspace, and we need to create a new test root and populate the test tree.
populateTestTree(this.testController, rawTestData.tests, undefined, this, token);
}
sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, {
tool: this.testProvider,
failed: false,
});
}
public resolveExecution(payload: ExecutionTestPayload | CoveragePayload, runInstance: TestRun): void {
if ('coverage' in payload) {
// coverage data is sent once per connection
traceVerbose('Coverage data received.');
this._resolveCoverage(payload as CoveragePayload, runInstance);
} else {
this._resolveExecution(payload as ExecutionTestPayload, runInstance);
}
}
public _resolveCoverage(payload: CoveragePayload, runInstance: TestRun): void {
if (payload.result === undefined) {
return;
}
for (const [key, value] of Object.entries(payload.result)) {
const fileNameStr = key;
const fileCoverageMetrics: FileCoverageMetrics = value;
const linesCovered = fileCoverageMetrics.lines_covered ? fileCoverageMetrics.lines_covered : []; // undefined if no lines covered
const linesMissed = fileCoverageMetrics.lines_missed ? fileCoverageMetrics.lines_missed : []; // undefined if no lines missed
const executedBranches = fileCoverageMetrics.executed_branches;
const totalBranches = fileCoverageMetrics.total_branches;
const lineCoverageCount = new TestCoverageCount(
linesCovered.length,
linesCovered.length + linesMissed.length,
);
let fileCoverage: FileCoverage;
const uri = Uri.file(fileNameStr);
if (totalBranches === -1) {
// branch coverage was not enabled and should not be displayed
fileCoverage = new FileCoverage(uri, lineCoverageCount);
} else {
const branchCoverageCount = new TestCoverageCount(executedBranches, totalBranches);
fileCoverage = new FileCoverage(uri, lineCoverageCount, branchCoverageCount);
}
runInstance.addCoverage(fileCoverage);
// create detailed coverage array for each file (only line coverage on detailed, not branch)
const detailedCoverageArray: FileCoverageDetail[] = [];
// go through all covered lines, create new StatementCoverage, and add to detailedCoverageArray
for (const line of linesCovered) {
// line is 1-indexed, so we need to subtract 1 to get the 0-indexed line number
// true value means line is covered
const statementCoverage = new StatementCoverage(
true,
new Range(line - 1, 0, line - 1, Number.MAX_SAFE_INTEGER),
);
detailedCoverageArray.push(statementCoverage);
}
for (const line of linesMissed) {
// line is 1-indexed, so we need to subtract 1 to get the 0-indexed line number
// false value means line is NOT covered
const statementCoverage = new StatementCoverage(
false,
new Range(line - 1, 0, line - 1, Number.MAX_SAFE_INTEGER),
);
detailedCoverageArray.push(statementCoverage);
}
this.detailedCoverageMap.set(uri.fsPath, detailedCoverageArray);
}
}
public _resolveExecution(payload: ExecutionTestPayload, runInstance: TestRun): void {
const rawTestExecData = payload as ExecutionTestPayload;
if (rawTestExecData !== undefined && rawTestExecData.result !== undefined) {
// Map which holds the subtest information for each test item.
// iterate through payload and update the UI accordingly.
for (const keyTemp of Object.keys(rawTestExecData.result)) {
const testCases: TestItem[] = [];
// grab leaf level test items
this.testController.items.forEach((i) => {
const tempArr: TestItem[] = getTestCaseNodes(i);
testCases.push(...tempArr);
});
const testItem = rawTestExecData.result[keyTemp];
if (testItem.outcome === 'error') {
const rawTraceback = testItem.traceback ?? '';
const traceback = splitLines(rawTraceback, {
trim: false,
removeEmptyEntries: true,
}).join('\r\n');
const text = `${testItem.test} failed with error: ${
testItem.message ?? testItem.outcome
}\r\n${traceback}`;
const message = new TestMessage(text);
const grabVSid = this.runIdToVSid.get(keyTemp);
// search through freshly built array of testItem to find the failed test and update UI.
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri) {
if (indiItem.range) {
message.location = new Location(indiItem.uri, indiItem.range);
}
runInstance.errored(indiItem, message);
}
}
});
} else if (testItem.outcome === 'failure' || testItem.outcome === 'passed-unexpected') {
const rawTraceback = testItem.traceback ?? '';
const traceback = splitLines(rawTraceback, {
trim: false,
removeEmptyEntries: true,
}).join('\r\n');
const text = `${testItem.test} failed: ${testItem.message ?? testItem.outcome}\r\n${traceback}`;
const message = new TestMessage(text);
// note that keyTemp is a runId for unittest library...
const grabVSid = this.runIdToVSid.get(keyTemp);
// search through freshly built array of testItem to find the failed test and update UI.
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri) {
if (indiItem.range) {
message.location = new Location(indiItem.uri, indiItem.range);
}
runInstance.failed(indiItem, message);
}
}
});
} else if (testItem.outcome === 'success' || testItem.outcome === 'expected-failure') {
const grabTestItem = this.runIdToTestItem.get(keyTemp);
const grabVSid = this.runIdToVSid.get(keyTemp);
if (grabTestItem !== undefined) {
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri) {
runInstance.passed(grabTestItem);
}
}
});
}
} else if (testItem.outcome === 'skipped') {
const grabTestItem = this.runIdToTestItem.get(keyTemp);
const grabVSid = this.runIdToVSid.get(keyTemp);
if (grabTestItem !== undefined) {
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri) {
runInstance.skipped(grabTestItem);
}
}
});
}
} else if (testItem.outcome === 'subtest-failure') {
// split on [] or () based on how the subtest is setup.
const [parentTestCaseId, subtestId] = splitTestNameWithRegex(keyTemp);
const parentTestItem = this.runIdToTestItem.get(parentTestCaseId);
const data = testItem;
// find the subtest's parent test item
if (parentTestItem) {
const subtestStats = this.subTestStats.get(parentTestCaseId);
if (subtestStats) {
subtestStats.failed += 1;
} else {
this.subTestStats.set(parentTestCaseId, {
failed: 1,
passed: 0,
});
// clear since subtest items don't persist between runs
clearAllChildren(parentTestItem);
}
const subTestItem = this.testController?.createTestItem(
subtestId,
subtestId,
parentTestItem.uri,
);
// create a new test item for the subtest
if (subTestItem) {
const traceback = data.traceback ?? '';
const text = `${data.subtest} failed: ${
testItem.message ?? testItem.outcome
}\r\n${traceback}`;
parentTestItem.children.add(subTestItem);
runInstance.started(subTestItem);
const message = new TestMessage(text);
if (parentTestItem.uri && parentTestItem.range) {
message.location = new Location(parentTestItem.uri, parentTestItem.range);
}
runInstance.failed(subTestItem, message);
} else {
throw new Error('Unable to create new child node for subtest');
}
} else {
throw new Error('Parent test item not found');
}
} else if (testItem.outcome === 'subtest-success') {
// split on [] or () based on how the subtest is setup.
const [parentTestCaseId, subtestId] = splitTestNameWithRegex(keyTemp);
const parentTestItem = this.runIdToTestItem.get(parentTestCaseId);
// find the subtest's parent test item
if (parentTestItem) {
const subtestStats = this.subTestStats.get(parentTestCaseId);
if (subtestStats) {
subtestStats.passed += 1;
} else {
this.subTestStats.set(parentTestCaseId, { failed: 0, passed: 1 });
// clear since subtest items don't persist between runs
clearAllChildren(parentTestItem);
}
const subTestItem = this.testController?.createTestItem(
subtestId,
subtestId,
parentTestItem.uri,
);
// create a new test item for the subtest
if (subTestItem) {
parentTestItem.children.add(subTestItem);
runInstance.started(subTestItem);
runInstance.passed(subTestItem);
} else {
throw new Error('Unable to create new child node for subtest');
}
} else {
throw new Error('Parent test item not found');
}
}
}
}
}
}