1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as sinon from 'sinon';
import { Event } from 'vs/base/common/event';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { mock } from 'vs/base/test/common/mock';
import { assertThrowsAsync } from 'vs/base/test/common/utils';
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { insertCellAtIndex } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
import { NotebookExecutionService } from 'vs/workbench/contrib/notebook/browser/notebookExecutionServiceImpl';
import { NotebookKernelService } from 'vs/workbench/contrib/notebook/browser/notebookKernelServiceImpl';
import { NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModelImpl';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { CellKind, IOutputDto, NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
import { INotebookKernel, INotebookKernelService, ISelectedNotebooksChangeEvent } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { setupInstantiationService, withTestNotebook as _withTestNotebook } from 'vs/workbench/contrib/notebook/test/browser/testNotebookEditor';
suite('NotebookExecutionService', () => {
let instantiationService: TestInstantiationService;
let kernelService: INotebookKernelService;
let disposables: DisposableStore;
setup(function () {
disposables = new DisposableStore();
instantiationService = setupInstantiationService(disposables);
instantiationService.stub(INotebookService, new class extends mock<INotebookService>() {
override onDidAddNotebookDocument = Event.None;
override onWillRemoveNotebookDocument = Event.None;
override getNotebookTextModels() { return []; }
});
kernelService = instantiationService.createInstance(NotebookKernelService);
instantiationService.set(INotebookKernelService, kernelService);
});
teardown(() => {
disposables.dispose();
});
async function withTestNotebook(cells: [string, string, CellKind, IOutputDto[], NotebookCellMetadata][], callback: (viewModel: NotebookViewModel, textModel: NotebookTextModel) => void | Promise<void>) {
return _withTestNotebook(cells, (editor, viewModel) => callback(viewModel, viewModel.notebookDocument));
}
// test('ctor', () => {
// instantiationService.createInstance(NotebookEditorKernelManager, { activeKernel: undefined, viewModel: undefined });
// const contextKeyService = instantiationService.get(IContextKeyService);
// assert.strictEqual(contextKeyService.getContextKeyValue(NOTEBOOK_KERNEL_COUNT.key), 0);
// });
test('cell is not runnable when no kernel is selected', async () => {
await withTestNotebook(
[],
async (viewModel) => {
const executionService = instantiationService.createInstance(NotebookExecutionService);
const cell = insertCellAtIndex(viewModel, 1, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
await assertThrowsAsync(async () => await executionService.executeNotebookCell(cell));
});
});
test('cell is not runnable when kernel does not support the language', async () => {
await withTestNotebook(
[],
async (viewModel) => {
kernelService.registerKernel(new TestNotebookKernel({ languages: ['testlang'] }));
const executionService = instantiationService.createInstance(NotebookExecutionService);
const cell = insertCellAtIndex(viewModel, 1, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
await assertThrowsAsync(async () => await executionService.executeNotebookCell(cell));
});
});
test('cell is runnable when kernel does support the language', async () => {
await withTestNotebook(
[],
async (viewModel) => {
const kernel = new TestNotebookKernel({ languages: ['javascript'] });
kernelService.registerKernel(kernel);
const executionService = instantiationService.createInstance(NotebookExecutionService);
const executeSpy = sinon.spy();
kernel.executeNotebookCellsRequest = executeSpy;
const cell = insertCellAtIndex(viewModel, 0, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
await executionService.executeNotebookCells(viewModel.notebookDocument, [cell]);
assert.strictEqual(executeSpy.calledOnce, true);
});
});
test('select kernel when running cell', async function () {
// https://github.com/microsoft/vscode/issues/121904
return withTestNotebook([], async viewModel => {
assert.strictEqual(kernelService.getMatchingKernel(viewModel.notebookDocument).all.length, 0);
let didExecute = false;
const kernel = new class extends TestNotebookKernel {
constructor() {
super({ languages: ['javascript'] });
this.id = 'mySpecialId';
}
override async executeNotebookCellsRequest() {
didExecute = true;
return;
}
};
kernelService.registerKernel(kernel);
const executionService = instantiationService.createInstance(NotebookExecutionService);
let event: ISelectedNotebooksChangeEvent | undefined;
kernelService.onDidChangeSelectedNotebooks(e => event = e);
const cell = insertCellAtIndex(viewModel, 0, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
await executionService.executeNotebookCells(viewModel.notebookDocument, [cell]);
assert.strictEqual(didExecute, true);
assert.ok(event !== undefined);
assert.strictEqual(event.newKernel, kernel.id);
assert.strictEqual(event.oldKernel, undefined);
});
});
test('Completes unconfirmed executions', async function () {
return withTestNotebook([], async viewModel => {
let didExecute = false;
const kernel = new class extends TestNotebookKernel {
constructor() {
super({ languages: ['javascript'] });
this.id = 'mySpecialId';
}
override async executeNotebookCellsRequest() {
didExecute = true;
return;
}
};
kernelService.registerKernel(kernel);
const executionService = instantiationService.createInstance(NotebookExecutionService);
const exeStateService = instantiationService.get(INotebookExecutionStateService);
const cell = insertCellAtIndex(viewModel, 0, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
await executionService.executeNotebookCells(viewModel.notebookDocument, [cell]);
assert.strictEqual(didExecute, true);
assert.strictEqual(exeStateService.getCellExecution(cell.uri), undefined);
});
});
});
class TestNotebookKernel implements INotebookKernel {
id: string = 'test';
label: string = '';
viewType = '*';
onDidChange = Event.None;
extension: ExtensionIdentifier = new ExtensionIdentifier('test');
localResourceRoot: URI = URI.file('/test');
description?: string | undefined;
detail?: string | undefined;
preloadUris: URI[] = [];
preloadProvides: string[] = [];
supportedLanguages: string[] = [];
executeNotebookCellsRequest(): Promise<void> {
throw new Error('Method not implemented.');
}
cancelNotebookCellExecution(): Promise<void> {
throw new Error('Method not implemented.');
}
constructor(opts?: { languages: string[] }) {
this.supportedLanguages = opts?.languages ?? [PLAINTEXT_LANGUAGE_ID];
}
}
|