Skip to content

Commit c394b7c

Browse files
Convert extensions/ql-vscode/src/helpers.ts to call typed commands
1 parent ddf5b3a commit c394b7c

4 files changed

Lines changed: 20 additions & 25 deletions

File tree

extensions/ql-vscode/src/common/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type BuiltInVsCodeCommands = {
4242
value: unknown,
4343
) => Promise<void>;
4444
"workbench.action.reloadWindow": () => Promise<void>;
45+
"vscode.openFolder": (uri: Uri) => Promise<void>;
4546
};
4647

4748
// Commands that are available before the extension is fully activated.

extensions/ql-vscode/src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ async function installOrUpdateThenTryActivate(
573573
await installOrUpdateDistribution(ctx, app, distributionManager, config);
574574

575575
try {
576-
await prepareCodeTour();
576+
await prepareCodeTour(app.commands);
577577
} catch (e: unknown) {
578578
void extLogger.log(
579579
`Could not open tutorial workspace automatically: ${getErrorMessage(e)}`,

extensions/ql-vscode/src/helpers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
window as Window,
1717
workspace,
1818
env,
19-
commands,
2019
} from "vscode";
2120
import { CodeQLCliServer, QlpacksInfo } from "./cli";
2221
import { UserCancellationException } from "./progress";
@@ -27,6 +26,7 @@ import { RedactableError } from "./pure/errors";
2726
import { getQlPackPath } from "./pure/ql";
2827
import { dbSchemeToLanguage } from "./common/query-language";
2928
import { isCodespacesTemplate } from "./config";
29+
import { AppCommandManager } from "./common/commands";
3030

3131
// Shared temporary folder for the extension.
3232
export const tmpDir = dirSync({
@@ -271,7 +271,9 @@ export function isFolderAlreadyInWorkspace(folderName: string) {
271271
/** Check if the current workspace is the CodeTour and open the workspace folder.
272272
* Without this, we can't run the code tour correctly.
273273
**/
274-
export async function prepareCodeTour(): Promise<void> {
274+
export async function prepareCodeTour(
275+
commandManager: AppCommandManager,
276+
): Promise<void> {
275277
if (workspace.workspaceFolders?.length) {
276278
const currentFolder = workspace.workspaceFolders[0].uri.fsPath;
277279

@@ -308,7 +310,7 @@ export async function prepareCodeTour(): Promise<void> {
308310
`In prepareCodeTour() method, going to open the tutorial workspace file: ${tutorialWorkspacePath}`,
309311
);
310312

311-
await commands.executeCommand("vscode.openFolder", tutorialWorkspaceUri);
313+
await commandManager.execute("vscode.openFolder", tutorialWorkspaceUri);
312314
}
313315
}
314316
}

extensions/ql-vscode/test/vscode-tests/no-workspace/helpers.test.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
commands,
32
EnvironmentVariableCollection,
43
EnvironmentVariableMutator,
54
Event,
@@ -41,6 +40,7 @@ import {
4140
import { reportStreamProgress } from "../../../src/progress";
4241
import { QueryLanguage } from "../../../src/common/query-language";
4342
import { Setting } from "../../../src/config";
43+
import { createMockCommandManager } from "../../__mocks__/commandsMock";
4444

4545
describe("helpers", () => {
4646
describe("Invocation rate limiter", () => {
@@ -612,13 +612,11 @@ describe("prepareCodeTour", () => {
612612
await mkdir(tourDirPath);
613613

614614
// spy that we open the workspace file by calling the 'vscode.openFolder' command
615-
const commandSpy = jest.spyOn(commands, "executeCommand");
616-
commandSpy.mockImplementation(() => Promise.resolve());
617-
618-
await prepareCodeTour();
615+
const executeCommand = jest.fn();
616+
await prepareCodeTour(createMockCommandManager({ executeCommand }));
619617

620618
expect(showInformationMessageSpy).toHaveBeenCalled();
621-
expect(commandSpy).toHaveBeenCalledWith(
619+
expect(executeCommand).toHaveBeenCalledWith(
622620
"vscode.openFolder",
623621
expect.objectContaining({
624622
path: Uri.parse(tutorialWorkspacePath).fsPath,
@@ -641,12 +639,10 @@ describe("prepareCodeTour", () => {
641639
await mkdir(tourDirPath);
642640

643641
// spy that we open the workspace file by calling the 'vscode.openFolder' command
644-
const commandSpy = jest.spyOn(commands, "executeCommand");
645-
commandSpy.mockImplementation(() => Promise.resolve());
646-
647-
await prepareCodeTour();
642+
const executeCommand = jest.fn();
643+
await prepareCodeTour(createMockCommandManager({ executeCommand }));
648644

649-
expect(commandSpy).not.toHaveBeenCalled();
645+
expect(executeCommand).not.toHaveBeenCalled();
650646
});
651647
});
652648
});
@@ -658,24 +654,20 @@ describe("prepareCodeTour", () => {
658654
await mkdir(tourDirPath);
659655

660656
// spy that we open the workspace file by calling the 'vscode.openFolder' command
661-
const commandSpy = jest.spyOn(commands, "executeCommand");
662-
commandSpy.mockImplementation(() => Promise.resolve());
657+
const executeCommand = jest.fn();
658+
await prepareCodeTour(createMockCommandManager({ executeCommand }));
663659

664-
await prepareCodeTour();
665-
666-
expect(commandSpy).not.toHaveBeenCalled();
660+
expect(executeCommand).not.toHaveBeenCalled();
667661
});
668662
});
669663

670664
describe("if we're in a different repo with no tour", () => {
671665
it("should not open the tutorial workspace", async () => {
672666
// spy that we open the workspace file by calling the 'vscode.openFolder' command
673-
const commandSpy = jest.spyOn(commands, "executeCommand");
674-
commandSpy.mockImplementation(() => Promise.resolve());
675-
676-
await prepareCodeTour();
667+
const executeCommand = jest.fn();
668+
await prepareCodeTour(createMockCommandManager({ executeCommand }));
677669

678-
expect(commandSpy).not.toHaveBeenCalled();
670+
expect(executeCommand).not.toHaveBeenCalled();
679671
});
680672
});
681673
});

0 commit comments

Comments
 (0)