-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathTestDiscovery.ts
266 lines (239 loc) · 9.37 KB
/
TestDiscovery.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
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import { SwiftPackage, TargetType } from "../SwiftPackage";
import { LSPTestItem } from "../sourcekit-lsp/lspExtensions";
import { reduceTestItemChildren } from "./TestUtils";
/** Test class definition */
export interface TestClass extends Omit<Omit<LSPTestItem, "location">, "children"> {
location: vscode.Location | undefined;
children: TestClass[];
}
/**
* Tag that denotes TestItems should be runnable in the VS Code UI.
* Test items that do not have this tag will not have the green "run test" triangle button.
*/
export const runnableTag = new vscode.TestTag("runnable");
/**
* Tags that should not be duplicated on TestItems when applying parent tags to children.
*/
const defaultTags = [runnableTag.id, "test-target", "XCTest", "swift-testing"];
/**
* Update Test Controller TestItems based off array of TestClasses.
*
* The function creates the TestTargets based off the test targets in the Swift
* Package
* @param testController Test controller
* @param swiftPackage A swift package containing test targets
* @param testClasses Array of test classes
*/
export function updateTestsFromClasses(
testController: vscode.TestController,
swiftPackage: SwiftPackage,
testItems: TestClass[]
) {
const targets = swiftPackage.getTargets(TargetType.test).map(target => {
const filteredItems = testItems.filter(
testItem =>
testItem.location && swiftPackage.getTarget(testItem.location.uri.fsPath) === target
);
return {
id: target.c99name,
label: target.name,
children: filteredItems,
location: undefined,
disabled: false,
style: "test-target",
tags: [],
} as TestClass;
});
updateTests(testController, targets);
}
/**
* Update Test Controller TestItems based off array of TestTargets
* @param testController Test controller
* @param testItems Array of TestClasses
* @param filterFile Filter test deletion just for tests in the one file
*/
export function updateTests(
testController: vscode.TestController,
testItems: TestClass[],
filterFile?: vscode.Uri
) {
const incomingTestsLookup = createIncomingTestLookup(testItems);
function removeOldTests(testItem: vscode.TestItem) {
testItem.children.forEach(child => removeOldTests(child));
// If the existing item isn't in the map
if (
!incomingTestsLookup.get(testItem.id) &&
(!filterFile || testItem.uri?.fsPath === filterFile.fsPath)
) {
const collection = testItem.parent ? testItem.parent.children : testController.items;
if (
testItem.children.size === 0 ||
testItemHasParameterizedTestResultChildren(testItem)
) {
collection.delete(testItem.id);
}
}
}
// Skip removing tests if the test explorer is empty
if (testController.items.size !== 0) {
testController.items.forEach(removeOldTests);
}
// Add/update the top level test items. upsertTestItem will descend the tree of children adding them as well.
testItems.forEach(testItem => {
upsertTestItem(testController, testItem);
});
}
/**
* Returns true if all children have no URI.
* This indicates the test item is parameterized and the children are the results.
*/
function testItemHasParameterizedTestResultChildren(testItem: vscode.TestItem) {
return (
testItem.children.size > 0 &&
reduceTestItemChildren(
testItem.children,
(acc, child) => acc || child.uri !== undefined,
false
) === false
);
}
/**
* Create a lookup of the incoming tests we can compare to the existing list of tests
* to produce a list of tests that are no longer present. If a filterFile is specified we
* scope this work to just the tests inside that file.
*/
function createIncomingTestLookup(
collection: TestClass[],
filterFile?: vscode.Uri
): Map<string, TestClass> {
const dictionary = new Map<string, TestClass>();
function traverse(testItem: TestClass) {
// If we are filtering based on tests being one file and this
// function isn't in the file then ignore
if (!filterFile || testItem.location?.uri.fsPath === filterFile.fsPath) {
dictionary.set(testItem.id, testItem);
testItem.children.forEach(item => traverse(item));
}
}
collection.forEach(item => traverse(item));
return dictionary;
}
/**
* Merges the TestItems recursively from the `existingItem` in to the `newItem`
*/
function deepMergeTestItemChildren(existingItem: vscode.TestItem, newItem: vscode.TestItem) {
reduceTestItemChildren(
existingItem.children,
(collection, testItem: vscode.TestItem) => {
const existing = collection.get(testItem.id);
if (existing) {
deepMergeTestItemChildren(existing, testItem);
}
collection.add(testItem);
return collection;
},
newItem.children
);
}
/**
* Given a `TestClass` adds the TestClasses tags to each of its children.
* Does not apply recursively.
* @param testClass A test class whose tags should be propagated to its children.
* @returns A `TestClass` whose children include the parent's tags.
*/
function applyTagsToChildren(testClass: TestClass): TestClass {
const tagsToAdd = testClass.tags.filter(tag => !defaultTags.includes(tag.id));
return {
...testClass,
children: testClass.children.map(child => ({
...child,
tags: [...child.tags, ...tagsToAdd],
})),
};
}
/**
* Updates the existing `vscode.TestItem` if it exists with the same ID as the `TestClass`,
* otherwise creates an add a new one. The location on the returned vscode.TestItem is always updated.
*/
export function upsertTestItem(
testController: vscode.TestController,
testItem: TestClass,
parent?: vscode.TestItem
): vscode.TestItem {
const collection = parent?.children ?? testController.items;
const existingItem = collection.get(testItem.id);
let newItem: vscode.TestItem;
// Unfortunately TestItem.uri is readonly so if the location of the test has changed
// we need to create a new TestItem. If the location of the new test item is undefined
// then don't create a new item and use the old one.
if (
existingItem === undefined ||
(existingItem && testItem.location?.uri && existingItem.uri !== testItem.location.uri)
) {
newItem = testController.createTestItem(
testItem.id,
testItem.label,
testItem.location?.uri
);
// We want to keep existing children if they exist.
if (existingItem) {
const existingChildren: vscode.TestItem[] = [];
existingItem.children.forEach(child => {
existingChildren.push(child);
});
newItem.children.replace(existingChildren);
}
} else {
newItem = existingItem;
}
// At this point all the test items that should have been deleted are out of the tree.
// Its possible we're dropping a whole branch of test items on top of an existing one,
// and we want to merge these branches instead of the new one replacing the existing one.
if (existingItem) {
deepMergeTestItemChildren(existingItem, newItem);
}
// In VS Code tags are not inherited automatically, so if we're recieving a suite we need
// to set a suites tag on all of its children. Because test items are added top down the children
// aren't updated recursively all at once, but rather one level at a time which then propagages
// parent tags down the tree as children are upserted.
testItem = applyTagsToChildren(testItem);
const hasTestStyleTag = testItem.tags.find(tag => tag.id === testItem.style);
// Manually add the test style as a tag if it isn't already in the tags list.
// This lets the user filter by test type.
newItem.tags = hasTestStyleTag
? [...testItem.tags]
: [{ id: testItem.style }, ...testItem.tags];
if (testItem.disabled === false) {
newItem.tags = [...newItem.tags, runnableTag];
}
newItem.label = testItem.label;
newItem.range = testItem.location?.range;
if (testItem.sortText) {
newItem.sortText = testItem.sortText;
} else if (!testItem.location) {
// TestItems without a location should be sorted to the top.
const zeros = ``.padStart(8, "0");
newItem.sortText = `${zeros}:${testItem.label}`;
}
// Performs an upsert based on whether a test item exists in the collection with the same id.
// If no parent is provided operate on the testController's root items.
collection.add(newItem);
testItem.children.forEach(child => {
upsertTestItem(testController, child, newItem);
});
return newItem;
}