Skip to content

Commit 5c7c06b

Browse files
committed
Only configure test explorer once in tests
1 parent 023feac commit 5c7c06b

File tree

2 files changed

+65
-73
lines changed

2 files changed

+65
-73
lines changed

test/integration-tests/testexplorer/TestExplorerIntegration.test.ts

Lines changed: 55 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import * as assert from "assert";
1616
import * as vscode from "vscode";
17-
import { beforeEach } from "mocha";
17+
import { beforeEach, afterEach } from "mocha";
1818
import { testAssetUri } from "../../fixtures";
1919
import { TestExplorer } from "../../../src/TestExplorer/TestExplorer";
2020
import {
@@ -24,7 +24,6 @@ import {
2424
eventPromise,
2525
gatherTests,
2626
runTest,
27-
testExplorerFor,
2827
waitForTestExplorerReady,
2928
} from "./utilities";
3029
import { WorkspaceContext } from "../../../src/WorkspaceContext";
@@ -40,7 +39,7 @@ import {
4039
reduceTestItemChildren,
4140
} from "../../../src/TestExplorer/TestUtils";
4241
import { runnableTag } from "../../../src/TestExplorer/TestDiscovery";
43-
import { activateExtensionForTest, updateSettings } from "../utilities/testutilities";
42+
import { activateExtensionForSuite, updateSettings } from "../utilities/testutilities";
4443
import { Commands } from "../../../src/commands";
4544
import { SwiftToolchain } from "../../../src/toolchain/toolchain";
4645

@@ -52,6 +51,26 @@ suite("Test Explorer Suite", function () {
5251
let workspaceContext: WorkspaceContext;
5352
let testExplorer: TestExplorer;
5453

54+
activateExtensionForSuite({
55+
async setup(ctx) {
56+
workspaceContext = ctx;
57+
const packageFolder = testAssetUri("defaultPackage");
58+
const targetFolder = workspaceContext.folders.find(
59+
folder => folder.folder.path === packageFolder.path
60+
);
61+
62+
if (!targetFolder) {
63+
throw new Error("Unable to find test explorer");
64+
}
65+
66+
testExplorer = targetFolder.addTestExplorer();
67+
68+
// Set up the listener before bringing the text explorer in to focus,
69+
// which starts searching the workspace for tests.
70+
await waitForTestExplorerReady(testExplorer);
71+
},
72+
});
73+
5574
suite("Debugging", function () {
5675
async function runXCTest() {
5776
const suiteId = "PackageTests.PassingXCTestSuite";
@@ -81,27 +100,22 @@ suite("Test Explorer Suite", function () {
81100
}
82101

83102
suite("lldb-dap", () => {
84-
activateExtensionForTest({
85-
async setup(ctx) {
86-
// lldb-dap is only present/functional in the toolchain in 6.0.2 and up.
87-
if (ctx.swiftVersion.isLessThan(new Version(6, 0, 2))) {
88-
this.skip();
89-
}
90-
91-
workspaceContext = ctx;
92-
testExplorer = testExplorerFor(
93-
workspaceContext,
94-
testAssetUri("defaultPackage")
95-
);
103+
let resetSettings: (() => Promise<void>) | undefined;
104+
beforeEach(async function () {
105+
// lldb-dap is only present/functional in the toolchain in 6.0.2 and up.
106+
if (workspaceContext.swiftVersion.isLessThan(new Version(6, 0, 2))) {
107+
this.skip();
108+
}
96109

97-
// Set up the listener before bringing the text explorer in to focus,
98-
// which starts searching the workspace for tests.
99-
await waitForTestExplorerReady(testExplorer);
110+
resetSettings = await updateSettings({
111+
"swift.debugger.useDebugAdapterFromToolchain": true,
112+
});
113+
});
100114

101-
return await updateSettings({
102-
"swift.debugger.useDebugAdapterFromToolchain": true,
103-
});
104-
},
115+
afterEach(async () => {
116+
if (resetSettings) {
117+
await resetSettings();
118+
}
105119
});
106120

107121
test("Debugs specified XCTest test", runXCTest);
@@ -122,34 +136,28 @@ suite("Test Explorer Suite", function () {
122136
}
123137
}
124138

125-
activateExtensionForTest({
126-
async setup(ctx) {
127-
// CodeLLDB on windows doesn't print output and so cannot be parsed
128-
if (process.platform === "win32") {
129-
this.skip();
130-
return;
131-
}
132-
133-
workspaceContext = ctx;
134-
testExplorer = testExplorerFor(
135-
workspaceContext,
136-
testAssetUri("defaultPackage")
137-
);
139+
let resetSettings: (() => Promise<void>) | undefined;
140+
beforeEach(async function () {
141+
// CodeLLDB on windows doesn't print output and so cannot be parsed
142+
if (process.platform === "win32") {
143+
this.skip();
144+
}
138145

139-
// Set up the listener before bringing the text explorer in to focus,
140-
// which starts searching the workspace for tests.
141-
await waitForTestExplorerReady(testExplorer);
146+
const lldbPath =
147+
process.env["CI"] === "1"
148+
? { "lldb.library": await getLLDBDebugAdapterPath() }
149+
: {};
142150

143-
const lldbPath =
144-
process.env["CI"] === "1"
145-
? { "lldb.library": await getLLDBDebugAdapterPath() }
146-
: {};
151+
resetSettings = await updateSettings({
152+
"swift.debugger.useDebugAdapterFromToolchain": false,
153+
...lldbPath,
154+
});
155+
});
147156

148-
return await updateSettings({
149-
"swift.debugger.useDebugAdapterFromToolchain": false,
150-
...lldbPath,
151-
});
152-
},
157+
afterEach(async () => {
158+
if (resetSettings) {
159+
await resetSettings();
160+
}
153161
});
154162

155163
test("Debugs specified XCTest test", async function () {
@@ -170,26 +178,6 @@ suite("Test Explorer Suite", function () {
170178
});
171179

172180
suite("Standard", () => {
173-
activateExtensionForTest({
174-
async setup(ctx) {
175-
workspaceContext = ctx;
176-
const packageFolder = testAssetUri("defaultPackage");
177-
const targetFolder = workspaceContext.folders.find(
178-
folder => folder.folder.path === packageFolder.path
179-
);
180-
181-
if (!targetFolder) {
182-
throw new Error("Unable to find test explorer");
183-
}
184-
185-
testExplorer = targetFolder.addTestExplorer();
186-
187-
// Set up the listener before bringing the text explorer in to focus,
188-
// which starts searching the workspace for tests.
189-
await waitForTestExplorerReady(testExplorer);
190-
},
191-
});
192-
193181
test("Finds Tests", async function () {
194182
if (workspaceContext.swiftVersion.isGreaterThanOrEqual(new Version(6, 0, 0))) {
195183
// 6.0 uses the LSP which returns tests in the order they're declared.

test/integration-tests/testexplorer/utilities.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,16 @@ export async function gatherTests(
239239
const testItems = tests.map(test => {
240240
const testItem = getTestItem(controller, test);
241241
if (!testItem) {
242-
const testsInController: string[] = [];
243-
controller.items.forEach(item => {
244-
testsInController.push(
245-
`${item.id}: ${item.label} ${item.error ? `(error: ${item.error})` : ""}`
246-
);
247-
});
242+
const testsInController = reduceTestItemChildren(
243+
controller.items,
244+
(acc, item) => {
245+
acc.push(
246+
`${item.id}: ${item.label} ${item.error ? `(error: ${item.error})` : ""}`
247+
);
248+
return acc;
249+
},
250+
[] as string[]
251+
);
248252

249253
assert.fail(
250254
`Unable to find ${test} in Test Controller. Items in test controller are: ${testsInController.join(", ")}`

0 commit comments

Comments
 (0)