Skip to content

Commit 2b59455

Browse files
committed
Add integration tests for build commands
- Validate the workflow of user calling the Swift: Run Build/Clean Build/Debug Build commands. - Ensure Swift: Run Build will not get blocked by pre-set breakpoint. - Ensure Swift: Clean Build will result in a cleaned up .build folder. - Ensure Swift: Debug Build will stop on a breakpoint and resume. Issue: #1184
1 parent 6f4544b commit 2b59455

File tree

4 files changed

+95
-9
lines changed

4 files changed

+95
-9
lines changed

src/commands.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ export function registerToolchainCommands(
6464
];
6565
}
6666

67+
export enum Commands {
68+
RUN = "swift.run",
69+
DEBUG = "swift.debug",
70+
CLEAN_BUILD = "swift.cleanBuild",
71+
}
72+
6773
/**
6874
* Registers this extension's commands in the given {@link vscode.ExtensionContext context}.
6975
*/
@@ -74,9 +80,9 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
7480
resolveDependencies(ctx)
7581
),
7682
vscode.commands.registerCommand("swift.updateDependencies", () => updateDependencies(ctx)),
77-
vscode.commands.registerCommand("swift.run", () => runBuild(ctx)),
78-
vscode.commands.registerCommand("swift.debug", () => debugBuild(ctx)),
79-
vscode.commands.registerCommand("swift.cleanBuild", () => cleanBuild(ctx)),
83+
vscode.commands.registerCommand(Commands.RUN, () => runBuild(ctx)),
84+
vscode.commands.registerCommand(Commands.DEBUG, () => debugBuild(ctx)),
85+
vscode.commands.registerCommand(Commands.CLEAN_BUILD, () => cleanBuild(ctx)),
8086
vscode.commands.registerCommand("swift.runTestsMultipleTimes", item => {
8187
if (ctx.currentFolder) {
8288
return runTestMultipleTimes(ctx.currentFolder, item, false);

src/commands/build.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import { FolderContext } from "../FolderContext";
2323
* Executes a {@link vscode.Task task} to run swift target.
2424
*/
2525
export async function runBuild(ctx: WorkspaceContext) {
26-
await debugBuildWithOptions(ctx, { noDebug: true });
26+
return await debugBuildWithOptions(ctx, { noDebug: true });
2727
}
2828

2929
/**
3030
* Executes a {@link vscode.Task task} to debug swift target.
3131
*/
3232
export async function debugBuild(ctx: WorkspaceContext) {
33-
await debugBuildWithOptions(ctx, {});
33+
return await debugBuildWithOptions(ctx, {});
3434
}
3535

3636
/**
@@ -41,7 +41,7 @@ export async function cleanBuild(ctx: WorkspaceContext) {
4141
if (!current) {
4242
return;
4343
}
44-
await folderCleanBuild(current);
44+
return await folderCleanBuild(current);
4545
}
4646

4747
/**
@@ -62,7 +62,7 @@ export async function folderCleanBuild(folderContext: FolderContext) {
6262
folderContext.workspaceContext.toolchain
6363
);
6464

65-
await executeTaskWithUI(task, "Clean Build", folderContext);
65+
return await executeTaskWithUI(task, "Clean Build", folderContext);
6666
}
6767

6868
/**

src/debugger/launch.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,17 @@ export async function debugLaunchConfig(
175175
config: vscode.DebugConfiguration,
176176
options: vscode.DebugSessionOptions = {}
177177
) {
178-
return new Promise<void>((resolve, reject) => {
178+
return new Promise<boolean>((resolve, reject) => {
179179
vscode.debug.startDebugging(workspaceFolder, config, options).then(
180180
started => {
181181
if (started) {
182182
const terminateSession = vscode.debug.onDidTerminateDebugSession(async () => {
183183
// dispose terminate debug handler
184184
terminateSession.dispose();
185-
resolve();
185+
resolve(true);
186186
});
187+
} else {
188+
resolve(false);
187189
}
188190
},
189191
reason => {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2024 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import * as vscode from "vscode";
16+
import * as fs from "fs";
17+
import * as path from "path";
18+
import { expect } from "chai";
19+
import { folderContextPromise, globalWorkspaceContextPromise } from "../extension.test";
20+
import { waitForNoRunningTasks } from "../../utilities";
21+
import { testAssetUri } from "../../fixtures";
22+
import { FolderContext } from "../../../src/FolderContext";
23+
import { WorkspaceContext } from "../../../src/WorkspaceContext";
24+
import { Commands } from "../../../src/commands";
25+
import { makeDebugConfigurations } from "../../../src/debugger/launch";
26+
27+
suite("Build Commands", function () {
28+
let folderContext: FolderContext;
29+
let workspaceContext: WorkspaceContext;
30+
const uri = testAssetUri("defaultPackage/Sources/PackageExe/main.swift");
31+
const breakpoints = [
32+
new vscode.SourceBreakpoint(new vscode.Location(uri, new vscode.Position(2, 0))),
33+
];
34+
35+
suiteSetup(async function () {
36+
workspaceContext = await globalWorkspaceContextPromise;
37+
await waitForNoRunningTasks();
38+
folderContext = await folderContextPromise("defaultPackage");
39+
await workspaceContext.focusFolder(folderContext);
40+
await vscode.window.showTextDocument(uri);
41+
makeDebugConfigurations(folderContext, undefined, true);
42+
});
43+
44+
suiteTeardown(async () => {
45+
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
46+
});
47+
48+
test("Swift: Run Build", async () => {
49+
// A breakpoint will have not effect on the Run command.
50+
vscode.debug.addBreakpoints(breakpoints);
51+
52+
const result = await vscode.commands.executeCommand(Commands.RUN);
53+
expect(result).to.be.true;
54+
55+
vscode.debug.removeBreakpoints(breakpoints);
56+
});
57+
58+
test("Swift: Clean Build", async () => {
59+
const buildPath = path.join(folderContext.folder.fsPath, ".build");
60+
const beforeItemCount = fs.readdirSync(buildPath).length;
61+
62+
const result = await vscode.commands.executeCommand(Commands.CLEAN_BUILD);
63+
expect(result).to.be.true;
64+
65+
const afterItemCount = fs.readdirSync(buildPath).length;
66+
expect(afterItemCount).to.be.lessThan(beforeItemCount);
67+
});
68+
69+
test("Swift: Debug Build", async () => {
70+
vscode.debug.addBreakpoints(breakpoints);
71+
72+
const result = vscode.commands.executeCommand(Commands.DEBUG);
73+
expect(result).to.eventually.be.true;
74+
75+
await vscode.commands.executeCommand("workbench.action.debug.continue");
76+
vscode.debug.removeBreakpoints(breakpoints);
77+
});
78+
});

0 commit comments

Comments
 (0)