Skip to content

Commit 5138e12

Browse files
committed
Some clean up
1 parent d4d3dfb commit 5138e12

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

src/SwiftSnippets.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ export function setSnippetContextKey(ctx: WorkspaceContext) {
4949
* If current file is a Swift Snippet run it
5050
* @param ctx Workspace Context
5151
*/
52-
export async function runSnippet(ctx: WorkspaceContext) {
53-
await debugSnippetWithOptions(ctx, { noDebug: true });
52+
export async function runSnippet(ctx: WorkspaceContext): Promise<boolean | undefined> {
53+
return await debugSnippetWithOptions(ctx, { noDebug: true });
5454
}
5555

5656
/**
5757
* If current file is a Swift Snippet run it in the debugger
5858
* @param ctx Workspace Context
5959
*/
60-
export async function debugSnippet(ctx: WorkspaceContext) {
61-
await debugSnippetWithOptions(ctx, {});
60+
export async function debugSnippet(ctx: WorkspaceContext): Promise<boolean | undefined> {
61+
return await debugSnippetWithOptions(ctx, {});
6262
}
6363

6464
export async function debugSnippetWithOptions(
6565
ctx: WorkspaceContext,
6666
options: vscode.DebugSessionOptions
67-
) {
67+
): Promise<boolean | undefined> {
6868
const folderContext = ctx.currentFolder;
6969
if (!ctx.currentDocument || !folderContext) {
7070
return;
@@ -89,7 +89,7 @@ export async function debugSnippetWithOptions(
8989

9090
try {
9191
// queue build task and when it is complete run executable in the debugger
92-
await folderContext.taskQueue
92+
return await folderContext.taskQueue
9393
.queueOperation(new TaskOperation(snippetBuildTask))
9494
.then(result => {
9595
if (result === 0) {
@@ -106,5 +106,6 @@ export async function debugSnippetWithOptions(
106106
});
107107
} catch {
108108
// ignore error if task failed to run
109+
return;
109110
}
110111
}

test/integration-tests/SwiftSnippet.test.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,24 @@ import { expect } from "chai";
1919
import {
2020
continueSession,
2121
waitForDebugAdapterRequest,
22-
waitForDebugAdapterExit,
2322
waitUntilDebugSessionTerminates,
2423
} from "../utilities/debug";
2524
import { Version } from "../../src/utilities/version";
2625
import { activateExtensionForSuite, folderInRootWorkspace } from "./utilities/testutilities";
2726
import { WorkspaceContext } from "../../src/WorkspaceContext";
2827
import { join } from "path";
29-
30-
suite("SwiftSnippet Test Suite", function () {
28+
import { closeAllEditors } from "../utilities/commands";
29+
30+
function normalizePath(...segments: string[]): string {
31+
let path = join(...segments);
32+
if (process.platform === "win32") {
33+
path = path + ".exe";
34+
path = path.replace(/\//g, "\\");
35+
}
36+
return path.toLocaleLowerCase(); // Windows may use d:\ or D:\
37+
}
38+
39+
suite.only("SwiftSnippet Test Suite", function () {
3140
this.timeout(120000);
3241

3342
const uri = testAssetUri("defaultPackage/Snippets/hello.swift");
@@ -56,50 +65,37 @@ suite("SwiftSnippet Test Suite", function () {
5665
});
5766

5867
suiteTeardown(async () => {
59-
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
68+
closeAllEditors();
6069
vscode.debug.removeBreakpoints(breakpoints);
6170
});
6271

6372
test("Run `Swift: Run Swift Snippet` command for snippet file", async () => {
6473
const sessionPromise = waitUntilDebugSessionTerminates("Run hello");
65-
const exitPromise = waitForDebugAdapterExit("Run hello");
66-
67-
await vscode.commands.executeCommand("swift.runSnippet");
6874

69-
const exitCode = await exitPromise;
70-
expect(exitCode).to.equal(0);
75+
const succeeded = await vscode.commands.executeCommand("swift.runSnippet");
7176

77+
expect(succeeded).to.be.true;
7278
const session = await sessionPromise;
73-
let path = join(testAssetPath("defaultPackage"), ".build", "debug", "hello");
74-
if (process.platform === "win32") {
75-
path = path + ".exe";
76-
}
77-
expect(session.configuration.program?.toLowerCase()).to.equal(
78-
path.toLocaleLowerCase() // Windows may use d:\ or D:\
79+
expect(normalizePath(session.configuration.program)).to.equal(
80+
normalizePath(testAssetPath("defaultPackage"), ".build", "debug", "hello")
7981
);
8082
expect(session.configuration).to.have.property("noDebug", true);
8183
});
8284

8385
test("Run `Swift: Debug Swift Snippet` command for snippet file", async () => {
8486
const bpPromise = waitForDebugAdapterRequest("Run hello", "stackTrace");
8587
const sessionPromise = waitUntilDebugSessionTerminates("Run hello");
86-
const exitPromise = waitForDebugAdapterExit("Run hello");
8788

88-
vscode.commands.executeCommand("swift.debugSnippet");
89+
const succeeded = vscode.commands.executeCommand("swift.debugSnippet");
8990

9091
// Once bp is hit, continue
9192
await bpPromise.then(() => continueSession());
9293

93-
const exitCode = await exitPromise;
94-
expect(exitCode).to.equal(0);
94+
await expect(succeeded).to.eventually.be.true;
9595

9696
const session = await sessionPromise;
97-
let path = join(testAssetPath("defaultPackage"), ".build", "debug", "hello");
98-
if (process.platform === "win32") {
99-
path = path + ".exe";
100-
}
101-
expect(session.configuration.program?.toLowerCase()).to.equal(
102-
path.toLocaleLowerCase() // Windows may use d:\ or D:\
97+
expect(normalizePath(session.configuration.program)).to.equal(
98+
normalizePath(testAssetPath("defaultPackage"), ".build", "debug", "hello")
10399
);
104100
expect(session.configuration).to.not.have.property("noDebug");
105101
});

test/integration-tests/utilities/testutilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { testAssetUri } from "../../fixtures";
2020
import { WorkspaceContext } from "../../../src/WorkspaceContext";
2121
import { FolderContext } from "../../../src/FolderContext";
2222
import { waitForNoRunningTasks } from "../../utilities/tasks";
23+
import { closeAllEditors } from "../../utilities/commands";
2324

2425
function getRootWorkspaceFolder(): vscode.WorkspaceFolder {
2526
const result = vscode.workspace.workspaceFolders?.at(0);
@@ -175,7 +176,7 @@ const extensionBootstrapper = (() => {
175176
await waitForNoRunningTasks({ timeout: 10000 });
176177

177178
// Close all editors before deactivating the extension.
178-
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
179+
closeAllEditors();
179180

180181
await activatedAPI.workspaceContext?.removeWorkspaceFolder(getRootWorkspaceFolder());
181182
await activatedAPI.deactivate();

test/utilities/commands.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 { Workbench } from "../../src/utilities/commands";
17+
18+
export async function closeAllEditors() {
19+
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
20+
}

0 commit comments

Comments
 (0)