Repository navigation
Expand file tree
/
Copy pathdebugger.functional.test.tsx
More file actions
445 lines (395 loc) · 19.1 KB
/
Copy pathdebugger.functional.test.tsx
File metadata and controls
445 lines (395 loc) · 19.1 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
import * as path from 'path';
import * as TypeMoq from 'typemoq';
import * as uuid from 'uuid/v4';
import { CodeLens, Disposable, Position, Range, SourceBreakpoint, Uri } from 'vscode';
import { CancellationToken } from 'vscode-jsonrpc';
import { expect } from 'chai';
import { IApplicationShell, IDocumentManager } from '../../client/common/application/types';
import { RunByLine } from '../../client/common/experiments/groups';
import { createDeferred, waitForPromise } from '../../client/common/utils/async';
import { noop } from '../../client/common/utils/misc';
import { EXTENSION_ROOT_DIR } from '../../client/constants';
import { Commands, Identifiers } from '../../client/datascience/constants';
import { InteractiveWindowMessages } from '../../client/datascience/interactive-common/interactiveWindowTypes';
import {
IDataScienceCodeLensProvider,
IDebugLocationTracker,
IInteractiveWindowProvider,
IJupyterDebugService,
IJupyterExecution
} from '../../client/datascience/types';
import { DebugState } from '../../datascience-ui/interactive-common/mainState';
import { ImageButton } from '../../datascience-ui/react-common/imageButton';
import { DataScienceIocContainer } from './dataScienceIocContainer';
import { takeSnapshot, writeDiffSnapshot } from './helpers';
import { getInteractiveCellResults, getOrCreateInteractiveWindow } from './interactiveWindowTestHelpers';
import { MockDocument } from './mockDocument';
import { MockDocumentManager } from './mockDocumentManager';
import { addCell, createNewEditor } from './nativeEditorTestHelpers';
import {
getLastOutputCell,
openVariableExplorer,
runInteractiveTest,
runNativeTest,
waitForMessage
} from './testHelpers';
import { verifyVariables } from './variableTestHelpers';
//import { asyncDump } from '../common/asyncDump';
// tslint:disable-next-line:max-func-body-length no-any
suite('DataScience Debugger tests', () => {
const disposables: Disposable[] = [];
const postDisposables: Disposable[] = [];
let ioc: DataScienceIocContainer;
let lastErrorMessage: string | undefined;
let jupyterDebuggerService: IJupyterDebugService | undefined;
// tslint:disable-next-line: no-any
let snapshot: any;
suiteSetup(function () {
snapshot = takeSnapshot();
// Debugger tests require jupyter to run. Othewrise can't not really testing them
const isRollingBuild = process.env ? process.env.VSCODE_PYTHON_ROLLING !== undefined : false;
if (!isRollingBuild) {
// tslint:disable-next-line:no-console
console.log('Skipping Debugger tests. Requires python environment');
// tslint:disable-next-line:no-invalid-this
this.skip();
}
});
suiteTeardown(() => {
writeDiffSnapshot(snapshot, 'Debugger');
});
setup(async () => {
ioc = new DataScienceIocContainer();
});
async function createIOC() {
ioc.registerDataScienceTypes();
jupyterDebuggerService = ioc.serviceManager.get<IJupyterDebugService>(
IJupyterDebugService,
Identifiers.MULTIPLEXING_DEBUGSERVICE
);
// Rebind the appshell so we can change what happens on an error
const dummyDisposable = {
dispose: () => {
return;
}
};
const appShell = TypeMoq.Mock.ofType<IApplicationShell>();
appShell.setup((a) => a.showErrorMessage(TypeMoq.It.isAnyString())).returns((e) => (lastErrorMessage = 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.showSaveDialog(TypeMoq.It.isAny()))
.returns(() => Promise.resolve(Uri.file('test.ipynb')));
appShell.setup((a) => a.setStatusBarMessage(TypeMoq.It.isAny())).returns(() => dummyDisposable);
ioc.serviceManager.rebindInstance<IApplicationShell>(IApplicationShell, appShell.object);
// Make sure the history provider and execution factory in the container is created (the extension does this on startup in the extension)
// This is necessary to get the appropriate live share services up and running.
ioc.get<IInteractiveWindowProvider>(IInteractiveWindowProvider);
ioc.get<IJupyterExecution>(IJupyterExecution);
ioc.get<IDebugLocationTracker>(IDebugLocationTracker);
await ioc.activate();
return ioc;
}
teardown(async () => {
for (const disposable of disposables) {
if (!disposable) {
continue;
}
// tslint:disable-next-line:no-any
const promise = disposable.dispose() as Promise<any>;
if (promise) {
await promise;
}
}
await ioc.dispose();
lastErrorMessage = undefined;
for (const disposable of postDisposables) {
if (!disposable) {
continue;
}
// tslint:disable-next-line:no-any
const promise = disposable.dispose() as Promise<any>;
if (promise) {
await promise;
}
}
});
suiteTeardown(() => {
// asyncDump();
});
async function debugCell(
code: string,
breakpoint?: Range,
breakpointFile?: string,
expectError?: boolean,
stepAndVerify?: () => void
): Promise<void> {
// Create a dummy document with just this code
const docManager = ioc.get<IDocumentManager>(IDocumentManager) as MockDocumentManager;
const fileName = path.join(EXTENSION_ROOT_DIR, 'foo.py');
docManager.addDocument(code, fileName);
if (breakpoint) {
const sourceFile = breakpointFile ? path.join(EXTENSION_ROOT_DIR, breakpointFile) : fileName;
const sb: SourceBreakpoint = {
location: {
uri: Uri.file(sourceFile),
range: breakpoint
},
id: uuid(),
enabled: true
};
jupyterDebuggerService!.addBreakpoints([sb]);
}
// Start the jupyter server
const history = await getOrCreateInteractiveWindow(ioc);
const expectedBreakLine = breakpoint && !breakpointFile ? breakpoint.start.line : 2; // 2 because of the 'breakpoint()' that gets added
// Debug this code. We should either hit the breakpoint or stop on entry
const resultPromise = getInteractiveCellResults(ioc, ioc.wrapper!, async () => {
let breakPromise = createDeferred<void>();
// Make sure that our code lens provider has signaled a change before we check the lenses
let newLensPromise = createDeferred<void>();
let newLensDispose;
const codeLensProvider = ioc.serviceManager.get<IDataScienceCodeLensProvider>(IDataScienceCodeLensProvider);
if (codeLensProvider.onDidChangeCodeLenses) {
newLensDispose = codeLensProvider.onDidChangeCodeLenses(() => newLensPromise.resolve());
}
disposables.push(jupyterDebuggerService!.onBreakpointHit(() => breakPromise.resolve()));
const done = history.debugCode(code, fileName, 0, docManager.activeTextEditor);
await waitForPromise(Promise.race([done, breakPromise.promise]), 60000);
if (expectError) {
assert.ok(lastErrorMessage, 'Error did not occur when expected');
throw Error('Exiting cell results');
} else {
assert.ok(breakPromise.resolved, 'Breakpoint event did not fire');
assert.ok(!lastErrorMessage, `Error occurred ${lastErrorMessage}`);
const stackFrames = await jupyterDebuggerService!.getStack();
assert.ok(stackFrames, 'Stack trace not computable');
assert.ok(stackFrames.length >= 1, 'Not enough frames');
assert.equal(stackFrames[0].line, expectedBreakLine, 'Stopped on wrong line number');
await waitForPromise(newLensPromise.promise, 10_000);
verifyCodeLenses(expectedBreakLine);
newLensDispose?.dispose();
// Step if allowed
if (stepAndVerify && ioc.wrapper && !ioc.mockJupyter) {
// Verify variables work
openVariableExplorer(ioc.wrapper);
breakPromise = createDeferred<void>();
await jupyterDebuggerService?.step();
await breakPromise.promise;
await waitForMessage(ioc, InteractiveWindowMessages.VariablesComplete);
const variableRefresh = waitForMessage(ioc, InteractiveWindowMessages.VariablesComplete);
await jupyterDebuggerService?.requestVariables();
await variableRefresh;
// Force an update so we render whatever the current state is
ioc.wrapper.update();
// Then verify results.
stepAndVerify();
}
newLensPromise = createDeferred<void>();
if (codeLensProvider.onDidChangeCodeLenses) {
newLensDispose = codeLensProvider.onDidChangeCodeLenses(() => newLensPromise.resolve());
}
// Verify break location
await jupyterDebuggerService!.continue();
await waitForPromise(newLensPromise.promise, 10_000);
verifyCodeLenses(undefined);
newLensDispose?.dispose();
}
});
if (!expectError) {
const cellResults = await resultPromise;
assert.ok(cellResults, 'No cell results after finishing debugging');
} else {
try {
await resultPromise;
} catch {
noop();
}
}
await history.dispose();
}
function verifyCodeLenses(expectedBreakLine: number | undefined) {
// We should have three debug code lenses which should all contain the break line
const codeLenses = getCodeLenses();
if (expectedBreakLine) {
assert.equal(codeLenses.length, 3, 'Incorrect number of debug code lenses stop');
expect(codeLenses[0].command!.command).to.be.equal(Commands.DebugContinue);
expect(codeLenses[1].command!.command).to.be.equal(Commands.DebugStop);
expect(codeLenses[2].command!.command).to.be.equal(Commands.DebugStepOver);
codeLenses.forEach((codeLens) => {
assert.ok(codeLens.range.contains(new Position(expectedBreakLine - 1, 0)));
});
} else {
// Two options, either we are in Debug-Run mode and expect no lenses.
// Or we are in Design mode and expect run - run below - debug cell - goto
if (codeLenses.length !== 4) {
assert.equal(codeLenses.length, 0, 'Incorrect number of debug code lenses debug - run');
} else {
assert.equal(codeLenses.length, 4, 'Incorrect number of debug code lenses design after debug');
expect(codeLenses[0].command!.command).to.be.equal(Commands.RunCell);
expect(codeLenses[1].command!.command).to.be.equal(Commands.RunCellAndAllBelow);
expect(codeLenses[2].command!.command).to.be.equal(Commands.DebugCell);
expect(codeLenses[3].command!.command).to.be.equal(Commands.ScrollToCell);
}
}
}
function getCodeLenses(): CodeLens[] {
const documentManager = ioc.serviceManager.get<IDocumentManager>(IDocumentManager) as MockDocumentManager;
const codeLensProvider = ioc.serviceManager.get<IDataScienceCodeLensProvider>(IDataScienceCodeLensProvider);
const doc = documentManager.textDocuments[0];
const result = codeLensProvider.provideCodeLenses(doc, CancellationToken.None);
// tslint:disable-next-line:no-any
if ((result as any).length) {
return result as CodeLens[];
}
return [];
}
runInteractiveTest(
'Debug cell without breakpoint',
async () => {
await debugCell('#%%\nprint("bar")');
},
createIOC
);
runInteractiveTest(
'Check variables',
async () => {
ioc.setExperimentState(RunByLine.experiment, true);
await debugCell('#%%\nx = [4, 6]\nx = 5', undefined, undefined, false, () => {
const targetResult = {
name: 'x',
value: '[4, 6]',
supportsDataExplorer: true,
type: 'list',
size: 0,
shape: '',
count: 2,
truncated: false
};
verifyVariables(ioc!.wrapper!, [targetResult]);
});
},
createIOC
);
runInteractiveTest(
'Debug temporary file',
async () => {
const code = '#%%\nprint("bar")';
// Create a dummy document with just this code
const docManager = ioc.get<IDocumentManager>(IDocumentManager) as MockDocumentManager;
const fileName = 'Untitled-1';
docManager.addDocument(code, fileName);
const mockDoc = docManager.textDocuments[0] as MockDocument;
mockDoc.forceUntitled();
// Start the jupyter server
const history = await getOrCreateInteractiveWindow(ioc);
const expectedBreakLine = 2; // 2 because of the 'breakpoint()' that gets added
// Debug this code. We should either hit the breakpoint or stop on entry
const resultPromise = getInteractiveCellResults(ioc, ioc.wrapper!, async () => {
const breakPromise = createDeferred<void>();
disposables.push(jupyterDebuggerService!.onBreakpointHit(() => breakPromise.resolve()));
const targetUri = Uri.file(fileName);
const done = history.debugCode(code, targetUri.fsPath, 0, docManager.activeTextEditor);
await waitForPromise(Promise.race([done, breakPromise.promise]), 60000);
assert.ok(breakPromise.resolved, 'Breakpoint event did not fire');
assert.ok(!lastErrorMessage, `Error occurred ${lastErrorMessage}`);
const stackFrames = await jupyterDebuggerService!.getStack();
assert.ok(stackFrames, 'Stack trace not computable');
assert.ok(stackFrames.length >= 1, 'Not enough frames');
assert.equal(stackFrames[0].line, expectedBreakLine, 'Stopped on wrong line number');
assert.ok(
stackFrames[0].source!.path!.includes('baz.py'),
'Stopped on wrong file name. Name should have been saved'
);
// Verify break location
await jupyterDebuggerService!.continue();
});
const cellResults = await resultPromise;
assert.ok(cellResults, 'No cell results after finishing debugging');
await history.dispose();
},
createIOC
);
runNativeTest(
'Run by line',
async () => {
// Create an editor so something is listening to messages
await createNewEditor(ioc);
const wrapper = ioc.wrapper!;
// Add a cell into the UI and wait for it to render and submit it.
await addCell(wrapper, ioc, 'a=1\na', true);
// Step into this cell using the button
let cell = getLastOutputCell(wrapper, 'NativeCell');
let ImageButtons = cell.find(ImageButton);
assert.equal(ImageButtons.length, 7, 'Cell buttons not found');
const runByLineButton = ImageButtons.at(3);
// tslint:disable-next-line: no-any
assert.equal((runByLineButton.instance().props as any).tooltip, 'Run by line');
const promise = waitForMessage(ioc, InteractiveWindowMessages.ShowingIp);
runByLineButton.simulate('click');
await promise;
// We should be in the break state. See if buttons indicate that or not
cell = getLastOutputCell(wrapper, 'NativeCell');
ImageButtons = cell.find(ImageButton);
assert.equal(ImageButtons.length, 4, 'Cell buttons wrong number');
},
() => {
ioc.setExperimentState(RunByLine.experiment, true);
return createIOC();
}
);
runNativeTest(
'Run by line state check',
async () => {
// Create an editor so something is listening to messages
await createNewEditor(ioc);
const wrapper = ioc.wrapper!;
// Add a cell into the UI and wait for it to render and submit it.
await addCell(wrapper, ioc, 'a=1\na=2\na=3', true);
// Step into this cell using the button
let cell = getLastOutputCell(wrapper, 'NativeCell');
let ImageButtons = cell.find(ImageButton);
assert.equal(ImageButtons.length, 7, 'Cell buttons not found');
const runByLineButton = ImageButtons.at(3);
// tslint:disable-next-line: no-any
assert.equal((runByLineButton.instance().props as any).tooltip, 'Run by line');
const promise = waitForMessage(ioc, InteractiveWindowMessages.DebugStateChange, {
withPayload: (p) => {
return p.oldState === DebugState.Design && p.newState === DebugState.Run;
}
});
runByLineButton.simulate('click');
await promise;
// We should be running, is the run by line button disabled?
cell = getLastOutputCell(wrapper, 'NativeCell');
ImageButtons = cell.find(ImageButton);
// tslint:disable-next-line: no-any
let runByLineButtonProps = ImageButtons.at(3).instance().props as any;
expect(runByLineButtonProps.disabled).to.equal(true, 'Run by line button not disabled when running');
// Now wait for break mode
const breakPromise = waitForMessage(ioc, InteractiveWindowMessages.DebugStateChange, {
withPayload: (p) => {
return p.oldState === DebugState.Run && p.newState === DebugState.Break;
}
});
await breakPromise;
cell = getLastOutputCell(wrapper, 'NativeCell');
ImageButtons = cell.find(ImageButton);
// tslint:disable-next-line: no-any
runByLineButtonProps = ImageButtons.at(3).instance().props as any;
expect(runByLineButtonProps.disabled).to.equal(false, 'Run by line button not active in break mode');
},
() => {
ioc.setExperimentState(RunByLine.experiment, true);
return createIOC();
}
);
});