forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebook.functional.test.ts
More file actions
1499 lines (1368 loc) · 68 KB
/
notebook.functional.test.ts
File metadata and controls
1499 lines (1368 loc) · 68 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { nbformat } from '@jupyterlab/coreutils';
import { assert } from 'chai';
import { ChildProcess } from 'child_process';
import * as fs from 'fs-extra';
import { injectable } from 'inversify';
import * as os from 'os';
import * as path from 'path';
import { SemVer } from 'semver';
import { Readable, Writable } from 'stream';
import { anything, instance, mock, when } from 'ts-mockito';
import * as TypeMoq from 'typemoq';
import * as uuid from 'uuid/v4';
import { Disposable, Uri } from 'vscode';
import { CancellationToken, CancellationTokenSource } from 'vscode-jsonrpc';
import { ApplicationShell } from '../../client/common/application/applicationShell';
import { IApplicationShell } from '../../client/common/application/types';
import { Cancellation, CancellationError } from '../../client/common/cancellation';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { LocalZMQKernel } from '../../client/common/experiments/groups';
import { traceError, traceInfo } from '../../client/common/logger';
import { IFileSystem } from '../../client/common/platform/types';
import { IPythonExecutionFactory, IPythonExecutionService, Output } from '../../client/common/process/types';
import { Product } from '../../client/common/types';
import { createDeferred, waitForPromise } from '../../client/common/utils/async';
import { noop } from '../../client/common/utils/misc';
import { Architecture } from '../../client/common/utils/platform';
import { Identifiers } from '../../client/datascience/constants';
import { getMessageForLibrariesNotInstalled } from '../../client/datascience/jupyter/interpreter/jupyterInterpreterDependencyService';
import { JupyterExecutionFactory } from '../../client/datascience/jupyter/jupyterExecutionFactory';
import { JupyterKernelPromiseFailedError } from '../../client/datascience/jupyter/kernels/jupyterKernelPromiseFailedError';
import { HostJupyterNotebook } from '../../client/datascience/jupyter/liveshare/hostJupyterNotebook';
import {
CellState,
ICell,
IJupyterConnection,
IJupyterExecution,
IJupyterKernelSpec,
INotebook,
INotebookExecutionLogger,
INotebookExporter,
INotebookImporter,
INotebookProvider,
InterruptResult
} from '../../client/datascience/types';
import { IInterpreterService, IKnownSearchPathsForInterpreters } from '../../client/interpreter/contracts';
import { InterpreterType, PythonInterpreter } from '../../client/pythonEnvironments/discovery/types';
import { concatMultilineStringInput } from '../../datascience-ui/common';
import { generateTestState, ICellViewModel } from '../../datascience-ui/interactive-common/mainState';
import { sleep } from '../core';
import { DataScienceIocContainer } from './dataScienceIocContainer';
import { takeSnapshot, writeDiffSnapshot } from './helpers';
import { getIPConnectionInfo } from './jupyterHelpers';
import { SupportedCommands } from './mockJupyterManager';
import { MockPythonService } from './mockPythonService';
// tslint:disable:no-any no-multiline-string max-func-body-length no-console max-classes-per-file trailing-comma
suite('DataScience notebook tests', () => {
[false, true].forEach((useRawKernel) => {
suite(`${useRawKernel ? 'With Direct Kernel' : 'With Jupyter Server'}`, () => {
const disposables: Disposable[] = [];
let notebookProvider: INotebookProvider;
let pythonFactory: IPythonExecutionFactory;
let ioc: DataScienceIocContainer;
let modifiedConfig = false;
const baseUri = Uri.file('foo.py');
let snapshot: any;
// tslint:disable-next-line: no-function-expression
setup(async function () {
ioc = new DataScienceIocContainer();
if (ioc.shouldMockJupyter && useRawKernel) {
// tslint:disable-next-line: no-invalid-this
this.skip();
return;
} else {
ioc.setExperimentState(LocalZMQKernel.experiment, useRawKernel);
}
ioc.registerDataScienceTypes();
await ioc.activate();
notebookProvider = ioc.get<INotebookProvider>(INotebookProvider);
pythonFactory = ioc.get<IPythonExecutionFactory>(IPythonExecutionFactory);
});
suiteSetup(() => {
snapshot = takeSnapshot();
});
suiteTeardown(() => {
writeDiffSnapshot(snapshot, `Notebook ${useRawKernel}`);
});
teardown(async () => {
try {
if (modifiedConfig) {
traceInfo('Attempting to put jupyter default config back');
const procService = await createPythonService();
if (procService) {
await procService.exec(['-m', 'jupyter', 'notebook', '--generate-config', '-y'], {});
}
}
traceInfo('Shutting down after test.');
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < disposables.length; i += 1) {
const disposable = disposables[i];
if (disposable) {
const promise = disposable.dispose() as Promise<any>;
if (promise) {
await promise;
}
}
}
await ioc.dispose();
traceInfo('Shutdown after test complete.');
} catch (e) {
traceError(e);
}
if (process.env.PYTHONWARNINGS) {
delete process.env.PYTHONWARNINGS;
}
});
function escapePath(p: string) {
return p.replace(/\\/g, '\\\\');
}
function srcDirectory() {
return path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'datascience');
}
function extractDataOutput(cell: ICell): any {
assert.equal(cell.data.cell_type, 'code', `Wrong type of cell returned`);
const codeCell = cell.data as nbformat.ICodeCell;
if (codeCell.outputs.length > 0) {
assert.equal(codeCell.outputs.length, 1, 'Cell length not correct');
const data = codeCell.outputs[0].data;
const error = codeCell.outputs[0].evalue;
if (error) {
assert.fail(`Unexpected error: ${error}`);
}
assert.ok(data, `No data object on the cell`);
if (data) {
// For linter
assert.ok(data.hasOwnProperty('text/plain'), `Cell mime type not correct`);
assert.ok((data as any)['text/plain'], `Cell mime type not correct`);
return (data as any)['text/plain'];
}
}
}
async function verifySimple(
notebook: INotebook | undefined,
code: string,
expectedValue: any,
pathVerify = false
): Promise<void> {
const cells = await notebook!.execute(code, path.join(srcDirectory(), 'foo.py'), 2, uuid());
assert.equal(cells.length, 1, `Wrong number of cells returned`);
const data = extractDataOutput(cells[0]);
if (pathVerify) {
// For a path comparison normalize output and add single quotes on expected value
const normalizedOutput = path.normalize(data).toUpperCase();
const normalizedTarget = `'${path.normalize(expectedValue).toUpperCase()}'`;
assert.equal(normalizedOutput, normalizedTarget, 'Cell path values does not match');
} else {
assert.equal(data, expectedValue, 'Cell value does not match');
}
}
async function verifyError(
notebook: INotebook | undefined,
code: string,
errorString: string
): Promise<void> {
const cells = await notebook!.execute(code, path.join(srcDirectory(), 'foo.py'), 2, uuid());
assert.equal(cells.length, 1, `Wrong number of cells returned`);
assert.equal(cells[0].data.cell_type, 'code', `Wrong type of cell returned`);
const cell = cells[0].data as nbformat.ICodeCell;
assert.equal(cell.outputs.length, 1, `Cell length not correct`);
const error = cell.outputs[0].evalue;
if (error) {
assert.ok(error, 'Error not found when expected');
assert.equal(error, errorString, 'Unexpected error found');
}
}
async function verifyCell(
notebook: INotebook | undefined,
index: number,
code: string,
mimeType: string,
cellType: string,
verifyValue: (data: any) => void
): Promise<void> {
// Verify results of an execute
const cells = await notebook!.execute(code, path.join(srcDirectory(), 'foo.py'), 2, uuid());
assert.equal(cells.length, 1, `${index}: Wrong number of cells returned`);
if (cellType === 'code') {
assert.equal(cells[0].data.cell_type, cellType, `${index}: Wrong type of cell returned`);
const cell = cells[0].data as nbformat.ICodeCell;
assert.ok(cell.outputs.length >= 1, `${index}: Cell length not correct`);
const error = cell.outputs[0].evalue;
if (error) {
assert.ok(false, `${index}: Unexpected error: ${error}`);
}
const data = cell.outputs[0].data;
const text = cell.outputs[0].text;
assert.ok(data || text, `${index}: No data object on the cell for ${code}`);
if (data) {
// For linter
assert.ok(
data.hasOwnProperty(mimeType) || data.hasOwnProperty('text/plain'),
`${index}: Cell mime type not correct for ${JSON.stringify(data)}`
);
const actualMimeType = data.hasOwnProperty(mimeType) ? mimeType : 'text/plain';
assert.ok((data as any)[actualMimeType], `${index}: Cell mime type not correct`);
verifyValue((data as any)[actualMimeType]);
}
if (text) {
verifyValue(text);
}
} else if (cellType === 'markdown') {
assert.equal(cells[0].data.cell_type, cellType, `${index}: Wrong type of cell returned`);
const cell = cells[0].data as nbformat.IMarkdownCell;
const outputSource = concatMultilineStringInput(cell.source);
verifyValue(outputSource);
} else if (cellType === 'error') {
const cell = cells[0].data as nbformat.ICodeCell;
assert.equal(cell.outputs.length, 1, `${index}: Cell length not correct`);
const error = cell.outputs[0].evalue;
assert.ok(error, 'Error not found when expected');
verifyValue(error);
}
}
function testMimeTypes(
types: {
markdownRegEx: string | undefined;
code: string;
mimeType: string;
result: any;
cellType: string;
verifyValue(data: any): void;
}[]
) {
runTest('MimeTypes', async () => {
// Prefill with the output (This is only necessary for mocking)
types.forEach((t) => {
addMockData(t.code, t.result, t.mimeType, t.cellType);
});
// Test all mime types together so we don't have to startup and shutdown between
// each
const server = await createNotebook();
if (server) {
for (let i = 0; i < types.length; i += 1) {
const markdownRegex = types[i].markdownRegEx ? types[i].markdownRegEx : '';
ioc.getSettings().datascience.markdownRegularExpression = markdownRegex!;
await verifyCell(
server,
i,
types[i].code,
types[i].mimeType,
types[i].cellType,
types[i].verifyValue
);
}
}
});
}
function runTest(
name: string,
func: (_this: Mocha.Context) => Promise<void>,
_notebookProc?: ChildProcess,
rebindFunc?: () => void
) {
test(name, async function () {
// Give tests a chance to rebind IOC services before we fetch jupyterExecution and processFactory
if (rebindFunc) {
rebindFunc();
}
console.log(`Starting test ${name} ...`);
// tslint:disable-next-line: no-invalid-this
return func(this);
});
}
async function createNotebookWithNonDefaultConfig(): Promise<INotebook | undefined> {
const newSettings = { ...ioc.getSettings().datascience, useDefaultConfig: false };
ioc.forceSettingsChanged(undefined, ioc.getSettings().pythonPath, newSettings);
return createNotebook();
}
async function createNotebook(
uri?: string,
launchingFile?: string,
expectFailure?: boolean
): Promise<INotebook | undefined> {
// Catch exceptions. Throw a specific assertion if the promise fails
try {
if (uri) {
const newSettings = { ...ioc.getSettings().datascience, jupyterServerURI: uri };
ioc.forceSettingsChanged(undefined, ioc.getSettings().pythonPath, newSettings);
}
const notebook = await notebookProvider.getOrCreateNotebook({
identity: Uri.parse(Identifiers.InteractiveWindowIdentity)
});
launchingFile = launchingFile || path.join(srcDirectory(), 'foo.py');
if (notebook) {
await notebook.setLaunchingFile(launchingFile);
}
return notebook;
} catch (exc) {
if (!expectFailure) {
assert.ok(false, `Expected server to be created, but got ${exc}`);
}
}
}
function addMockData(code: string, result: string | number, mimeType?: string, cellType?: string) {
if (ioc.mockJupyter) {
if (cellType && cellType === 'error') {
ioc.mockJupyter.addError(code, result.toString());
} else {
ioc.mockJupyter.addCell(code, result, mimeType);
}
}
}
function changeMockWorkingDirectory(workingDir: string) {
if (ioc.mockJupyter) {
ioc.mockJupyter.changeWorkingDirectory(workingDir);
}
}
function addInterruptableMockData(
code: string,
resultGenerator: (c: CancellationToken) => Promise<{ result: string; haveMore: boolean }>
) {
if (ioc.mockJupyter) {
ioc.mockJupyter.addContinuousOutputCell(code, resultGenerator);
}
}
async function createPythonService(
versionRequirement?: number
): Promise<IPythonExecutionService | undefined> {
if (!ioc.mockJupyter) {
const python = await ioc.getJupyterCapableInterpreter();
if (
python &&
python.version?.major &&
(!versionRequirement || python.version?.major > versionRequirement)
) {
return pythonFactory.createActivatedEnvironment({
resource: undefined,
interpreter: python,
allowEnvironmentFetchExceptions: true,
bypassCondaExecution: true
});
}
}
}
async function startRemoteServer(pythonService: IPythonExecutionService, args: string[]): Promise<string> {
const connectionFound = createDeferred();
const exeResult = pythonService.execObservable(args, {
throwOnStdErr: false
});
disposables.push(exeResult);
exeResult.out.subscribe(
(output: Output<string>) => {
traceInfo(`Remote server output: ${output.out}`);
const connectionURL = getIPConnectionInfo(output.out);
if (connectionURL) {
connectionFound.resolve(connectionURL);
}
},
(e) => {
traceInfo(`Remote server error: ${e}`);
connectionFound.reject(e);
}
);
traceInfo('Connecting to remote server');
const connString = await connectionFound.promise;
const uri = connString as string;
// Wait another 3 seconds to give notebook time to be ready. Not sure
// how else to know when it's okay to connect to. Mac on azure seems
// to connect too fast and then is unable to actually communicate.
await sleep(3000);
return uri;
}
runTest('Remote Self Certs', async (_this: Mocha.Context) => {
const pythonService = await createPythonService(2);
// Skip test for older python and raw kernel
if (pythonService && !useRawKernel) {
// We will only connect if we allow for self signed cert connections
ioc.forceDataScienceSettingsChanged({
allowUnauthorizedRemoteConnection: true,
jupyterLaunchTimeout: 60000
});
const pemFile = path.join(
EXTENSION_ROOT_DIR,
'src',
'test',
'datascience',
'serverConfigFiles',
'jcert.pem'
);
const keyFile = path.join(
EXTENSION_ROOT_DIR,
'src',
'test',
'datascience',
'serverConfigFiles',
'jkey.key'
);
const uri = await startRemoteServer(pythonService, [
'-m',
'jupyter',
'notebook',
'--NotebookApp.open_browser=False',
'--NotebookApp.ip=*',
'--NotebookApp.port=9999',
`--certfile=${pemFile}`,
`--keyfile=${keyFile}`
]);
traceInfo('Waiting for notebook');
// We have a connection string here, so try to connect jupyterExecution to the notebook server
const notebook = await createNotebook(uri);
if (!notebook) {
assert.fail(`Failed to connect to remote self cert server on ${uri}`);
} else {
await verifySimple(notebook, `a=1${os.EOL}a`, 1);
}
} else {
traceInfo('Remote Self Cert is not supported on 2.7');
_this.skip();
}
});
// Connect to a server that doesn't have a token or password, customers use this and we regressed it once
runTest(
'Remote No Auth',
async () => {
const pythonService = await createPythonService();
if (pythonService) {
const configFile = path.join(
EXTENSION_ROOT_DIR,
'src',
'test',
'datascience',
'serverConfigFiles',
'remoteNoAuth.py'
);
const uri = await startRemoteServer(pythonService, [
'-m',
'jupyter',
'notebook',
`--config=${configFile}`
]);
// We have a connection string here, so try to connect jupyterExecution to the notebook server
const notebook = await createNotebook(uri);
if (!notebook) {
assert.fail('Failed to connect to remote password server');
} else {
await verifySimple(notebook, `a=1${os.EOL}a`, 1);
}
}
},
undefined,
() => {
const dummyDisposable = {
dispose: () => {
return;
}
};
const appShell = TypeMoq.Mock.ofType<IApplicationShell>();
appShell
.setup((a) => a.showErrorMessage(TypeMoq.It.isAnyString()))
.returns((e) => {
throw e;
});
appShell
.setup((a) => a.showInformationMessage(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => Promise.resolve(''));
appShell
.setup((a) =>
a.showInformationMessage(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())
)
.returns((_a1: string, a2: string, _a3: string) => Promise.resolve(a2));
appShell
.setup((a) =>
a.showInformationMessage(
TypeMoq.It.isAny(),
TypeMoq.It.isAny(),
TypeMoq.It.isAny(),
TypeMoq.It.isAny()
)
)
.returns((_a1: string, a2: string, _a3: string, _a4: string) => Promise.resolve(a2));
appShell.setup((a) => a.showInputBox(TypeMoq.It.isAny())).returns(() => Promise.resolve(''));
appShell.setup((a) => a.setStatusBarMessage(TypeMoq.It.isAny())).returns(() => dummyDisposable);
ioc.serviceManager.rebindInstance<IApplicationShell>(IApplicationShell, appShell.object);
}
);
runTest('Remote Password', async () => {
const pythonService = await createPythonService();
if (pythonService && !useRawKernel) {
const configFile = path.join(
EXTENSION_ROOT_DIR,
'src',
'test',
'datascience',
'serverConfigFiles',
'remotePassword.py'
);
const uri = await startRemoteServer(pythonService, [
'-m',
'jupyter',
'notebook',
`--config=${configFile}`
]);
traceInfo('Waiting for notebook');
// We have a connection string here, so try to connect jupyterExecution to the notebook server
const notebook = await createNotebook(uri);
if (!notebook) {
assert.fail('Failed to connect to remote password server');
} else {
await verifySimple(notebook, `a=1${os.EOL}a`, 1);
}
}
});
runTest('Remote', async () => {
const pythonService = await createPythonService();
if (pythonService) {
const configFile = path.join(
EXTENSION_ROOT_DIR,
'src',
'test',
'datascience',
'serverConfigFiles',
'remoteToken.py'
);
const uri = await startRemoteServer(pythonService, [
'-m',
'jupyter',
'notebook',
`--config=${configFile}`
]);
// We have a connection string here, so try to connect jupyterExecution to the notebook server
const notebook = await createNotebook(uri);
if (!notebook) {
assert.fail('Failed to connect to remote server');
} else {
await verifySimple(notebook, `a=1${os.EOL}a`, 1);
}
}
});
runTest('Creation', async () => {
await createNotebook();
});
runTest('Failure', async (_this: Mocha.Context) => {
if (!useRawKernel) {
// Make a dummy class that will fail during launch
class FailedProcess extends JupyterExecutionFactory {
public isNotebookSupported = (): Promise<boolean> => {
return Promise.resolve(false);
};
}
ioc.serviceManager.rebind<IJupyterExecution>(IJupyterExecution, FailedProcess);
await createNotebook(undefined, undefined, true);
} else {
// This test is useless for raw kernel. You can't fail to launch a python process
_this.skip();
}
});
test('Not installed', async function () {
if (!useRawKernel) {
// Rewire our data we use to search for processes
@injectable()
class EmptyInterpreterService implements IInterpreterService {
public get hasInterpreters(): Promise<boolean> {
return Promise.resolve(true);
}
public onDidChangeInterpreter(
_listener: (e: void) => any,
_thisArgs?: any,
_disposables?: Disposable[]
): Disposable {
return { dispose: noop };
}
public onDidChangeInterpreterInformation(
_listener: (e: PythonInterpreter) => any,
_thisArgs?: any,
_disposables?: Disposable[]
): Disposable {
return { dispose: noop };
}
public getInterpreters(_resource?: Uri): Promise<PythonInterpreter[]> {
return Promise.resolve([]);
}
public autoSetInterpreter(): Promise<void> {
throw new Error('Method not implemented');
}
public getActiveInterpreter(_resource?: Uri): Promise<PythonInterpreter | undefined> {
return Promise.resolve(undefined);
}
public getInterpreterDetails(_pythonPath: string, _resoure?: Uri): Promise<PythonInterpreter> {
throw new Error('Method not implemented');
}
public refresh(_resource: Uri): Promise<void> {
throw new Error('Method not implemented');
}
public initialize(): void {
throw new Error('Method not implemented');
}
public getDisplayName(_interpreter: Partial<PythonInterpreter>): Promise<string> {
throw new Error('Method not implemented');
}
public shouldAutoSetInterpreter(): Promise<boolean> {
throw new Error('Method not implemented');
}
}
@injectable()
class EmptyPathService implements IKnownSearchPathsForInterpreters {
public getSearchPaths(): string[] {
return [];
}
}
ioc.serviceManager.rebind<IInterpreterService>(IInterpreterService, EmptyInterpreterService);
ioc.serviceManager.rebind<IKnownSearchPathsForInterpreters>(
IKnownSearchPathsForInterpreters,
EmptyPathService
);
await createNotebook(undefined, undefined, true);
} else {
// tslint:disable-next-line: no-invalid-this
this.skip();
}
});
runTest('Export/Import', async () => {
// Get a bunch of test cells (use our test cells from the react controls)
const testFolderPath = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'datascience');
const testState = generateTestState(testFolderPath);
const cells = testState.cellVMs.map((cellVM: ICellViewModel, _index: number) => {
return cellVM.cell;
});
// Translate this into a notebook
// Make sure we have a change dir happening
const settings = { ...ioc.getSettings().datascience };
settings.changeDirOnImportExport = true;
ioc.forceSettingsChanged(undefined, ioc.getSettings().pythonPath, settings);
const exporter = ioc.serviceManager.get<INotebookExporter>(INotebookExporter);
const newFolderPath = path.join(
EXTENSION_ROOT_DIR,
'src',
'test',
'datascience',
'WorkspaceDir',
'WorkspaceSubDir',
'foo.ipynb'
);
const notebook = await exporter.translateToNotebook(cells, newFolderPath);
assert.ok(notebook, 'Translate to notebook is failing');
// Make sure we added in our chdir
if (notebook) {
const nbcells = notebook.cells;
if (nbcells) {
const firstCellText: string = nbcells[0].source as string;
assert.ok(firstCellText.includes('os.chdir'), `${firstCellText} does not include 'os.chdir`);
}
}
// Save to a temp file
const fileSystem = ioc.serviceManager.get<IFileSystem>(IFileSystem);
const importer = ioc.serviceManager.get<INotebookImporter>(INotebookImporter);
const temp = await fileSystem.createTemporaryFile('.ipynb');
try {
await fs.writeFile(temp.filePath, JSON.stringify(notebook), 'utf8');
// Try importing this. This should verify export works and that importing is possible
const results = await importer.importFromFile(temp.filePath);
// Make sure we have a single chdir in our results
const first = results.indexOf('os.chdir');
assert.ok(first >= 0, 'No os.chdir in import');
const second = results.indexOf('os.chdir', first + 1);
assert.equal(second, -1, 'More than one chdir in the import. It should be skipped');
// Make sure we have a cell in our results
assert.ok(/#\s*%%/.test(results), 'No cells in returned import');
} finally {
importer.dispose();
temp.dispose();
}
});
// tslint:disable-next-line:no-invalid-template-strings
runTest('Verify ${fileDirname} working directory', async () => {
// Verify that the default ${fileDirname} setting sets the working directory to the file path
changeMockWorkingDirectory(`'${srcDirectory()}'`);
const notebook = await createNotebook();
await verifySimple(notebook, 'import os\nos.getcwd()', srcDirectory(), true);
await verifySimple(notebook, 'import sys\nsys.path[0]', srcDirectory(), true);
});
runTest('Change Interpreter', async () => {
const isRollingBuild = process.env ? process.env.VSCODE_PYTHON_ROLLING !== undefined : false;
// Real Jupyter doesn't help this test at all and is tricky to set up for it, so just skip it
if (!isRollingBuild) {
const server = await createNotebook();
// Create again, we should get the same server from the cache
const server2 = await createNotebook();
// tslint:disable-next-line: triple-equals
assert.ok(server == server2, 'With no settings changed we should return the cached server');
// Create a new mock interpreter with a different path
const newPython: PythonInterpreter = {
path: '/foo/bar/baz/python.exe',
version: new SemVer('3.6.6-final'),
sysVersion: '1.0.0.0',
sysPrefix: 'Python',
type: InterpreterType.Unknown,
architecture: Architecture.x64
};
// Add interpreter into mock jupyter service and set it as active
ioc.addInterpreter(newPython, SupportedCommands.all);
// Create a new notebook, we should still be the same as interpreter is just saved for notebook creation
const server3 = await createNotebook();
// tslint:disable-next-line: triple-equals
assert.ok(server == server3, 'With interpreter changed we should not return a new server');
} else {
console.log(`Skipping Change Interpreter test in non-mocked Jupyter case`);
}
});
runTest('Restart kernel', async () => {
addMockData(`a=1${os.EOL}a`, 1);
addMockData(`a+=1${os.EOL}a`, 2);
addMockData(`a+=4${os.EOL}a`, 6);
addMockData('a', `name 'a' is not defined`, 'error');
const server = await createNotebook();
// Setup some state and verify output is correct
await verifySimple(server, `a=1${os.EOL}a`, 1);
await verifySimple(server, `a+=1${os.EOL}a`, 2);
await verifySimple(server, `a+=4${os.EOL}a`, 6);
console.log('Waiting for idle');
// In unit tests we have to wait for status idle before restarting. Unit tests
// seem to be timing out if the restart throws any exceptions (even if they're caught)
await server!.waitForIdle(10000);
console.log('Restarting kernel');
try {
await server!.restartKernel(10000);
console.log('Waiting for idle');
await server!.waitForIdle(10000);
console.log('Verifying restart');
await verifyError(server, 'a', `name 'a' is not defined`);
} catch (exc) {
assert.ok(
exc instanceof JupyterKernelPromiseFailedError,
`Restarting did not timeout correctly for ${exc}`
);
}
});
class TaggedCancellationTokenSource extends CancellationTokenSource {
public tag: string;
constructor(tag: string) {
super();
this.tag = tag;
}
}
async function testCancelableCall<T>(
method: (t: CancellationToken) => Promise<T>,
messageFormat: string,
timeout: number
): Promise<boolean> {
const tokenSource = new TaggedCancellationTokenSource(messageFormat.format(timeout.toString()));
const disp = setTimeout(
(_s) => {
tokenSource.cancel();
},
timeout,
tokenSource.tag
);
try {
// tslint:disable-next-line:no-string-literal
(tokenSource.token as any)['tag'] = messageFormat.format(timeout.toString());
await method(tokenSource.token);
} catch (exc) {
// This should happen. This means it was canceled.
assert.ok(exc instanceof CancellationError, `Non cancellation error found : ${exc.stack}`);
} finally {
clearTimeout(disp);
tokenSource.dispose();
}
return true;
}
async function testCancelableMethod<T>(
method: (t: CancellationToken) => Promise<T>,
messageFormat: string,
short?: boolean
): Promise<boolean> {
const timeouts = short ? [10, 20, 30, 100] : [300, 400, 500, 1000];
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < timeouts.length; i += 1) {
await testCancelableCall(method, messageFormat, timeouts[i]);
}
return true;
}
runTest('Cancel execution', async (_this: Mocha.Context) => {
if (useRawKernel) {
// Not cancellable at the moment. Just starts a process
_this.skip();
return;
}
if (ioc.mockJupyter) {
ioc.mockJupyter.setProcessDelay(2000);
addMockData(`a=1${os.EOL}a`, 1);
}
const jupyterExecution = ioc.get<IJupyterExecution>(IJupyterExecution);
// Try different timeouts, canceling after the timeout on each
assert.ok(
await testCancelableMethod(
(t: CancellationToken) => jupyterExecution.connectToNotebookServer(undefined, t),
'Cancel did not cancel start after {0}ms'
)
);
if (ioc.mockJupyter) {
ioc.mockJupyter.setProcessDelay(undefined);
}
// Make sure doing normal start still works
const nonCancelSource = new CancellationTokenSource();
const server = await jupyterExecution.connectToNotebookServer(undefined, nonCancelSource.token);
const notebook = server
? await server.createNotebook(baseUri, Uri.parse(Identifiers.InteractiveWindowIdentity))
: undefined;
assert.ok(notebook, 'Server not found with a cancel token that does not cancel');
// Make sure can run some code too
await verifySimple(notebook, `a=1${os.EOL}a`, 1);
if (ioc.mockJupyter) {
ioc.mockJupyter.setProcessDelay(200);
}
// Force a settings changed so that all of the cached data is cleared
ioc.forceSettingsChanged(undefined, '/usr/bin/test3/python');
assert.ok(
await testCancelableMethod(
(t: CancellationToken) => jupyterExecution.getUsableJupyterPython(t),
'Cancel did not cancel getusable after {0}ms',
true
)
);
assert.ok(
await testCancelableMethod(
(t: CancellationToken) => jupyterExecution.isNotebookSupported(t),
'Cancel did not cancel isNotebook after {0}ms',
true
)
);
assert.ok(
await testCancelableMethod(
(t: CancellationToken) => jupyterExecution.isImportSupported(t),
'Cancel did not cancel isImport after {0}ms',
true
)
);
});
async function interruptExecute(
notebook: INotebook | undefined,
code: string,
interruptMs: number,
sleepMs: number
): Promise<InterruptResult> {
let interrupted = false;
let finishedBefore = false;
const finishedPromise = createDeferred();
let error;
const observable = notebook!.executeObservable(code, Uri.file('foo.py').fsPath, 0, uuid(), false);
observable.subscribe(
(c) => {
if (c.length > 0 && c[0].state === CellState.error) {
finishedBefore = !interrupted;
finishedPromise.resolve();
}
if (c.length > 0 && c[0].state === CellState.finished) {
finishedBefore = !interrupted;
finishedPromise.resolve();
}
},
(err) => {
error = err;
finishedPromise.resolve();
},
() => finishedPromise.resolve()
);
// Then interrupt
interrupted = true;
const result = await notebook!.interruptKernel(interruptMs);
// Then we should get our finish unless there was a restart
await waitForPromise(finishedPromise.promise, sleepMs);
assert.equal(finishedBefore, false, 'Finished before the interruption');
assert.equal(error, undefined, 'Error thrown during interrupt');
assert.ok(
finishedPromise.completed ||
result === InterruptResult.TimedOut ||
result === InterruptResult.Success,
`Interrupt restarted ${result} for: ${code}`
);
return result;
}
runTest('Interrupt kernel', async (_this: Mocha.Context) => {
// Interrupt doesn't work yet for the raw kernel.
if (useRawKernel) {
_this.skip();
return;
}
const returnable = `import signal
import _thread
import time
keep_going = True
def handler(signum, frame):
global keep_going
print('signal')
keep_going = False
signal.signal(signal.SIGINT, handler)
while keep_going:
print(".")
time.sleep(.1)`;
const fourSecondSleep = `import time${os.EOL}time.sleep(4)${os.EOL}print("foo")`;
const kill = `import signal
import time
import os
keep_going = True
def handler(signum, frame):