forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestTreeViewProvider.ts
More file actions
211 lines (193 loc) · 8.62 KB
/
testTreeViewProvider.ts
File metadata and controls
211 lines (193 loc) · 8.62 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { Event, EventEmitter, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode';
import { ICommandManager, IWorkspaceService } from '../../common/application/types';
import { Commands, CommandSource } from '../../common/constants';
import { IDisposable, IDisposableRegistry } from '../../common/types';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { getChildren, getParent, getTestDataItemType } from '../common/testUtils';
import {
ITestCollectionStorageService,
ITestDataItemResource,
ITestManagementService,
ITestTreeViewProvider,
TestDataItem,
TestDataItemType,
Tests,
TestStatus,
TestWorkspaceFolder,
WorkspaceTestStatus,
} from '../common/types';
import { TestTreeItem } from './testTreeViewItem';
@injectable()
export class TestTreeViewProvider implements ITestTreeViewProvider, ITestDataItemResource, IDisposable {
public readonly onDidChangeTreeData: Event<TestDataItem | undefined>;
public readonly discovered = new Set<string>();
public readonly testsAreBeingDiscovered: Map<string, boolean>;
private _onDidChangeTreeData = new EventEmitter<TestDataItem | undefined>();
private disposables: IDisposable[] = [];
constructor(
@inject(ITestCollectionStorageService) private testStore: ITestCollectionStorageService,
@inject(ITestManagementService) private testService: ITestManagementService,
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry,
) {
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
disposableRegistry.push(this);
this.testsAreBeingDiscovered = new Map<string, boolean>();
this.disposables.push(this.testService.onDidStatusChange(this.onTestStatusChanged, this));
this.testStore.onDidChange((e) => this._onDidChangeTreeData.fire(e.data), this, this.disposables);
this.workspace.onDidChangeWorkspaceFolders(
() => this._onDidChangeTreeData.fire(undefined),
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 testData.resource;
}
/**
* 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<TreeItem> {
const defaultCollapsibleState = (await this.shouldElementBeExpandedByDefault(element))
? TreeItemCollapsibleState.Expanded
: undefined;
return new TestTreeItem(element.resource, element, defaultCollapsibleState);
}
/**
* 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 async getChildren(element?: TestDataItem): Promise<TestDataItem[]> {
if (element) {
if (element instanceof TestWorkspaceFolder) {
let tests = this.testStore.getTests(element.workspaceFolder.uri);
if (!tests && !this.discovered.has(element.workspaceFolder.uri.fsPath)) {
this.discovered.add(element.workspaceFolder.uri.fsPath);
await this.commandManager.executeCommand(
Commands.Tests_Discover,
element,
CommandSource.testExplorer,
undefined,
);
tests = this.testStore.getTests(element.workspaceFolder.uri);
}
return this.getRootNodes(tests);
}
return getChildren(element);
}
if (!Array.isArray(this.workspace.workspaceFolders) || this.workspace.workspaceFolders.length === 0) {
return [];
}
sendTelemetryEvent(EventName.UNITTEST_EXPLORER_WORK_SPACE_COUNT, undefined, {
count: this.workspace.workspaceFolders.length,
});
// If we are in a single workspace
if (this.workspace.workspaceFolders.length === 1) {
const tests = this.testStore.getTests(this.workspace.workspaceFolders[0].uri);
return this.getRootNodes(tests);
}
// If we are in a mult-root workspace, then nest the test data within a
// virtual node, represending the workspace folder.
return this.workspace.workspaceFolders.map((workspaceFolder) => new TestWorkspaceFolder(workspaceFolder));
}
/**
* 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 | undefined> {
if (element instanceof TestWorkspaceFolder) {
return;
}
const tests = this.testStore.getTests(element.resource);
return tests ? getParent(tests, element) : undefined;
}
/**
* If we have test files directly in root directory, return those.
* If we have test folders and no test files under the root directory, then just return the test directories.
* The goal is not avoid returning an empty root node, when all it contains are child nodes for folders.
*
* @param {Tests} [tests]
* @returns
* @memberof TestTreeViewProvider
*/
public getRootNodes(tests?: Tests) {
if (tests && tests.rootTestFolders && tests.rootTestFolders.length === 1) {
return [...tests.rootTestFolders[0].testFiles, ...tests.rootTestFolders[0].folders];
}
return tests ? tests.rootTestFolders : [];
}
/**
* 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 workspaceFolder = this.workspace.getWorkspaceFolder(resource);
if (!workspaceFolder) {
return;
}
const tests = this.testStore.getTests(resource);
if (tests && tests.testFolders) {
this._onDidChangeTreeData.fire(new TestWorkspaceFolder(workspaceFolder));
}
}
/**
* Event handler for TestStatusChanged (coming from the ITestManagementService).
* 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.set(e.workspace.fsPath, true);
return;
}
if (!this.testsAreBeingDiscovered.get(e.workspace.fsPath)) {
return;
}
this.testsAreBeingDiscovered.set(e.workspace.fsPath, false);
this.refresh(e.workspace);
}
private async shouldElementBeExpandedByDefault(element: TestDataItem) {
const parent = await this.getParent(element);
if (!parent || getTestDataItemType(parent) === TestDataItemType.workspaceFolder) {
return true;
}
return false;
}
}