Skip to content

Commit 6ce707c

Browse files
committed
Some e2e/smoke tests for our integration with sourcekit-lsp
* Moving macros into common file with tests from #1236 * Fix some flakyness and failures
1 parent cf37af5 commit 6ce707c

File tree

2 files changed

+93
-33
lines changed

2 files changed

+93
-33
lines changed

test/integration-tests/language/macro.test.ts test/integration-tests/language/LanguageClientIntegration.test.ts

+80-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { expect } from "chai";
1818
import { LanguageClientManager } from "../../../src/sourcekit-lsp/LanguageClientManager";
1919
import { WorkspaceContext } from "../../../src/WorkspaceContext";
2020
import { testAssetUri } from "../../fixtures";
21-
import { FolderContext } from "../../../src/FolderContext";
2221
import { executeTaskAndWaitForResult, waitForNoRunningTasks } from "../../utilities";
2322
import { getBuildAllTask, SwiftTask } from "../../../src/tasks/SwiftTaskProvider";
2423
import { Version } from "../../../src/utilities/version";
@@ -37,38 +36,43 @@ async function waitForClientState(
3736
return clientState;
3837
}
3938

40-
suite("Integration, Macros Functionality Support with Sourcekit-lsp", function () {
41-
// Take around 60 seconds if running in isolation, longer than default timeout
42-
this.timeout(2 * 60 * 1000);
39+
async function buildProject(ctx: WorkspaceContext, name: string) {
40+
await waitForNoRunningTasks();
41+
const folderContext = await folderInRootWorkspace(name, ctx);
42+
const task = (await getBuildAllTask(folderContext)) as SwiftTask;
43+
const { exitCode, output } = await executeTaskAndWaitForResult(task);
44+
expect(exitCode, `${output}`).to.equal(0);
45+
}
4346

47+
suite("Language Client Integration Suite", function () {
4448
let clientManager: LanguageClientManager;
4549
let workspaceContext: WorkspaceContext;
46-
let folderContext: FolderContext;
4750

4851
activateExtensionForSuite({
4952
async setup(ctx) {
53+
this.timeout(5 * 60 * 1000);
54+
5055
workspaceContext = ctx;
51-
// Expand Macro support in Swift started from 6.1
52-
if (workspaceContext.swiftVersion.isLessThan(new Version(6, 1, 0))) {
53-
this.skip();
54-
}
5556

5657
// Wait for a clean starting point, and build all tasks for the fixture
57-
await waitForNoRunningTasks();
58-
folderContext = await folderInRootWorkspace("swift-macro", workspaceContext);
59-
await workspaceContext.focusFolder(folderContext);
60-
const tasks = (await getBuildAllTask(folderContext)) as SwiftTask;
61-
const { exitCode, output } = await executeTaskAndWaitForResult(tasks);
62-
expect(exitCode, `${output}`).to.equal(0);
58+
if (workspaceContext.swiftVersion.isGreaterThanOrEqual(new Version(6, 1, 0))) {
59+
await buildProject(ctx, "swift-macro");
60+
}
61+
await buildProject(ctx, "defaultPackage");
6362

6463
// Ensure lsp client is ready
65-
clientManager = workspaceContext.languageClientManager;
64+
clientManager = ctx.languageClientManager;
6665
const clientState = await waitForClientState(clientManager, langclient.State.Running);
6766
expect(clientState).to.equals(langclient.State.Running);
6867
},
6968
});
7069

7170
test("Expand Macro", async function () {
71+
// Expand Macro support in Swift started from 6.1
72+
if (workspaceContext.swiftVersion.isLessThan(new Version(6, 1, 0))) {
73+
this.skip();
74+
}
75+
7276
// Focus on the file of interest
7377
const uri = testAssetUri("swift-macro/Sources/swift-macroClient/main.swift");
7478
await vscode.window.showTextDocument(uri);
@@ -128,4 +132,64 @@ suite("Integration, Macros Functionality Support with Sourcekit-lsp", function (
128132
const content = referenceDocument.getText();
129133
expect(content).to.include(expectedMacro);
130134
});
135+
136+
suite("Symbols", () => {
137+
const uri = testAssetUri("defaultPackage/Sources/PackageExe/main.swift");
138+
const expectedDefinitionUri = testAssetUri(
139+
"defaultPackage/Sources/PackageLib/PackageLib.swift"
140+
);
141+
// Position of the symbol 'a' in main.swift
142+
const position = new vscode.Position(2, 6);
143+
144+
test("Goto Definition", async function () {
145+
// Focus on the file of interest
146+
const editor = await vscode.window.showTextDocument(uri);
147+
const document = editor.document;
148+
149+
// Position of the symbol 'a' in main.swift
150+
const definitionLocations = await vscode.commands.executeCommand<vscode.Location[]>(
151+
"vscode.executeDefinitionProvider",
152+
document.uri,
153+
position
154+
);
155+
156+
expect(definitionLocations).to.have.lengthOf(
157+
1,
158+
"There should be one definition of 'a'."
159+
);
160+
161+
const definition = definitionLocations[0];
162+
163+
// Assert that the definition is in PackageLib.swift at line 0
164+
expect(definition.uri.toString()).to.equal(expectedDefinitionUri.toString());
165+
expect(definition.range.start.line).to.equal(0);
166+
});
167+
168+
test("Find All References", async function () {
169+
// Focus on the file of interest
170+
const editor = await vscode.window.showTextDocument(uri);
171+
const document = editor.document;
172+
173+
const referenceLocations = await vscode.commands.executeCommand<vscode.Location[]>(
174+
"vscode.executeReferenceProvider",
175+
document.uri,
176+
position
177+
);
178+
179+
// We expect 2 references - one in `main.swift` and one in `PackageLib.swift`
180+
expect(referenceLocations).to.have.lengthOf(
181+
2,
182+
"There should be two references to 'a'."
183+
);
184+
185+
// Extract reference URIs and sort them to have a predictable order
186+
const referenceUris = referenceLocations.map(ref => ref.uri.toString()).sort();
187+
const expectedUris = [
188+
uri.toString(), // Reference in main.swift
189+
expectedDefinitionUri.toString(), // Reference in PackageLib.swift
190+
].sort();
191+
192+
expect(referenceUris).to.deep.equal(expectedUris);
193+
});
194+
});
131195
});

test/utilities.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,19 @@ export function mutable<T>(target: T): Mutable<T> {
3636
export async function executeTaskAndWaitForResult(
3737
fixture: SwiftTaskFixture | SwiftTask
3838
): Promise<{ exitCode?: number; output: string }> {
39-
const task = ("task" in fixture ? fixture.task : fixture) as SwiftTask;
40-
let output = "";
41-
const disposables = [task.execution.onDidWrite(e => (output += e))];
42-
const promise = new Promise<number | undefined>(res =>
43-
disposables.push(
44-
task.execution.onDidClose(e => {
45-
disposables.forEach(d => d.dispose());
46-
res(typeof e === "number" ? e : undefined);
47-
})
48-
)
49-
);
50-
await vscode.tasks.executeTask(task);
51-
const exitCode = await promise;
52-
return {
53-
output,
54-
exitCode,
55-
};
39+
const task = "task" in fixture ? fixture.task : fixture;
40+
const exitPromise = waitForEndTaskProcess(task);
41+
return await vscode.tasks.executeTask(task).then(async execution => {
42+
let output = "";
43+
const runningTask = execution.task as SwiftTask;
44+
const disposables = [runningTask.execution.onDidWrite(e => (output += e))];
45+
const exitCode = await exitPromise;
46+
disposables.forEach(d => d.dispose());
47+
return {
48+
output,
49+
exitCode,
50+
};
51+
});
5652
}
5753

5854
/**

0 commit comments

Comments
 (0)