Skip to content

Commit c84c69f

Browse files
Convert extensions/ql-vscode/src/vscode-utils/external-files.ts to call typed commands
1 parent a9e4951 commit c84c69f

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

extensions/ql-vscode/src/query-history/query-history-manager.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ export class QueryHistoryManager extends DisposableObject {
680680
}
681681

682682
if (singleItem.completedQuery.logFileLocation) {
683-
await tryOpenExternalFile(singleItem.completedQuery.logFileLocation);
683+
await tryOpenExternalFile(
684+
this.app.commands,
685+
singleItem.completedQuery.logFileLocation,
686+
);
684687
} else {
685688
void showAndLogWarningMessage("No log file available");
686689
}
@@ -797,7 +800,10 @@ export class QueryHistoryManager extends DisposableObject {
797800
}
798801

799802
if (finalSingleItem.evalLogLocation) {
800-
await tryOpenExternalFile(finalSingleItem.evalLogLocation);
803+
await tryOpenExternalFile(
804+
this.app.commands,
805+
finalSingleItem.evalLogLocation,
806+
);
801807
} else {
802808
this.warnNoEvalLogs();
803809
}
@@ -822,7 +828,10 @@ export class QueryHistoryManager extends DisposableObject {
822828
}
823829

824830
if (finalSingleItem.evalLogSummaryLocation) {
825-
await tryOpenExternalFile(finalSingleItem.evalLogSummaryLocation);
831+
await tryOpenExternalFile(
832+
this.app.commands,
833+
finalSingleItem.evalLogSummaryLocation,
834+
);
826835
return;
827836
}
828837

@@ -965,7 +974,10 @@ export class QueryHistoryManager extends DisposableObject {
965974
const query = finalSingleItem.completedQuery.query;
966975
const hasInterpretedResults = query.canHaveInterpretedResults();
967976
if (hasInterpretedResults) {
968-
await tryOpenExternalFile(query.resultsPaths.interpretedResultsPath);
977+
await tryOpenExternalFile(
978+
this.app.commands,
979+
query.resultsPaths.interpretedResultsPath,
980+
);
969981
} else {
970982
const label = this.labelProvider.getLabel(finalSingleItem);
971983
void showAndLogInformationMessage(
@@ -994,11 +1006,11 @@ export class QueryHistoryManager extends DisposableObject {
9941006
}
9951007
const query = finalSingleItem.completedQuery.query;
9961008
if (await query.hasCsv()) {
997-
void tryOpenExternalFile(query.csvPath);
1009+
void tryOpenExternalFile(this.app.commands, query.csvPath);
9981010
return;
9991011
}
10001012
if (await query.exportCsvResults(this.qs.cliServer, query.csvPath)) {
1001-
void tryOpenExternalFile(query.csvPath);
1013+
void tryOpenExternalFile(this.app.commands, query.csvPath);
10021014
}
10031015
}
10041016

@@ -1022,6 +1034,7 @@ export class QueryHistoryManager extends DisposableObject {
10221034
}
10231035

10241036
await tryOpenExternalFile(
1037+
this.app.commands,
10251038
await finalSingleItem.completedQuery.query.ensureCsvAlerts(
10261039
this.qs.cliServer,
10271040
this.dbm,
@@ -1049,6 +1062,7 @@ export class QueryHistoryManager extends DisposableObject {
10491062
}
10501063

10511064
await tryOpenExternalFile(
1065+
this.app.commands,
10521066
await finalSingleItem.completedQuery.query.ensureDilPath(
10531067
this.qs.cliServer,
10541068
),

extensions/ql-vscode/src/vscode-utils/external-files.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { commands, Uri, window } from "vscode";
1+
import { Uri, window } from "vscode";
2+
import { AppCommandManager } from "../common/commands";
23
import {
34
showAndLogExceptionWithTelemetry,
45
showBinaryChoiceDialog,
56
} from "../helpers";
67
import { redactableError } from "../pure/errors";
78
import { asError, getErrorMessage, getErrorStack } from "../pure/helpers-pure";
89

9-
export async function tryOpenExternalFile(fileLocation: string) {
10+
export async function tryOpenExternalFile(
11+
CommandManager: AppCommandManager,
12+
fileLocation: string,
13+
) {
1014
const uri = Uri.file(fileLocation);
1115
try {
1216
await window.showTextDocument(uri, { preview: false });
@@ -25,7 +29,7 @@ the file in the file explorer and dragging it into the workspace.`,
2529
);
2630
if (res) {
2731
try {
28-
await commands.executeCommand("revealFileInOS", uri);
32+
await CommandManager.execute("revealFileInOS", uri);
2933
} catch (e) {
3034
void showAndLogExceptionWithTelemetry(
3135
redactableError(
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22
import { tryOpenExternalFile } from "../../../../../src/vscode-utils/external-files";
3+
import { createMockCommandManager } from "../../../../__mocks__/commandsMock";
34
import { mockedObject } from "../../../utils/mocking.helpers";
45

56
describe("tryOpenExternalFile", () => {
@@ -9,9 +10,6 @@ describe("tryOpenExternalFile", () => {
910
let showInformationMessageSpy: jest.SpiedFunction<
1011
typeof vscode.window.showInformationMessage
1112
>;
12-
let executeCommandSpy: jest.SpiedFunction<
13-
typeof vscode.commands.executeCommand
14-
>;
1513

1614
beforeEach(() => {
1715
showTextDocumentSpy = jest
@@ -20,46 +18,52 @@ describe("tryOpenExternalFile", () => {
2018
showInformationMessageSpy = jest
2119
.spyOn(vscode.window, "showInformationMessage")
2220
.mockResolvedValue(undefined);
23-
executeCommandSpy = jest
24-
.spyOn(vscode.commands, "executeCommand")
25-
.mockResolvedValue(undefined);
2621
});
2722

2823
it("should open an external file", async () => {
29-
await tryOpenExternalFile("xxx");
24+
const executeCommand = jest.fn();
25+
const commandManager = createMockCommandManager({ executeCommand });
26+
27+
await tryOpenExternalFile(commandManager, "xxx");
3028
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
3129
expect(showTextDocumentSpy).toHaveBeenCalledWith(
3230
vscode.Uri.file("xxx"),
3331
expect.anything(),
3432
);
35-
expect(executeCommandSpy).not.toBeCalled();
33+
expect(executeCommand).not.toBeCalled();
3634
});
3735

3836
[
3937
"too large to open",
4038
"Files above 50MB cannot be synchronized with extensions",
4139
].forEach((msg) => {
4240
it(`should fail to open a file because "${msg}" and open externally`, async () => {
41+
const executeCommand = jest.fn();
42+
const commandManager = createMockCommandManager({ executeCommand });
43+
4344
showTextDocumentSpy.mockRejectedValue(new Error(msg));
4445
showInformationMessageSpy.mockResolvedValue({ title: "Yes" });
4546

46-
await tryOpenExternalFile("xxx");
47+
await tryOpenExternalFile(commandManager, "xxx");
4748
const uri = vscode.Uri.file("xxx");
4849
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
4950
expect(showTextDocumentSpy).toHaveBeenCalledWith(uri, expect.anything());
50-
expect(executeCommandSpy).toHaveBeenCalledWith("revealFileInOS", uri);
51+
expect(executeCommand).toHaveBeenCalledWith("revealFileInOS", uri);
5152
});
5253

5354
it(`should fail to open a file because "${msg}" and NOT open externally`, async () => {
55+
const executeCommand = jest.fn();
56+
const commandManager = createMockCommandManager({ executeCommand });
57+
5458
showTextDocumentSpy.mockRejectedValue(new Error(msg));
5559
showInformationMessageSpy.mockResolvedValue({ title: "No" });
5660

57-
await tryOpenExternalFile("xxx");
61+
await tryOpenExternalFile(commandManager, "xxx");
5862
const uri = vscode.Uri.file("xxx");
5963
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
6064
expect(showTextDocumentSpy).toHaveBeenCalledWith(uri, expect.anything());
6165
expect(showInformationMessageSpy).toBeCalled();
62-
expect(executeCommandSpy).not.toBeCalled();
66+
expect(executeCommand).not.toBeCalled();
6367
});
6468
});
6569
});

0 commit comments

Comments
 (0)