forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
testTreeViewProvider.ts
133 lines (117 loc) · 5.38 KB
/
testTreeViewProvider.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { Event, EventEmitter, Uri } from 'vscode';
import { IWorkspaceService } from '../../common/application/types';
import { IDisposable, IDisposableRegistry } from '../../common/types';
import { getChildren, getParent } from '../common/testUtils';
import { ITestCollectionStorageService, TestStatus } from '../common/types';
import { ITestDataItemResource, ITestTreeViewProvider, IUnitTestManagementService, TestDataItem, WorkspaceTestStatus } from '../types';
import { createTreeViewItemFrom, TestTreeItem } from './testTreeViewItem';
@injectable()
export class TestTreeViewProvider implements ITestTreeViewProvider, ITestDataItemResource, IDisposable {
public readonly onDidChangeTreeData: Event<TestDataItem | undefined>;
private _onDidChangeTreeData = new EventEmitter<TestDataItem | undefined>();
private testsAreBeingDiscovered: boolean = false;
private disposables: IDisposable[] = [];
constructor(
@inject(ITestCollectionStorageService) private testStore: ITestCollectionStorageService,
@inject(IUnitTestManagementService) private testService: IUnitTestManagementService,
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService,
@inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry
) {
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
disposableRegistry.push(this);
this.disposables.push(this.testService.onDidStatusChange(this.onTestStatusChanged, this));
this.testStore.onDidChange(e => this._onDidChangeTreeData.fire(e.data), this, this.disposables);
if (Array.isArray(workspace.workspaceFolders) && workspace.workspaceFolders.length > 0) {
this.refresh(workspace.workspaceFolders![0].uri);
}
}
/**
* We need a way to map a given TestDataItem to a Uri, so that other consumers (such
* as the commandHandler for the Test Explorer) have a way of accessing the Uri outside
* the purview off the TestTreeView.
*
* @param testData Test data item to map to a Uri
* @returns A Uri representing the workspace that the test data item exists within
*/
public getResource(_testData: Readonly<TestDataItem>): Uri {
return this.workspace.workspaceFolders![0].uri;
}
/**
* As the TreeViewProvider itself is getting disposed, ensure all registered listeners are disposed
* from our internal emitter.
*/
public dispose() {
this.disposables.forEach(d => d.dispose());
this._onDidChangeTreeData.dispose();
}
/**
* Get [TreeItem](#TreeItem) representation of the `element`
*
* @param element The element for which [TreeItem](#TreeItem) representation is asked for.
* @return [TreeItem](#TreeItem) representation of the element
*/
public async getTreeItem(element: TestDataItem): Promise<TestTreeItem> {
const resource = this.workspace.workspaceFolders![0].uri;
const parent = await this.getParent!(element);
return createTreeViewItemFrom(resource, element, parent);
}
/**
* Get the children of `element` or root if no element is passed.
*
* @param element The element from which the provider gets children. Can be `undefined`.
* @return Children of `element` or root if no element is passed.
*/
public getChildren(element?: TestDataItem): TestDataItem[] {
const resource = this.workspace.workspaceFolders![0].uri;
const tests = this.testStore.getTests(resource);
if (element === undefined) {
return tests && tests.testFolders ? tests.rootTestFolders : [];
}
return getChildren(element);
}
/**
* Optional method to return the parent of `element`.
* Return `null` or `undefined` if `element` is a child of root.
*
* **NOTE:** This method should be implemented in order to access [reveal](#TreeView.reveal) API.
*
* @param element The element for which the parent has to be returned.
* @return Parent of `element`.
*/
public async getParent?(element: TestDataItem): Promise<TestDataItem> {
const resource = this.workspace.workspaceFolders![0].uri;
const tests = this.testStore.getTests(resource)!;
return getParent(tests, element)!;
}
/**
* Refresh the view by rebuilding the model and signaling the tree view to update itself.
*
* @param resource The resource 'root' for this refresh to occur under.
*/
public refresh(resource: Uri): void {
const tests = this.testStore.getTests(resource);
if (tests && tests.testFolders) {
this._onDidChangeTreeData.fire();
}
}
/**
* Event handler for TestStatusChanged (coming from the IUnitTestManagementService).
* ThisThe TreeView needs to know when we begin discovery and when discovery completes.
*
* @param e The event payload containing context for the status change
*/
private onTestStatusChanged(e: WorkspaceTestStatus) {
if (e.status === TestStatus.Discovering) {
this.testsAreBeingDiscovered = true;
return;
}
if (this.testsAreBeingDiscovered) {
this.testsAreBeingDiscovered = false;
this.refresh(e.workspace);
}
}
}