Skip to content

fix: use wrapper functions for easier testing #24941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/client/common/vscodeApis/commandApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

import { commands, Disposable } from 'vscode';

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

/**
* Wrapper for vscode.commands.registerCommand to make it easier to mock in tests
*/
export function registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): Disposable {
return commands.registerCommand(command, callback, thisArg);
}

export function executeCommand<T = unknown>(command: string, ...rest: any[]): Thenable<T> {
return commands.executeCommand(command, ...rest);
}
10 changes: 10 additions & 0 deletions src/client/common/vscodeApis/windowApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import {
LogOutputChannel,
OutputChannel,
TerminalLinkProvider,
NotebookDocument,
NotebookEditor,
NotebookDocumentShowOptions,
} from 'vscode';
import { createDeferred, Deferred } from '../utils/async';
import { Resource } from '../types';
Expand All @@ -31,6 +34,13 @@ export function showTextDocument(uri: Uri): Thenable<TextEditor> {
return window.showTextDocument(uri);
}

export function showNotebookDocument(
document: NotebookDocument,
options?: NotebookDocumentShowOptions,
): Thenable<NotebookEditor> {
return window.showNotebookDocument(document, options);
}

export function showQuickPick<T extends QuickPickItem>(
items: readonly T[] | Thenable<readonly T[]>,
options?: QuickPickOptions,
Expand Down
9 changes: 9 additions & 0 deletions src/client/common/vscodeApis/workspaceApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ export function createDirectory(uri: vscode.Uri): Thenable<void> {
return vscode.workspace.fs.createDirectory(uri);
}

export function openNotebookDocument(uri: vscode.Uri): Thenable<vscode.NotebookDocument>;
export function openNotebookDocument(
notebookType: string,
content?: vscode.NotebookData,
): Thenable<vscode.NotebookDocument>;
export function openNotebookDocument(notebook: any, content?: vscode.NotebookData): Thenable<vscode.NotebookDocument> {
return vscode.workspace.openNotebookDocument(notebook, content);
}

export function copy(source: vscode.Uri, dest: vscode.Uri, options?: { overwrite?: boolean }): Thenable<void> {
return vscode.workspace.fs.copy(source, dest, options);
}
20 changes: 10 additions & 10 deletions src/client/repl/replCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
commands,
window,
NotebookController,
NotebookEditor,
ViewColumn,
Expand All @@ -9,11 +7,13 @@ import {
NotebookCellKind,
NotebookEdit,
WorkspaceEdit,
workspace,
Uri,
} from 'vscode';
import { getExistingReplViewColumn, getTabNameForUri } from './replUtils';
import { PVSC_EXTENSION_ID } from '../common/constants';
import { showNotebookDocument } from '../common/vscodeApis/windowApis';
import { openNotebookDocument, applyEdit } from '../common/vscodeApis/workspaceApis';
import { executeCommand } from '../common/vscodeApis/commandApis';

/**
* Function that opens/show REPL using IW UI.
Expand All @@ -26,17 +26,17 @@ export async function openInteractiveREPL(
let viewColumn = ViewColumn.Beside;
if (notebookDocument instanceof Uri) {
// Case where NotebookDocument is undefined, but workspace mementoURI exists.
notebookDocument = await workspace.openNotebookDocument(notebookDocument);
notebookDocument = await openNotebookDocument(notebookDocument);
} else if (notebookDocument) {
// Case where NotebookDocument (REPL document already exists in the tab)
const existingReplViewColumn = getExistingReplViewColumn(notebookDocument);
viewColumn = existingReplViewColumn ?? viewColumn;
} else if (!notebookDocument) {
// Case where NotebookDocument doesnt exist, or
// became outdated (untitled.ipynb created without Python extension knowing, effectively taking over original Python REPL's URI)
notebookDocument = await workspace.openNotebookDocument('jupyter-notebook');
notebookDocument = await openNotebookDocument('jupyter-notebook');
}
const editor = await window.showNotebookDocument(notebookDocument!, {
const editor = await showNotebookDocument(notebookDocument!, {
viewColumn,
asRepl: 'Python REPL',
preserveFocus,
Expand All @@ -52,7 +52,7 @@ export async function openInteractiveREPL(
return undefined;
}

await commands.executeCommand('notebook.selectKernel', {
await executeCommand('notebook.selectKernel', {
editor,
id: notebookController.id,
extension: PVSC_EXTENSION_ID,
Expand All @@ -69,7 +69,7 @@ export async function selectNotebookKernel(
notebookControllerId: string,
extensionId: string,
): Promise<void> {
await commands.executeCommand('notebook.selectKernel', {
await executeCommand('notebook.selectKernel', {
notebookEditor,
id: notebookControllerId,
extension: extensionId,
Expand All @@ -84,7 +84,7 @@ export async function executeNotebookCell(notebookEditor: NotebookEditor, code:
const cellIndex = replOptions?.appendIndex ?? notebook.cellCount;
await addCellToNotebook(notebook, cellIndex, code);
// Execute the cell
commands.executeCommand('notebook.cell.execute', {
executeCommand('notebook.cell.execute', {
ranges: [{ start: cellIndex, end: cellIndex + 1 }],
document: notebook.uri,
});
Expand All @@ -100,5 +100,5 @@ async function addCellToNotebook(notebookDocument: NotebookDocument, index: numb
const notebookEdit = NotebookEdit.insertCells(index, [notebookCellData]);
const workspaceEdit = new WorkspaceEdit();
workspaceEdit.set(notebookDocument!.uri, [notebookEdit]);
await workspace.applyEdit(workspaceEdit);
await applyEdit(workspaceEdit);
}
Loading