Skip to content

Commit 6a20c9c

Browse files
authored
fix: use wrapper functions for easier testing (#24941)
Fixes #24426
1 parent 41e6624 commit 6a20c9c

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/client/common/vscodeApis/commandApis.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
import { commands, Disposable } from 'vscode';
55

6-
/* eslint-disable @typescript-eslint/no-explicit-any */
7-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
6+
/**
7+
* Wrapper for vscode.commands.executeCommand to make it easier to mock in tests
8+
*/
9+
export function executeCommand<T>(command: string, ...rest: any[]): Thenable<T> {
10+
return commands.executeCommand<T>(command, ...rest);
11+
}
812

13+
/**
14+
* Wrapper for vscode.commands.registerCommand to make it easier to mock in tests
15+
*/
916
export function registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): Disposable {
1017
return commands.registerCommand(command, callback, thisArg);
1118
}
12-
13-
export function executeCommand<T = unknown>(command: string, ...rest: any[]): Thenable<T> {
14-
return commands.executeCommand(command, ...rest);
15-
}

src/client/common/vscodeApis/windowApis.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import {
2222
LogOutputChannel,
2323
OutputChannel,
2424
TerminalLinkProvider,
25+
NotebookDocument,
26+
NotebookEditor,
27+
NotebookDocumentShowOptions,
2528
} from 'vscode';
2629
import { createDeferred, Deferred } from '../utils/async';
2730
import { Resource } from '../types';
@@ -31,6 +34,13 @@ export function showTextDocument(uri: Uri): Thenable<TextEditor> {
3134
return window.showTextDocument(uri);
3235
}
3336

37+
export function showNotebookDocument(
38+
document: NotebookDocument,
39+
options?: NotebookDocumentShowOptions,
40+
): Thenable<NotebookEditor> {
41+
return window.showNotebookDocument(document, options);
42+
}
43+
3444
export function showQuickPick<T extends QuickPickItem>(
3545
items: readonly T[] | Thenable<readonly T[]>,
3646
options?: QuickPickOptions,

src/client/common/vscodeApis/workspaceApis.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ export function createDirectory(uri: vscode.Uri): Thenable<void> {
9898
return vscode.workspace.fs.createDirectory(uri);
9999
}
100100

101+
export function openNotebookDocument(uri: vscode.Uri): Thenable<vscode.NotebookDocument>;
102+
export function openNotebookDocument(
103+
notebookType: string,
104+
content?: vscode.NotebookData,
105+
): Thenable<vscode.NotebookDocument>;
106+
export function openNotebookDocument(notebook: any, content?: vscode.NotebookData): Thenable<vscode.NotebookDocument> {
107+
return vscode.workspace.openNotebookDocument(notebook, content);
108+
}
109+
101110
export function copy(source: vscode.Uri, dest: vscode.Uri, options?: { overwrite?: boolean }): Thenable<void> {
102111
return vscode.workspace.fs.copy(source, dest, options);
103112
}

src/client/repl/replCommandHandler.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import {
2-
commands,
3-
window,
42
NotebookController,
53
NotebookEditor,
64
ViewColumn,
@@ -9,11 +7,13 @@ import {
97
NotebookCellKind,
108
NotebookEdit,
119
WorkspaceEdit,
12-
workspace,
1310
Uri,
1411
} from 'vscode';
1512
import { getExistingReplViewColumn, getTabNameForUri } from './replUtils';
1613
import { PVSC_EXTENSION_ID } from '../common/constants';
14+
import { showNotebookDocument } from '../common/vscodeApis/windowApis';
15+
import { openNotebookDocument, applyEdit } from '../common/vscodeApis/workspaceApis';
16+
import { executeCommand } from '../common/vscodeApis/commandApis';
1717

1818
/**
1919
* Function that opens/show REPL using IW UI.
@@ -26,17 +26,17 @@ export async function openInteractiveREPL(
2626
let viewColumn = ViewColumn.Beside;
2727
if (notebookDocument instanceof Uri) {
2828
// Case where NotebookDocument is undefined, but workspace mementoURI exists.
29-
notebookDocument = await workspace.openNotebookDocument(notebookDocument);
29+
notebookDocument = await openNotebookDocument(notebookDocument);
3030
} else if (notebookDocument) {
3131
// Case where NotebookDocument (REPL document already exists in the tab)
3232
const existingReplViewColumn = getExistingReplViewColumn(notebookDocument);
3333
viewColumn = existingReplViewColumn ?? viewColumn;
3434
} else if (!notebookDocument) {
3535
// Case where NotebookDocument doesnt exist, or
3636
// became outdated (untitled.ipynb created without Python extension knowing, effectively taking over original Python REPL's URI)
37-
notebookDocument = await workspace.openNotebookDocument('jupyter-notebook');
37+
notebookDocument = await openNotebookDocument('jupyter-notebook');
3838
}
39-
const editor = await window.showNotebookDocument(notebookDocument!, {
39+
const editor = await showNotebookDocument(notebookDocument!, {
4040
viewColumn,
4141
asRepl: 'Python REPL',
4242
preserveFocus,
@@ -52,7 +52,7 @@ export async function openInteractiveREPL(
5252
return undefined;
5353
}
5454

55-
await commands.executeCommand('notebook.selectKernel', {
55+
await executeCommand('notebook.selectKernel', {
5656
editor,
5757
id: notebookController.id,
5858
extension: PVSC_EXTENSION_ID,
@@ -69,7 +69,7 @@ export async function selectNotebookKernel(
6969
notebookControllerId: string,
7070
extensionId: string,
7171
): Promise<void> {
72-
await commands.executeCommand('notebook.selectKernel', {
72+
await executeCommand('notebook.selectKernel', {
7373
notebookEditor,
7474
id: notebookControllerId,
7575
extension: extensionId,
@@ -84,7 +84,7 @@ export async function executeNotebookCell(notebookEditor: NotebookEditor, code:
8484
const cellIndex = replOptions?.appendIndex ?? notebook.cellCount;
8585
await addCellToNotebook(notebook, cellIndex, code);
8686
// Execute the cell
87-
commands.executeCommand('notebook.cell.execute', {
87+
executeCommand('notebook.cell.execute', {
8888
ranges: [{ start: cellIndex, end: cellIndex + 1 }],
8989
document: notebook.uri,
9090
});
@@ -100,5 +100,5 @@ async function addCellToNotebook(notebookDocument: NotebookDocument, index: numb
100100
const notebookEdit = NotebookEdit.insertCells(index, [notebookCellData]);
101101
const workspaceEdit = new WorkspaceEdit();
102102
workspaceEdit.set(notebookDocument!.uri, [notebookEdit]);
103-
await workspace.applyEdit(workspaceEdit);
103+
await applyEdit(workspaceEdit);
104104
}

0 commit comments

Comments
 (0)