Skip to content

Commit

Permalink
Some clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
award999 committed Dec 10, 2024
1 parent d4d3dfb commit ce73bd3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
13 changes: 7 additions & 6 deletions src/SwiftSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ export function setSnippetContextKey(ctx: WorkspaceContext) {
* If current file is a Swift Snippet run it
* @param ctx Workspace Context
*/
export async function runSnippet(ctx: WorkspaceContext) {
await debugSnippetWithOptions(ctx, { noDebug: true });
export async function runSnippet(ctx: WorkspaceContext): Promise<boolean | undefined> {
return await debugSnippetWithOptions(ctx, { noDebug: true });
}

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

export async function debugSnippetWithOptions(
ctx: WorkspaceContext,
options: vscode.DebugSessionOptions
) {
): Promise<boolean | undefined> {
const folderContext = ctx.currentFolder;
if (!ctx.currentDocument || !folderContext) {
return;
Expand All @@ -89,7 +89,7 @@ export async function debugSnippetWithOptions(

try {
// queue build task and when it is complete run executable in the debugger
await folderContext.taskQueue
return await folderContext.taskQueue
.queueOperation(new TaskOperation(snippetBuildTask))
.then(result => {
if (result === 0) {
Expand All @@ -106,5 +106,6 @@ export async function debugSnippetWithOptions(
});
} catch {
// ignore error if task failed to run
return;
}
}
42 changes: 19 additions & 23 deletions test/integration-tests/SwiftSnippet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ import { expect } from "chai";
import {
continueSession,
waitForDebugAdapterRequest,
waitForDebugAdapterExit,
waitUntilDebugSessionTerminates,
} from "../utilities/debug";
import { Version } from "../../src/utilities/version";
import { activateExtensionForSuite, folderInRootWorkspace } from "./utilities/testutilities";
import { WorkspaceContext } from "../../src/WorkspaceContext";
import { join } from "path";
import { closeAllEditors } from "../utilities/commands";

function normalizePath(...segments: string[]): string {
let path = join(...segments);
if (process.platform === "win32") {
path = path + ".exe";
path = path.replace(/\//g, "\\");
}
return path.toLocaleLowerCase(); // Windows may use d:\ or D:\
}

suite("SwiftSnippet Test Suite", function () {
this.timeout(120000);
Expand Down Expand Up @@ -56,50 +65,37 @@ suite("SwiftSnippet Test Suite", function () {
});

suiteTeardown(async () => {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
closeAllEditors();
vscode.debug.removeBreakpoints(breakpoints);
});

test("Run `Swift: Run Swift Snippet` command for snippet file", async () => {
const sessionPromise = waitUntilDebugSessionTerminates("Run hello");
const exitPromise = waitForDebugAdapterExit("Run hello");

await vscode.commands.executeCommand("swift.runSnippet");

const exitCode = await exitPromise;
expect(exitCode).to.equal(0);
const succeeded = await vscode.commands.executeCommand("swift.runSnippet");

expect(succeeded).to.be.true;
const session = await sessionPromise;
let path = join(testAssetPath("defaultPackage"), ".build", "debug", "hello");
if (process.platform === "win32") {
path = path + ".exe";
}
expect(session.configuration.program?.toLowerCase()).to.equal(
path.toLocaleLowerCase() // Windows may use d:\ or D:\
expect(normalizePath(session.configuration.program)).to.equal(
normalizePath(testAssetPath("defaultPackage"), ".build", "debug", "hello")
);
expect(session.configuration).to.have.property("noDebug", true);
});

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

vscode.commands.executeCommand("swift.debugSnippet");
const succeeded = vscode.commands.executeCommand("swift.debugSnippet");

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

const exitCode = await exitPromise;
expect(exitCode).to.equal(0);
await expect(succeeded).to.eventually.be.true;

const session = await sessionPromise;
let path = join(testAssetPath("defaultPackage"), ".build", "debug", "hello");
if (process.platform === "win32") {
path = path + ".exe";
}
expect(session.configuration.program?.toLowerCase()).to.equal(
path.toLocaleLowerCase() // Windows may use d:\ or D:\
expect(normalizePath(session.configuration.program)).to.equal(
normalizePath(testAssetPath("defaultPackage"), ".build", "debug", "hello")
);
expect(session.configuration).to.not.have.property("noDebug");
});
Expand Down
3 changes: 2 additions & 1 deletion test/integration-tests/utilities/testutilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { testAssetUri } from "../../fixtures";
import { WorkspaceContext } from "../../../src/WorkspaceContext";
import { FolderContext } from "../../../src/FolderContext";
import { waitForNoRunningTasks } from "../../utilities/tasks";
import { closeAllEditors } from "../../utilities/commands";

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

// Close all editors before deactivating the extension.
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
closeAllEditors();

await activatedAPI.workspaceContext?.removeWorkspaceFolder(getRootWorkspaceFolder());
await activatedAPI.deactivate();
Expand Down
20 changes: 20 additions & 0 deletions test/utilities/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 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 { Workbench } from "../../src/utilities/commands";

export async function closeAllEditors() {
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
}

0 comments on commit ce73bd3

Please sign in to comment.