forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtestItemUtilities.ts
More file actions
621 lines (540 loc) · 20.6 KB
/
testItemUtilities.ts
File metadata and controls
621 lines (540 loc) · 20.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import {
TestItem,
Uri,
Range,
Position,
TestController,
TestRunResult,
TestResultState,
TestResultSnapshot,
TestItemCollection,
} from 'vscode';
import { CancellationToken } from 'vscode-jsonrpc';
import { asyncForEach } from '../../../common/utils/arrayUtils';
import { traceError, traceVerbose } from '../../../logging';
import {
RawDiscoveredTests,
RawTest,
RawTestFile,
RawTestFolder,
RawTestFunction,
RawTestSuite,
TestData,
TestDataKinds,
} from './types';
// Todo: Use `TestTag` when the proposed API gets into stable.
export const RunTestTag = { id: 'python-run' };
export const DebugTestTag = { id: 'python-debug' };
function testItemCollectionToArray(collection: TestItemCollection): TestItem[] {
const items: TestItem[] = [];
collection.forEach((c) => {
items.push(c);
});
return items;
}
export function removeItemByIdFromChildren(
idToRawData: Map<string, TestData>,
item: TestItem,
childNodeIdsToRemove: string[],
): void {
childNodeIdsToRemove.forEach((id) => {
item.children.delete(id);
idToRawData.delete(id);
});
}
export type ErrorTestItemOptions = { id: string; label: string; error: string };
export function createErrorTestItem(testController: TestController, options: ErrorTestItemOptions): TestItem {
const testItem = testController.createTestItem(options.id, options.label);
testItem.canResolveChildren = false;
testItem.error = options.error;
testItem.tags = [RunTestTag, DebugTestTag];
return testItem;
}
export function createWorkspaceRootTestItem(
testController: TestController,
idToRawData: Map<string, TestData>,
options: { id: string; label: string; uri: Uri; runId: string; parentId?: string; rawId?: string },
): TestItem {
const testItem = testController.createTestItem(options.id, options.label, options.uri);
testItem.canResolveChildren = true;
idToRawData.set(options.id, {
...options,
rawId: options.rawId ?? options.id,
kind: TestDataKinds.Workspace,
});
testItem.tags = [RunTestTag, DebugTestTag];
return testItem;
}
function getParentIdFromRawParentId(
idToRawData: Map<string, TestData>,
testRoot: string,
raw: { parentid: string },
): string | undefined {
const parent = idToRawData.get(path.join(testRoot, raw.parentid));
let parentId;
if (parent) {
parentId = parent.id === '.' ? testRoot : parent.id;
}
return parentId;
}
function getRangeFromRawSource(raw: { source: string }): Range | undefined {
// We have to extract the line number from the source data. If it is available it
// saves us from running symbol script or querying language server for this info.
try {
const sourceLine = raw.source.substr(raw.source.indexOf(':') + 1);
const line = Number.parseInt(sourceLine, 10);
// Lines in raw data start at 1, vscode lines start at 0
return new Range(new Position(line - 1, 0), new Position(line, 0));
} catch (ex) {
// ignore
}
return undefined;
}
export function getRunIdFromRawData(id: string): string {
// TODO: This is a temporary solution to normalize test ids.
// The current method is error prone and easy to break. When we
// re-write the test adapters we should make sure we consider this.
// This is the id that will be used to compare with the results.
const runId = id
.replace(/\.py[^\w\-]/g, '') // we want to get rid of the `.py` in file names
.replace(/[\\\:\/]/g, '.')
.replace(/\:\:/g, '.')
.replace(/\.\./g, '.');
return runId.startsWith('.') ? runId.substr(1) : runId;
}
function createFolderOrFileTestItem(
testController: TestController,
idToRawData: Map<string, TestData>,
testRoot: string,
rawData: RawTestFolder | RawTestFile,
): TestItem {
const fullPath = path.join(testRoot, rawData.relpath);
const uri = Uri.file(fullPath);
const parentId = getParentIdFromRawParentId(idToRawData, testRoot, rawData);
const label = path.basename(fullPath);
const testItem = testController.createTestItem(fullPath, label, uri);
testItem.canResolveChildren = true;
idToRawData.set(testItem.id, {
id: testItem.id,
rawId: rawData.id,
runId: rawData.relpath,
uri,
kind: TestDataKinds.FolderOrFile,
parentId,
});
testItem.tags = [RunTestTag, DebugTestTag];
return testItem;
}
function updateFolderOrFileTestItem(
item: TestItem,
idToRawData: Map<string, TestData>,
testRoot: string,
rawData: RawTestFolder | RawTestFile,
): void {
const fullPath = path.join(testRoot, rawData.relpath);
const uri = Uri.file(fullPath);
const parentId = getParentIdFromRawParentId(idToRawData, testRoot, rawData);
item.label = path.basename(fullPath);
item.canResolveChildren = true;
idToRawData.set(item.id, {
id: item.id,
rawId: rawData.id,
runId: rawData.relpath,
uri,
kind: TestDataKinds.FolderOrFile,
parentId,
});
item.tags = [RunTestTag, DebugTestTag];
}
function createCollectionTestItem(
testController: TestController,
idToRawData: Map<string, TestData>,
testRoot: string,
rawData: RawTestSuite | RawTestFunction,
): TestItem {
// id can look like test_something.py::SomeClass
const id = path.join(testRoot, rawData.id);
// We need the actual document path so we can set the location for the tests. This will be
// used to provide test result status next to the tests.
const documentPath = path.join(testRoot, rawData.id.substr(0, rawData.id.indexOf(':')));
const uri = Uri.file(documentPath);
const label = rawData.name;
const parentId = getParentIdFromRawParentId(idToRawData, testRoot, rawData);
const runId = getRunIdFromRawData(rawData.id);
const testItem = testController.createTestItem(id, label, uri);
testItem.canResolveChildren = true;
idToRawData.set(testItem.id, {
id: testItem.id,
rawId: rawData.id,
runId,
uri,
kind: TestDataKinds.Collection,
parentId,
});
testItem.tags = [RunTestTag, DebugTestTag];
return testItem;
}
function updateCollectionTestItem(
item: TestItem,
idToRawData: Map<string, TestData>,
testRoot: string,
rawData: RawTestSuite | RawTestFunction,
): void {
// We need the actual document path so we can set the location for the tests. This will be
// used to provide test result status next to the tests.
const documentPath = path.join(testRoot, rawData.id.substr(0, rawData.id.indexOf(':')));
const uri = Uri.file(documentPath);
item.label = rawData.name;
const parentId = getParentIdFromRawParentId(idToRawData, testRoot, rawData);
const runId = getRunIdFromRawData(rawData.id);
item.canResolveChildren = true;
idToRawData.set(item.id, {
id: item.id,
rawId: rawData.id,
runId,
uri,
kind: TestDataKinds.Collection,
parentId,
});
item.tags = [RunTestTag, DebugTestTag];
}
function createTestCaseItem(
testController: TestController,
idToRawData: Map<string, TestData>,
testRoot: string,
rawData: RawTest,
): TestItem {
// id can look like:
// test_something.py::SomeClass::someTest
// test_something.py::SomeClass::someTest[x1]
const id = path.join(testRoot, rawData.id);
// We need the actual document path so we can set the location for the tests. This will be
// used to provide test result status next to the tests.
const documentPath = path.join(testRoot, rawData.source.substr(0, rawData.source.indexOf(':')));
const uri = Uri.file(documentPath);
const label = rawData.name;
const parentId = getParentIdFromRawParentId(idToRawData, testRoot, rawData);
const runId = getRunIdFromRawData(rawData.id);
const testItem = testController.createTestItem(id, label, uri);
testItem.canResolveChildren = false;
testItem.range = getRangeFromRawSource(rawData);
idToRawData.set(testItem.id, {
id: testItem.id,
rawId: rawData.id,
runId,
uri,
kind: TestDataKinds.Case,
parentId,
});
testItem.tags = [RunTestTag, DebugTestTag];
return testItem;
}
function updateTestCaseItem(
item: TestItem,
idToRawData: Map<string, TestData>,
testRoot: string,
rawData: RawTest,
): void {
// We need the actual document path so we can set the location for the tests. This will be
// used to provide test result status next to the tests.
const documentPath = path.join(testRoot, rawData.source.substr(0, rawData.source.indexOf(':')));
const uri = Uri.file(documentPath);
item.label = rawData.name;
const parentId = getParentIdFromRawParentId(idToRawData, testRoot, rawData);
const runId = getRunIdFromRawData(rawData.id);
item.canResolveChildren = false;
item.range = getRangeFromRawSource(rawData);
idToRawData.set(item.id, {
id: item.id,
rawId: rawData.id,
runId,
uri,
kind: TestDataKinds.Case,
parentId,
});
item.tags = [RunTestTag, DebugTestTag];
}
async function updateTestItemFromRawDataInternal(
item: TestItem,
testController: TestController,
idToRawData: Map<string, TestData>,
testRoot: string,
rawDataSet: RawDiscoveredTests[],
token?: CancellationToken,
): Promise<void> {
if (token?.isCancellationRequested) {
return;
}
const rawId = idToRawData.get(item.id)?.rawId;
if (!rawId) {
traceError(`Unknown node id: ${item.id}`);
return;
}
const nodeRawData = rawDataSet.filter(
(r) =>
r.root === rawId ||
r.rootid === rawId ||
r.parents.find((p) => p.id === rawId) ||
r.tests.find((t) => t.id === rawId),
);
if (nodeRawData.length === 0 && item.parent) {
removeItemByIdFromChildren(idToRawData, item.parent, [item.id]);
traceVerbose(`Following test item was removed Reason: No-Raw-Data ${item.id}`);
return;
}
if (nodeRawData.length > 1) {
// Something is wrong, there can only be one test node with that id
traceError(`Multiple (${nodeRawData.length}) raw data nodes had the same id: ${rawId}`);
return;
}
if (rawId === nodeRawData[0].root || rawId === nodeRawData[0].rootid) {
// This is a test root node, we need to update the entire tree
// The update children and remove any child that does not have raw data.
await asyncForEach(testItemCollectionToArray(item.children), async (c) => {
await updateTestItemFromRawData(c, testController, idToRawData, testRoot, nodeRawData, token);
});
// Create child nodes that are new.
// We only need to look at rawData.parents. Since at this level we either have folder or file.
const rawChildNodes = nodeRawData[0].parents.filter((p) => p.parentid === '.' || p.parentid === rawId);
const existingNodes: string[] = [];
item.children.forEach((c) => existingNodes.push(idToRawData.get(c.id)?.rawId ?? ''));
await asyncForEach(
rawChildNodes.filter((r) => !existingNodes.includes(r.id)),
async (r) => {
const childItem =
r.kind === 'file'
? createFolderOrFileTestItem(testController, idToRawData, testRoot, r as RawTestFile)
: createFolderOrFileTestItem(testController, idToRawData, testRoot, r as RawTestFolder);
item.children.add(childItem);
await updateTestItemFromRawData(childItem, testController, idToRawData, testRoot, nodeRawData, token);
},
);
return;
}
// First check if this is a parent node
const rawData = nodeRawData[0].parents.filter((r) => r.id === rawId);
if (rawData.length === 1) {
// This is either a File/Folder/Collection node
// Update the node data
switch (rawData[0].kind) {
case 'file':
updateFolderOrFileTestItem(item, idToRawData, testRoot, rawData[0] as RawTestFile);
break;
case 'folder':
updateFolderOrFileTestItem(item, idToRawData, testRoot, rawData[0] as RawTestFolder);
break;
case 'suite':
updateCollectionTestItem(item, idToRawData, testRoot, rawData[0] as RawTestSuite);
break;
case 'function':
updateCollectionTestItem(item, idToRawData, testRoot, rawData[0] as RawTestFunction);
break;
default:
break;
}
// The update children and remove any child that does not have raw data.
await asyncForEach(testItemCollectionToArray(item.children), async (c) => {
await updateTestItemFromRawData(c, testController, idToRawData, testRoot, nodeRawData, token);
});
// Create child nodes that are new.
// Get the existing child node ids so we can skip them
const existingNodes: string[] = [];
item.children.forEach((c) => existingNodes.push(idToRawData.get(c.id)?.rawId ?? ''));
// We first look at rawData.parents. Since at this level we either have folder or file.
// The current node is potentially a parent of one of these "parent" nodes or it is a parent
// of test case nodes. We will handle Test case nodes after handling parents.
const rawChildNodes = nodeRawData[0].parents.filter((p) => p.parentid === rawId);
await asyncForEach(
rawChildNodes.filter((r) => !existingNodes.includes(r.id)),
async (r) => {
let childItem;
switch (r.kind) {
case 'file':
childItem = createFolderOrFileTestItem(testController, idToRawData, testRoot, r as RawTestFile);
break;
case 'folder':
childItem = createFolderOrFileTestItem(
testController,
idToRawData,
testRoot,
r as RawTestFolder,
);
break;
case 'suite':
childItem = createCollectionTestItem(testController, idToRawData, testRoot, r as RawTestSuite);
break;
case 'function':
childItem = createCollectionTestItem(
testController,
idToRawData,
testRoot,
r as RawTestFunction,
);
break;
default:
break;
}
if (childItem) {
item.children.add(childItem);
// This node can potentially have children. So treat it like a new node and update it.
await updateTestItemFromRawData(
childItem,
testController,
idToRawData,
testRoot,
nodeRawData,
token,
);
}
},
);
// Now we will look at test case nodes. Create any test case node that does not already exist.
const rawTestCaseNodes = nodeRawData[0].tests.filter((p) => p.parentid === rawId);
rawTestCaseNodes
.filter((r) => !existingNodes.includes(r.id))
.forEach((r) => {
const childItem = createTestCaseItem(testController, idToRawData, testRoot, r);
item.children.add(childItem);
});
return;
}
if (rawData.length > 1) {
// Something is wrong, there can only be one test node with that id
traceError(`Multiple (${rawData.length}) raw data nodes had the same id: ${rawId}`);
return;
}
// We are here this means rawData.length === 0
// The node is probably is test case node. Try and find it.
const rawCaseData = nodeRawData[0].tests.filter((r) => r.id === rawId);
if (rawCaseData.length === 1) {
// This is a test case node
updateTestCaseItem(item, idToRawData, testRoot, rawCaseData[0]);
return;
}
if (rawCaseData.length > 1) {
// Something is wrong, there can only be one test node with that id
traceError(`Multiple (${rawCaseData.length}) raw data nodes had the same id: ${rawId}`);
}
}
export async function updateTestItemFromRawData(
item: TestItem,
testController: TestController,
idToRawData: Map<string, TestData>,
testRoot: string,
rawDataSet: RawDiscoveredTests[],
token?: CancellationToken,
): Promise<void> {
item.busy = true;
await updateTestItemFromRawDataInternal(item, testController, idToRawData, testRoot, rawDataSet, token);
item.busy = false;
}
/**
* Expands an exclude set to include all descendants of excluded items.
* After expansion, checking if a node is excluded is O(1) - just check set membership.
*/
export function expandExcludeSet(excludeSet: Set<TestItem> | undefined): Set<TestItem> | undefined {
if (!excludeSet || excludeSet.size === 0) {
return excludeSet;
}
const expanded = new Set<TestItem>();
excludeSet.forEach((item) => {
addWithDescendants(item, expanded);
});
return expanded;
}
function addWithDescendants(item: TestItem, set: Set<TestItem>): void {
if (set.has(item)) {
return;
}
set.add(item);
item.children.forEach((child) => addWithDescendants(child, set));
}
export function getTestCaseNodes(
testNode: TestItem,
collection: TestItem[] = [],
visited?: Set<TestItem>,
excludeSet?: Set<TestItem>,
): TestItem[] {
if (visited?.has(testNode)) {
return collection;
}
visited?.add(testNode);
// Skip excluded nodes (excludeSet should be pre-expanded to include descendants)
if (excludeSet?.has(testNode)) {
return collection;
}
if (!testNode.canResolveChildren && testNode.tags.length > 0) {
collection.push(testNode);
}
testNode.children.forEach((c) => {
if (testNode.canResolveChildren) {
getTestCaseNodes(c, collection, visited, excludeSet);
} else {
collection.push(testNode);
}
});
return collection;
}
export function getWorkspaceNode(testNode: TestItem, idToRawData: Map<string, TestData>): TestItem | undefined {
const raw = idToRawData.get(testNode.id);
if (raw) {
if (raw.kind === TestDataKinds.Workspace) {
return testNode;
}
if (testNode.parent) {
return getWorkspaceNode(testNode.parent, idToRawData);
}
}
return undefined;
}
export function getNodeByUri(root: TestItem, uri: Uri): TestItem | undefined {
if (root.uri?.fsPath === uri.fsPath) {
return root;
}
const nodes: TestItem[] = [];
root.children.forEach((c) => nodes.push(c));
// Search at the current level
for (const node of nodes) {
if (node.uri?.fsPath === uri.fsPath) {
return node;
}
}
// Search the children of the current level
for (const node of nodes) {
const found = getNodeByUri(node, uri);
if (found) {
return found;
}
}
return undefined;
}
function updateTestResultMapForSnapshot(resultMap: Map<string, TestResultState>, snapshot: TestResultSnapshot) {
for (const taskState of snapshot.taskStates) {
resultMap.set(snapshot.id, taskState.state);
}
snapshot.children.forEach((child) => updateTestResultMapForSnapshot(resultMap, child));
}
export function updateTestResultMap(
resultMap: Map<string, TestResultState>,
testResults: readonly TestRunResult[],
): void {
const ordered = new Array(...testResults).sort((a, b) => a.completedAt - b.completedAt);
ordered.forEach((testResult) => {
testResult.results.forEach((snapshot) => updateTestResultMapForSnapshot(resultMap, snapshot));
});
}
export function checkForFailedTests(resultMap: Map<string, TestResultState>): boolean {
return (
Array.from(resultMap.values()).find(
(state) => state === TestResultState.Failed || state === TestResultState.Errored,
) !== undefined
);
}
export function clearAllChildren(testNode: TestItem): void {
const ids: string[] = [];
testNode.children.forEach((c) => ids.push(c.id));
ids.forEach(testNode.children.delete);
}