diff --git a/src/commands.ts b/src/commands.ts index 63be024dd..7da7fac2a 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -72,6 +72,7 @@ export enum Commands { UPDATE_DEPENDENCIES = "swift.updateDependencies", RUN_TESTS_MULTIPLE_TIMES = "swift.runTestsMultipleTimes", RESET_PACKAGE = "swift.resetPackage", + RUN_PLUGIN_TASK = "swift.runPluginTask", USE_LOCAL_DEPENDENCY = "swift.useLocalDependency", UNEDIT_DEPENDENCY = "swift.uneditDependency", } @@ -112,7 +113,7 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] { }), vscode.commands.registerCommand("swift.runSnippet", () => runSnippet(ctx)), vscode.commands.registerCommand("swift.debugSnippet", () => debugSnippet(ctx)), - vscode.commands.registerCommand("swift.runPluginTask", () => runPluginTask()), + vscode.commands.registerCommand(Commands.RUN_PLUGIN_TASK, () => runPluginTask()), vscode.commands.registerCommand("swift.restartLSPServer", () => ctx.languageClientManager.restart() ), diff --git a/src/commands/runPluginTask.ts b/src/commands/runPluginTask.ts index ede0fcd8c..9e4dda370 100644 --- a/src/commands/runPluginTask.ts +++ b/src/commands/runPluginTask.ts @@ -15,7 +15,7 @@ import * as vscode from "vscode"; export async function runPluginTask() { - vscode.commands.executeCommand("workbench.action.tasks.runTask", { - type: "swift-plugin", - }); + const args = { type: "swift-plugin" }; + await vscode.commands.executeCommand("workbench.action.tasks.runTask", args); + return args; } diff --git a/test/integration-tests/BackgroundCompilation.test.ts b/test/integration-tests/BackgroundCompilation.test.ts index 54f4d4456..e1c02655a 100644 --- a/test/integration-tests/BackgroundCompilation.test.ts +++ b/test/integration-tests/BackgroundCompilation.test.ts @@ -17,7 +17,6 @@ import * as vscode from "vscode"; import { WorkspaceContext } from "../../src/WorkspaceContext"; import { testAssetUri } from "../fixtures"; import { waitForNoRunningTasks } from "../utilities"; -import { Workbench } from "../../src/utilities/commands"; import { activateExtensionForTest, updateSettings } from "./utilities/testutilities"; suite("BackgroundCompilation Test Suite", () => { @@ -34,10 +33,6 @@ suite("BackgroundCompilation Test Suite", () => { }, }); - suiteTeardown(async () => { - await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS); - }); - test("build all on save @slow", async () => { const taskPromise = new Promise(res => { vscode.tasks.onDidStartTask(e => { diff --git a/test/integration-tests/commands/build.test.ts b/test/integration-tests/commands/build.test.ts index 3c8decc75..d1317e1dd 100644 --- a/test/integration-tests/commands/build.test.ts +++ b/test/integration-tests/commands/build.test.ts @@ -53,9 +53,6 @@ suite("Build Commands", function () { await makeDebugConfigurations(folderContext, undefined, true); return settingsTeardown; }, - async teardown() { - await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS); - }, }); teardown(async () => { diff --git a/test/integration-tests/commands/runPluginTask.test.ts b/test/integration-tests/commands/runPluginTask.test.ts new file mode 100644 index 000000000..b310ba508 --- /dev/null +++ b/test/integration-tests/commands/runPluginTask.test.ts @@ -0,0 +1,63 @@ +//===----------------------------------------------------------------------===// +// +// 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 { expect } from "chai"; +import { testAssetUri } from "../../fixtures"; +import { FolderContext } from "../../../src/FolderContext"; +import { WorkspaceContext } from "../../../src/WorkspaceContext"; +import { Commands } from "../../../src/commands"; +import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities"; + +suite("Build Commands", function () { + let folderContext: FolderContext; + let workspaceContext: WorkspaceContext; + const uri = testAssetUri("command-plugin/Plugins/command-plugin/command-plugin.swift"); + + activateExtensionForSuite({ + async setup(ctx) { + // Open fixture and set focus to the plugin file under test + workspaceContext = ctx; + folderContext = await folderInRootWorkspace("command-plugin", workspaceContext); + await workspaceContext.focusFolder(folderContext); + await vscode.window.showTextDocument(uri); + + // These calls are needed for the command plugin command to show up. + // Otherwise will need to modified swift.disableAutoResolve config and a extension reload, + // which is not ideal. + await folderContext.loadSwiftPlugins(); + workspaceContext.updatePluginContextKey(); + }, + testAssets: ["command-plugin"], + }); + + test("Tasks: Run Task, swift-plugin", async () => { + // This will show a quick pick and is difficult to mock, since the tasks are already tested + // in the SwiftPluginTaskProvider integration test, just validate the arguments are valid. + const args = await vscode.commands.executeCommand(Commands.RUN_PLUGIN_TASK); + const tasks = await vscode.tasks.fetchTasks(args as vscode.TaskFilter); + + // Assert that there are exactly two tasks fetched + // One from tasks.json, another from the SwiftPluginTaskProvider + expect(tasks.length).to.equal(2); + + // Check if both tasks have the and names + const taskLabels = tasks.map(task => task.name); + const expectedLabels = [ + "swift-plugin: command-plugin", + "swift: command-plugin from tasks.json", + ]; + expect(taskLabels).to.deep.equal(expectedLabels); + }); +}); diff --git a/test/integration-tests/utilities/testutilities.ts b/test/integration-tests/utilities/testutilities.ts index a5f6b101b..48e9c657b 100644 --- a/test/integration-tests/utilities/testutilities.ts +++ b/test/integration-tests/utilities/testutilities.ts @@ -20,6 +20,7 @@ import { testAssetUri } from "../../fixtures"; import { WorkspaceContext } from "../../../src/WorkspaceContext"; import { FolderContext } from "../../../src/FolderContext"; import { waitForNoRunningTasks } from "../../utilities"; +import { Workbench } from "../../../src/utilities/commands"; function getRootWorkspaceFolder(): vscode.WorkspaceFolder { const result = vscode.workspace.workspaceFolders?.at(0); @@ -170,7 +171,7 @@ const extensionBootstrapper = (() => { await waitForNoRunningTasks({ timeout: 10000 }); // Close all editors before deactivating the extension. - await vscode.commands.executeCommand("workbench.action.closeAllEditors"); + await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS); await activatedAPI.workspaceContext?.removeWorkspaceFolder(getRootWorkspaceFolder()); await activatedAPI.deactivate();