Skip to content

Commit

Permalink
Add integration test for swift.runPluginTask command
Browse files Browse the repository at this point in the history
- Change runPluginTask to return the argument filter for better testability
- Given SwiftPluginTaskProvider integration test already test the tasks,
assert the command arguments are valid and return the right tasks for
picking.

Issue: #1230
  • Loading branch information
michael-weng committed Nov 27, 2024
1 parent c466b00 commit 51d270c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .vscode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ module.exports = defineConfig({
color: true,
timeout,
forbidOnly: isCIBuild,
grep: isFastTestRun ? "@slow" : undefined,
invert: isFastTestRun,
// grep: isFastTestRun ? "@slow" : undefined,
// invert: isFastTestRun,
grep: "swift-plugin",
slow: 10000,
reporter: path.join(__dirname, ".mocha-reporter.js"),
reporterOptions: {
Expand Down
3 changes: 2 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down Expand Up @@ -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()
),
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runPluginTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 0 additions & 5 deletions test/integration-tests/BackgroundCompilation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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<void>(res => {
vscode.tasks.onDidStartTask(e => {
Expand Down
3 changes: 0 additions & 3 deletions test/integration-tests/commands/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
63 changes: 63 additions & 0 deletions test/integration-tests/commands/runPluginTask.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
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";
import { Workbench } from "../../../src/utilities/commands";

function getRootWorkspaceFolder(): vscode.WorkspaceFolder {
const result = vscode.workspace.workspaceFolders?.at(0);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 51d270c

Please sign in to comment.