Skip to content

Commit a9f5aa8

Browse files
committed
Restructure to make more testable
1 parent 21c0a6c commit a9f5aa8

File tree

3 files changed

+77
-57
lines changed

3 files changed

+77
-57
lines changed

src/commands.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,13 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
148148
Commands.DEBUG,
149149
async target => await debugBuild(ctx, ...unwrapTreeItem(target))
150150
),
151-
vscode.commands.registerCommand(
152-
Commands.PLAY,
153-
async target => await runPlayground(ctx, target)
154-
),
151+
vscode.commands.registerCommand(Commands.PLAY, async target => {
152+
const folder = ctx.currentFolder;
153+
if (!folder) {
154+
return false;
155+
}
156+
return await runPlayground(folder, ctx.tasks, target);
157+
}),
155158
vscode.commands.registerCommand(Commands.CLEAN_BUILD, async () => await cleanBuild(ctx)),
156159
vscode.commands.registerCommand(
157160
Commands.RUN_TESTS_MULTIPLE_TIMES,

src/commands/runPlayground.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
import * as vscode from "vscode";
1515
import { Location, Range } from "vscode-languageclient";
1616

17-
import { WorkspaceContext } from "../WorkspaceContext";
17+
import { FolderContext } from "../FolderContext";
1818
import { createSwiftTask } from "../tasks/SwiftTaskProvider";
19+
import { TaskManager } from "../tasks/TaskManager";
1920
import { packageName } from "../utilities/tasks";
2021

2122
export interface PlaygroundItem {
@@ -34,9 +35,12 @@ export interface WorkspacePlaygroundItem extends PlaygroundItem {
3435
/**
3536
* Executes a {@link vscode.Task task} to run swift playground.
3637
*/
37-
export async function runPlayground(ctx: WorkspaceContext, item?: PlaygroundItem) {
38-
const folderContext = ctx.currentFolder;
39-
if (!folderContext || !item) {
38+
export async function runPlayground(
39+
folderContext: FolderContext,
40+
tasks: TaskManager,
41+
item?: PlaygroundItem
42+
) {
43+
if (!item) {
4044
return false;
4145
}
4246
const id = item.label ?? item.id;
@@ -52,6 +56,6 @@ export async function runPlayground(ctx: WorkspaceContext, item?: PlaygroundItem
5256
folderContext.toolchain
5357
);
5458

55-
await vscode.tasks.executeTask(task);
59+
await tasks.executeTaskAndWait(task);
5660
return true;
5761
}

test/integration-tests/commands/runPlayground.test.ts

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
import { expect } from "chai";
15+
import { stub } from "sinon";
1516
import * as vscode from "vscode";
1617

1718
import { FolderContext } from "@src/FolderContext";
1819
import { WorkspaceContext } from "@src/WorkspaceContext";
1920
import { Commands } from "@src/commands";
21+
import { runPlayground } from "@src/commands/runPlayground";
2022
import { SwiftTask } from "@src/tasks/SwiftTaskProvider";
23+
import { TaskManager } from "@src/tasks/TaskManager";
2124

22-
import { mockGlobalObject } from "../../MockUtils";
25+
import { MockedObject, instance, mockObject } from "../../MockUtils";
2326
import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities";
2427

2528
suite("Run Playground Command", function () {
2629
let folderContext: FolderContext;
2730
let workspaceContext: WorkspaceContext;
28-
29-
const mockTasks = mockGlobalObject(vscode, "tasks");
31+
let mockTaskManager: MockedObject<TaskManager>;
3032

3133
activateExtensionForSuite({
3234
async setup(ctx) {
@@ -37,60 +39,71 @@ suite("Run Playground Command", function () {
3739

3840
setup(async () => {
3941
await workspaceContext.focusFolder(folderContext);
42+
mockTaskManager = mockObject<TaskManager>({ executeTaskAndWait: stub().resolves() });
4043
});
4144

42-
test("No playground item provided", async () => {
43-
expect(await vscode.commands.executeCommand(Commands.PLAY), undefined).to.be.false;
44-
expect(mockTasks.executeTask).to.not.have.been.called;
45-
});
45+
suite("Command", () => {
46+
test("Succeeds", async () => {
47+
expect(
48+
await vscode.commands.executeCommand(Commands.PLAY, {
49+
id: "PackageLib/PackageLib.swift:3",
50+
})
51+
).to.be.true;
52+
});
53+
54+
test("No playground item provided", async () => {
55+
expect(await vscode.commands.executeCommand(Commands.PLAY), undefined).to.be.false;
56+
});
4657

47-
test("No folder focussed", async () => {
48-
await workspaceContext.focusFolder(null);
49-
expect(
50-
await vscode.commands.executeCommand(Commands.PLAY, {
51-
id: "PackageLib/PackageLib.swift:3",
52-
})
53-
).to.be.false;
54-
expect(mockTasks.executeTask).to.not.have.been.called;
58+
test("No folder focussed", async () => {
59+
await workspaceContext.focusFolder(null);
60+
expect(
61+
await vscode.commands.executeCommand(Commands.PLAY, {
62+
id: "PackageLib/PackageLib.swift:3",
63+
})
64+
).to.be.false;
65+
});
5566
});
5667

57-
test('Runs "swift play" on "id"', async () => {
58-
expect(
59-
await vscode.commands.executeCommand(Commands.PLAY, {
60-
id: "PackageLib/PackageLib.swift:3",
61-
})
62-
).to.be.true;
63-
expect(mockTasks.executeTask).to.have.been.calledOnce;
68+
suite("Arguments", () => {
69+
test('Runs "swift play" on "id"', async () => {
70+
expect(
71+
await runPlayground(folderContext, instance(mockTaskManager), {
72+
id: "PackageLib/PackageLib.swift:3",
73+
})
74+
).to.be.true;
75+
expect(mockTaskManager.executeTaskAndWait).to.have.been.calledOnce;
6476

65-
const task = mockTasks.executeTask.args[0][0] as SwiftTask;
66-
expect(task.execution.args).to.deep.equal(["play", "PackageLib/PackageLib.swift:3"]);
67-
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
68-
});
77+
const task = mockTaskManager.executeTaskAndWait.args[0][0] as SwiftTask;
78+
expect(task.execution.args).to.deep.equal(["play", "PackageLib/PackageLib.swift:3"]);
79+
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
80+
});
6981

70-
test('Runs "swift play" on "id" with space in path', async () => {
71-
expect(
72-
await vscode.commands.executeCommand(Commands.PLAY, {
73-
id: "PackageLib/Package Lib.swift:3",
74-
})
75-
).to.be.true;
76-
expect(mockTasks.executeTask).to.have.been.calledOnce;
82+
test('Runs "swift play" on "id" with space in path', async () => {
83+
expect(
84+
await runPlayground(folderContext, instance(mockTaskManager), {
85+
id: "PackageLib/Package Lib.swift:3",
86+
})
87+
).to.be.true;
88+
expect(mockTaskManager.executeTaskAndWait).to.have.been.calledOnce;
7789

78-
const task = mockTasks.executeTask.args[0][0] as SwiftTask;
79-
expect(task.execution.args).to.deep.equal(["play", "PackageLib/Package Lib.swift:3"]);
80-
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
81-
});
90+
const task = mockTaskManager.executeTaskAndWait.args[0][0] as SwiftTask;
91+
expect(task.execution.args).to.deep.equal(["play", "PackageLib/Package Lib.swift:3"]);
92+
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
93+
});
8294

83-
test('Runs "swift play" on "label"', async () => {
84-
expect(
85-
await vscode.commands.executeCommand(Commands.PLAY, {
86-
id: "PackageLib/PackageLib.swift:3",
87-
label: "bar",
88-
})
89-
).to.be.true;
90-
expect(mockTasks.executeTask).to.have.been.calledOnce;
95+
test('Runs "swift play" on "label"', async () => {
96+
expect(
97+
await runPlayground(folderContext, instance(mockTaskManager), {
98+
id: "PackageLib/PackageLib.swift:3",
99+
label: "bar",
100+
})
101+
).to.be.true;
102+
expect(mockTaskManager.executeTaskAndWait).to.have.been.calledOnce;
91103

92-
const task = mockTasks.executeTask.args[0][0] as SwiftTask;
93-
expect(task.execution.args).to.deep.equal(["play", "bar"]);
94-
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
104+
const task = mockTaskManager.executeTaskAndWait.args[0][0] as SwiftTask;
105+
expect(task.execution.args).to.deep.equal(["play", "bar"]);
106+
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
107+
});
95108
});
96109
});

0 commit comments

Comments
 (0)