Skip to content

Commit dc6dcb5

Browse files
committed
better sanity check, more test
1 parent 992c799 commit dc6dcb5

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/client/repl/nativeRepl.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,19 @@ export class NativeRepl implements Disposable {
169169
wsMementoUri,
170170
preserveFocus,
171171
);
172+
if (notebookEditor) {
173+
this.notebookDocument = notebookEditor.notebook;
174+
updateWorkspaceStateValue<string | undefined>(
175+
NATIVE_REPL_URI_MEMENTO,
176+
this.notebookDocument.uri.toString(),
177+
);
172178

173-
this.notebookDocument = notebookEditor.notebook;
174-
updateWorkspaceStateValue<string | undefined>(NATIVE_REPL_URI_MEMENTO, this.notebookDocument.uri.toString());
175-
176-
if (this.notebookDocument) {
177-
this.replController.updateNotebookAffinity(this.notebookDocument, NotebookControllerAffinity.Default);
178-
await selectNotebookKernel(notebookEditor, this.replController.id, PVSC_EXTENSION_ID);
179-
if (code) {
180-
await executeNotebookCell(notebookEditor, code);
179+
if (this.notebookDocument) {
180+
this.replController.updateNotebookAffinity(this.notebookDocument, NotebookControllerAffinity.Default);
181+
await selectNotebookKernel(notebookEditor, this.replController.id, PVSC_EXTENSION_ID);
182+
if (code) {
183+
await executeNotebookCell(notebookEditor, code);
184+
}
181185
}
182186
}
183187
}

src/client/repl/replCommandHandler.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
workspace,
1313
Uri,
1414
} from 'vscode';
15-
import { getExistingReplViewColumn } from './replUtils';
15+
import { getExistingReplViewColumn, getTabNameForUri } from './replUtils';
1616
import { PVSC_EXTENSION_ID } from '../common/constants';
1717

1818
/**
@@ -23,7 +23,7 @@ export async function openInteractiveREPL(
2323
notebookDocument: NotebookDocument | undefined,
2424
mementoValue: Uri | undefined,
2525
preserveFocus: boolean = true,
26-
): Promise<NotebookEditor> {
26+
): Promise<NotebookEditor | undefined> {
2727
let viewColumn = ViewColumn.Beside;
2828
if (mementoValue) {
2929
if (!notebookDocument) {
@@ -38,13 +38,22 @@ export async function openInteractiveREPL(
3838
// became outdated (untitled.ipynb created without Python extension knowing, effectively taking over original Python REPL's URI)
3939
notebookDocument = await workspace.openNotebookDocument('jupyter-notebook');
4040
}
41-
const editor = window.showNotebookDocument(notebookDocument!, {
41+
const editor = await window.showNotebookDocument(notebookDocument!, {
4242
viewColumn,
4343
asRepl: 'Python REPL',
4444
preserveFocus,
4545
});
46-
// sanity check that we opened a Native REPL from showNotebookDocument.
47-
// if not true, set notebook = undefined.
46+
47+
// Sanity check that we opened a Native REPL from showNotebookDocument.
48+
if (
49+
!editor ||
50+
!editor.notebook ||
51+
!editor.notebook.uri ||
52+
getTabNameForUri(editor.notebook.uri) !== 'Python REPL'
53+
) {
54+
return undefined;
55+
}
56+
4857
await commands.executeCommand('notebook.selectKernel', {
4958
editor,
5059
id: notebookController.id,

src/test/repl/nativeRepl.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ suite('REPL - Native REPL', () => {
1818
let setReplDirectoryStub: sinon.SinonStub;
1919
let setReplControllerSpy: sinon.SinonSpy;
2020
let getWorkspaceStateValueStub: sinon.SinonStub;
21+
let updateWorkspaceStateValueStub: sinon.SinonStub;
2122

2223
setup(() => {
2324
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
@@ -30,7 +31,7 @@ suite('REPL - Native REPL', () => {
3031
setReplDirectoryStub = sinon.stub(NativeRepl.prototype as any, 'setReplDirectory').resolves(); // Stubbing private method
3132
// Use a spy instead of a stub for setReplController
3233
setReplControllerSpy = sinon.spy(NativeRepl.prototype, 'setReplController');
33-
getWorkspaceStateValueStub = sinon.stub(persistentState, 'getWorkspaceStateValue').returns(undefined);
34+
updateWorkspaceStateValueStub = sinon.stub(persistentState, 'updateWorkspaceStateValue').resolves();
3435
});
3536

3637
teardown(() => {
@@ -55,6 +56,7 @@ suite('REPL - Native REPL', () => {
5556
});
5657

5758
test('sendToNativeRepl should look for memento URI if notebook document is undefined', async () => {
59+
getWorkspaceStateValueStub = sinon.stub(persistentState, 'getWorkspaceStateValue').returns(undefined);
5860
interpreterService
5961
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
6062
.returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment));
@@ -66,6 +68,19 @@ suite('REPL - Native REPL', () => {
6668
expect(getWorkspaceStateValueStub.calledOnce).to.be.true;
6769
});
6870

71+
test('sendToNativeRepl should call updateWorkspaceStateValue', async () => {
72+
getWorkspaceStateValueStub = sinon.stub(persistentState, 'getWorkspaceStateValue').returns('myNameIsMemento');
73+
interpreterService
74+
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
75+
.returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment));
76+
const interpreter = await interpreterService.object.getActiveInterpreter();
77+
const nativeRepl = await getNativeRepl(interpreter as PythonEnvironment, disposableArray);
78+
79+
nativeRepl.sendToNativeRepl(undefined, false);
80+
81+
expect(updateWorkspaceStateValueStub.calledOnce).to.be.true;
82+
});
83+
6984
test('create should call setReplDirectory, setReplController', async () => {
7085
const interpreter = await interpreterService.object.getActiveInterpreter();
7186
interpreterService

0 commit comments

Comments
 (0)