Skip to content

Commit

Permalink
- Added module enum for workbench commands string constant
Browse files Browse the repository at this point in the history
- Added comments for clarification
- Added utilities to listen for dap message, this is useful for test
synchronization. Code takes inspiration from
swiftlang#1126
  • Loading branch information
michael-weng committed Oct 31, 2024
1 parent 2b59455 commit 91f7852
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/ui/ReloadExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

import * as vscode from "vscode";
import { Workbench } from "../utilities/command";

/**
* Prompts the user to reload the extension in cases where we are unable to do
Expand All @@ -29,7 +30,7 @@ export async function showReloadExtensionNotification<T extends string>(
const buttons: ("Reload Extensions" | T)[] = ["Reload Extensions", ...items];
const selected = await vscode.window.showWarningMessage(message, ...buttons);
if (selected === "Reload Extensions") {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
await vscode.commands.executeCommand(Workbench.ACTION_RELOADWINDOW);
}
return selected;
}
19 changes: 19 additions & 0 deletions src/utilities/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
export enum Workbench {
ACTION_DEBUG_CONTINUE = "workbench.action.debug.continue",
ACTION_CLOSEALLEDITORS = "workbench.action.closeAllEditors",
ACTION_RELOADWINDOW = "workbench.action.reloadWindow",
}
3 changes: 2 additions & 1 deletion test/integration-tests/BackgroundCompilation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { WorkspaceContext } from "../../src/WorkspaceContext";
import { globalWorkspaceContextPromise } from "./extension.test";
import { testAssetUri } from "../fixtures";
import { waitForNoRunningTasks } from "../utilities";
import { Workbench } from "../../src/utilities/command";

suite("BackgroundCompilation Test Suite", () => {
let workspaceContext: WorkspaceContext;
Expand All @@ -31,7 +32,7 @@ suite("BackgroundCompilation Test Suite", () => {

suiteTeardown(async () => {
await vscode.workspace.getConfiguration("swift").update("backgroundCompilation", undefined);
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
});

test("build all on save @slow", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/integration-tests/DiagnosticsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DiagnosticsManager } from "../../src/DiagnosticsManager";
import { FolderContext } from "../../src/FolderContext";
import { Version } from "../../src/utilities/version";
import { folderContextPromise, globalWorkspaceContextPromise } from "./extension.test";
import { Workbench } from "../../src/utilities/command";

const waitForDiagnostics = (uris: vscode.Uri[], allowEmpty: boolean = true) =>
new Promise<void>(res =>
Expand Down Expand Up @@ -907,7 +908,7 @@ suite("DiagnosticsManager Test Suite", async function () {
});

teardown(async () => {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
});

test("Provides swift diagnostics", async () => {
Expand Down
19 changes: 16 additions & 3 deletions test/integration-tests/commands/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { FolderContext } from "../../../src/FolderContext";
import { WorkspaceContext } from "../../../src/WorkspaceContext";
import { Commands } from "../../../src/commands";
import { makeDebugConfigurations } from "../../../src/debugger/launch";
import { Workbench } from "../../../src/utilities/command";
import { continueSession, waitForDebugAdapterCommand } from "../../utilities/debug";

suite("Build Commands", function () {
let folderContext: FolderContext;
Expand All @@ -42,7 +44,7 @@ suite("Build Commands", function () {
});

suiteTeardown(async () => {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
});

test("Swift: Run Build", async () => {
Expand All @@ -63,16 +65,27 @@ suite("Build Commands", function () {
expect(result).to.be.true;

const afterItemCount = fs.readdirSync(buildPath).length;
// This test will run in order after the Swift: Run Build test,
// where .build folder is going to be filled with built artifacts.
// After executing the clean command the build directory is guranteed to have less entry.
expect(afterItemCount).to.be.lessThan(beforeItemCount);
});

test("Swift: Debug Build", async () => {
test("Swift: Debug Build @slow", async () => {
vscode.debug.addBreakpoints(breakpoints);
// Promise used to indicate we hit the break point.
// NB: "stopped" is the exact command when debuggee has stopped due to break point,
// but "stackTrace" is the deterministic sync point we will use to make sure we can execute continue
const bpPromise = waitForDebugAdapterCommand(
"Debug PackageExe (defaultPackage)",
"stackTrace",
workspaceContext
);

const result = vscode.commands.executeCommand(Commands.DEBUG);
expect(result).to.eventually.be.true;

await vscode.commands.executeCommand("workbench.action.debug.continue");
await bpPromise.then(() => continueSession());
vscode.debug.removeBreakpoints(breakpoints);
});
});
3 changes: 2 additions & 1 deletion test/integration-tests/editor/CommentCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import * as assert from "assert";
import * as vscode from "vscode";
import { CommentCompletionProviders } from "../../../src/editor/CommentCompletion";
import { Workbench } from "../../../src/utilities/command";

suite("CommentCompletion Test Suite", () => {
let document: vscode.TextDocument | undefined;
Expand All @@ -31,7 +32,7 @@ suite("CommentCompletion Test Suite", () => {

if (editor && document) {
await vscode.window.showTextDocument(document, editor.viewColumn);
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
}

provider.dispose();
Expand Down
3 changes: 2 additions & 1 deletion test/unit-tests/ui/ReloadExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { expect } from "chai";
import { mockGlobalObject } from "../../MockUtils";
import * as vscode from "vscode";
import { showReloadExtensionNotification } from "../../../src/ui/ReloadExtension";
import { Workbench } from "../../../src/utilities/command";

suite("showReloadExtensionNotification()", async function () {
const mockedVSCodeWindow = mockGlobalObject(vscode, "window");
Expand All @@ -38,7 +39,7 @@ suite("showReloadExtensionNotification()", async function () {
await showReloadExtensionNotification("Want to reload?");

expect(mockedVSCodeCommands.executeCommand).to.have.been.calledOnceWithExactly(
"workbench.action.reloadWindow"
Workbench.ACTION_RELOADWINDOW
);
});

Expand Down
78 changes: 78 additions & 0 deletions test/utilities/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// 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/command";
import { DebugAdapter } from "../../src/debugger/debugAdapter";
import { WorkspaceContext } from "../../src/WorkspaceContext";

export async function continueSession(): Promise<void> {
await vscode.commands.executeCommand(Workbench.ACTION_DEBUG_CONTINUE);
}

/**
* Waits for a specific message from the debug adapter.
*
* @param name The name of the debug session to wait for.
* @param matches A function to match the desired message.
* @param workspaceContext The workspace context containing toolchain information.
* @returns A promise that resolves with the matching message.
*/
export async function waitForDebugAdapterMessage(
name: string,
matches: (message: any) => boolean,
workspaceContext: WorkspaceContext
): Promise<any> {
return await new Promise<any>(res => {
const disposable = vscode.debug.registerDebugAdapterTrackerFactory(
DebugAdapter.getLaunchConfigType(workspaceContext.toolchain.swiftVersion),
{
createDebugAdapterTracker: function (
session: vscode.DebugSession
): vscode.ProviderResult<vscode.DebugAdapterTracker> {
if (session.name !== name) {
return;
}
return {
onDidSendMessage(message) {
if (matches(message)) {
disposable.dispose();
res(message);
}
},
};
},
}
);
});
}

/**
* Waits for a specific command to be sent by the debug adapter.
*
* @param name The name of the debug session to wait for.
* @param command The command to wait for.
* @param workspaceContext The workspace context containing toolchain information.
* @returns A promise that resolves with the matching command message.
*/
export async function waitForDebugAdapterCommand(
name: string,
command: string,
workspaceContext: WorkspaceContext
): Promise<any> {
return await waitForDebugAdapterMessage(
name,
(m: any) => m.command === command,
workspaceContext
);
}

0 comments on commit 91f7852

Please sign in to comment.