forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.test.ts
More file actions
437 lines (381 loc) · 21.6 KB
/
Copy pathmisc.test.ts
File metadata and controls
437 lines (381 loc) · 21.6 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// tslint:disable:no-suspicious-comment max-func-body-length no-invalid-this
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import { ThreadEvent } from 'vscode-debugadapter';
import { DebugClient } from 'vscode-debugadapter-testsupport';
import { LaunchRequestArguments } from '../../client/debugger/Common/Contracts';
import { sleep } from '../common';
import { IS_CI_SERVER, IS_MULTI_ROOT_TEST } from '../initialize';
use(chaiAsPromised);
const debugFilesPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'debugging');
const DEBUG_ADAPTER = path.join(__dirname, '..', '..', 'client', 'debugger', 'Main.js');
const MAX_SIGNED_INT32 = Math.pow(2, 31) - 1;
const EXPERIMENTAL_DEBUG_ADAPTER = path.join(__dirname, '..', '..', 'client', 'debugger', 'mainV2.js');
[DEBUG_ADAPTER, EXPERIMENTAL_DEBUG_ADAPTER].forEach(testAdapterFilePath => {
const debugAdapterFileName = path.basename(testAdapterFilePath);
const debuggerType = debugAdapterFileName === 'Main.js' ? 'python' : 'pythonExperimental';
suite(`Standard Debugging - Misc tests: ${debuggerType}`, () => {
let debugClient: DebugClient;
setup(async function () {
if (!IS_MULTI_ROOT_TEST) {
this.skip();
}
// Temporary, untill new version of PTVSD is bundled we cannot run tests
if (debuggerType !== 'python' && IS_CI_SERVER) {
return this.skip();
}
await new Promise(resolve => setTimeout(resolve, 1000));
debugClient = new DebugClient('node', testAdapterFilePath, debuggerType);
await debugClient.start();
});
teardown(async () => {
// Wait for a second before starting another test (sometimes, sockets take a while to get closed).
await new Promise(resolve => setTimeout(resolve, 1000));
try {
// tslint:disable-next-line:no-empty
await debugClient.stop().catch(() => { });
// tslint:disable-next-line:no-empty
} catch (ex) { }
});
function buildLauncArgs(pythonFile: string, stopOnEntry: boolean = false): LaunchRequestArguments {
// Temporary, untill new version of PTVSD is bundled we cannot run tests.
// For now lets run test locally.
const pythonPath = debuggerType === 'python' ? 'python' : '/Users/donjayamanne/anaconda3/envs/py36/bin/python';
const env = debuggerType === 'python' ? {} : { PYTHONPATH: '/Users/donjayamanne/Desktop/Development/vscode/ptvsd' };
return {
program: path.join(debugFilesPath, pythonFile),
cwd: debugFilesPath,
stopOnEntry,
debugOptions: ['RedirectOutput'],
pythonPath,
args: [],
env,
envFile: '',
logToFile: false,
type: debuggerType
};
}
test('Should run program to the end', async () => {
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('simplePrint.py', false)),
debugClient.waitForEvent('initialized'),
debugClient.waitForEvent('terminated')
]);
});
test('Should stop on entry', async function () {
if (debuggerType !== 'python') {
return this.skip();
}
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('simplePrint.py', true)),
debugClient.waitForEvent('initialized'),
debugClient.waitForEvent('stopped')
]);
});
test('test stderr output', async function () {
if (debuggerType !== 'python') {
return this.skip();
}
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('stdErrOutput.py', false)),
debugClient.waitForEvent('initialized'),
//TODO: ptvsd does not differentiate.
debugClient.assertOutput('stdout', 'error output'),
debugClient.waitForEvent('terminated')
]);
});
test('Test stdout output', async function () {
if (debuggerType !== 'python') {
return this.skip();
}
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('stdOutOutput.py', false)),
debugClient.waitForEvent('initialized'),
debugClient.assertOutput('stdout', 'normal output'),
debugClient.waitForEvent('terminated')
]);
});
test('Should run program to the end (with stopOnEntry=true and continue)', async function () {
if (debuggerType !== 'python') {
return this.skip();
}
const threadIdPromise = debugClient.waitForEvent('thread');
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('simplePrint.py', true)),
debugClient.waitForEvent('initialized'),
debugClient.waitForEvent('stopped')
]);
const threadId = ((await threadIdPromise) as ThreadEvent).body.threadId;
await Promise.all([
debugClient.continueRequest({ threadId }),
debugClient.waitForEvent('terminated')
]);
});
test('Ensure threadid is int32', async () => {
const launchArgs = buildLauncArgs('sample2.py', false);
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
await debugClient.hitBreakpoint(launchArgs, breakpointLocation);
const threads = await debugClient.threadsRequest();
expect(threads).to.be.not.equal(undefined, 'no threads response');
expect(threads.body.threads).to.be.lengthOf(1);
const threadId = threads.body.threads[0].id;
expect(threadId).to.be.lessThan(MAX_SIGNED_INT32 + 1, 'ThreadId is not an integer');
await Promise.all([
debugClient.continueRequest({ threadId }),
debugClient.waitForEvent('terminated')
]);
});
test('Should break at print statement (line 3)', async () => {
const launchArgs = buildLauncArgs('sample2.py', false);
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
await debugClient.hitBreakpoint(launchArgs, breakpointLocation);
});
test('Test conditional breakpoints', async () => {
const threadIdPromise = debugClient.waitForEvent('thread');
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('forever.py', false)),
debugClient.waitForEvent('initialized')
]);
const breakpointLocation = { path: path.join(debugFilesPath, 'forever.py'), column: 0, line: 5 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column, condition: 'i == 3' }],
source: { path: breakpointLocation.path }
});
await sleep(1);
await threadIdPromise;
const frames = await debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
// Wait for breakpoint to hit
const frameId = frames.body.stackFrames[0].id;
const scopes = await debugClient.scopesRequest({ frameId });
expect(scopes.body.scopes).of.length(1, 'Incorrect number of scopes');
const variablesReference = scopes.body.scopes[0].variablesReference;
const variables = await debugClient.variablesRequest({ variablesReference });
const vari = variables.body.variables.find(item => item.name === 'i')!;
expect(vari).to.be.not.equal('undefined', 'variable \'i\' is undefined');
expect(vari.value).to.be.equal('3');
});
test('Test variables', async () => {
const threadIdPromise = debugClient.waitForEvent('thread');
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('sample2.py', false)),
debugClient.waitForEvent('initialized')
]);
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
await threadIdPromise;
const stackFramesPromise = debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
// Wait for breakpoint to hit
const frameId = (await stackFramesPromise).body.stackFrames[0].id;
const scopes = await debugClient.scopesRequest({ frameId });
expect(scopes.body.scopes).of.length(1, 'Incorrect number of scopes');
const variablesReference = scopes.body.scopes[0].variablesReference;
const variables = await debugClient.variablesRequest({ variablesReference });
const vara = variables.body.variables.find(item => item.name === 'a')!;
const varb = variables.body.variables.find(item => item.name === 'b')!;
const varfile = variables.body.variables.find(item => item.name === '__file__')!;
const vardoc = variables.body.variables.find(item => item.name === '__doc__')!;
expect(vara).to.be.not.equal('undefined', 'variable \'a\' is undefined');
expect(vara.value).to.be.equal('1');
expect(varb).to.be.not.equal('undefined', 'variable \'b\' is undefined');
expect(varb.value).to.be.equal('2');
expect(varfile).to.be.not.equal('undefined', 'variable \'__file__\' is undefined');
expect(varfile.value).to.be.equal(`'${path.join(debugFilesPath, 'sample2.py')}'`);
expect(vardoc).to.be.not.equal('undefined', 'variable \'__doc__\' is undefined');
});
test('Test editing variables', async () => {
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('sample2.py', false)),
debugClient.waitForEvent('initialized')
]);
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
// const threadId = ((await threadIdPromise) as ThreadEvent).body.threadId;
const stackFramesPromise = debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
// Wait for breakpoint to hit
const frameId = (await stackFramesPromise).body.stackFrames[0].id;
const scopes = await debugClient.scopesRequest({ frameId });
expect(scopes.body.scopes).of.length(1, 'Incorrect number of scopes');
const variablesReference = scopes.body.scopes[0].variablesReference;
const variables = await debugClient.variablesRequest({ variablesReference });
const vara = variables.body.variables.find(item => item.name === 'a')!;
expect(vara).to.be.not.equal('undefined', 'variable \'a\' is undefined');
expect(vara.value).to.be.equal('1');
const response = await debugClient.setVariableRequest({ variablesReference, name: 'a', value: '1234' });
expect(response.success).to.be.equal(true, 'settting variable failed');
expect(response.body.value).to.be.equal('1234');
});
test('Test evaluating expressions', async () => {
const threadIdPromise = debugClient.waitForEvent('thread');
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('sample2.py', false)),
debugClient.waitForEvent('initialized')
]);
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
await threadIdPromise;
const stackFramesPromise = debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
// Wait for breakpoint to hit
const frameId = (await stackFramesPromise).body.stackFrames[0].id;
const response = await debugClient.evaluateRequest({ frameId, expression: '(a+b)*2' });
expect(response.success).to.be.equal(true, 'variable evaluation failed');
expect(response.body.result).to.be.equal('6', 'expression value is incorrect');
});
test('Test stepover', async () => {
const threadIdPromise = debugClient.waitForEvent('thread');
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('sample2.py', false)),
debugClient.waitForEvent('initialized')
]);
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
// hit breakpoint.
const threadId = ((await threadIdPromise) as ThreadEvent).body.threadId;
await debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
await debugClient.nextRequest({ threadId });
const functionLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 7 };
await debugClient.assertStoppedLocation('step', functionLocation);
await debugClient.nextRequest({ threadId });
const functionInvocationLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 11 };
await debugClient.assertStoppedLocation('step', functionInvocationLocation);
await debugClient.nextRequest({ threadId });
const printLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 13 };
await debugClient.assertStoppedLocation('step', printLocation);
});
test('Test stepin and stepout', async () => {
const threadIdPromise = debugClient.waitForEvent('thread');
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('sample2.py', false)),
debugClient.waitForEvent('initialized')
]);
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
// hit breakpoint.
await debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
const threadId = ((await threadIdPromise) as ThreadEvent).body.threadId;
await debugClient.nextRequest({ threadId });
const functionLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 7 };
await debugClient.assertStoppedLocation('step', functionLocation);
await debugClient.nextRequest({ threadId });
const functionInvocationLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 11 };
await debugClient.assertStoppedLocation('step', functionInvocationLocation);
await debugClient.stepInRequest({ threadId });
const loopPrintLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 8 };
await debugClient.assertStoppedLocation('step', loopPrintLocation);
await debugClient.stepOutRequest({ threadId });
await debugClient.assertStoppedLocation('step', functionInvocationLocation);
await debugClient.nextRequest({ threadId });
const printLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 13 };
await debugClient.assertStoppedLocation('step', printLocation);
});
test('Test pausing', async function () {
if (debuggerType !== 'python') {
return this.skip();
}
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('forever.py', false)),
debugClient.waitForEvent('initialized')
]);
await sleep(3);
const pauseLocation = { path: path.join(debugFilesPath, 'forever.py'), line: 5 };
const pausePromise = debugClient.assertStoppedLocation('pause', pauseLocation);
const threads = await debugClient.threadsRequest();
expect(threads).to.be.not.equal(undefined, 'no threads response');
expect(threads.body.threads).to.be.lengthOf(1);
await debugClient.pauseRequest({ threadId: threads.body.threads[0].id });
await pausePromise;
});
test('Test pausing on exceptions', async function () {
if (debuggerType !== 'python') {
return this.skip();
}
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('sample3WithEx.py', false)),
debugClient.waitForEvent('initialized')
]);
const pauseLocation = { path: path.join(debugFilesPath, 'sample3WithEx.py'), line: 5 };
await debugClient.assertStoppedLocation('exception', pauseLocation);
});
test('Test multi-threaded debugging', async () => {
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('multiThread.py', false)),
debugClient.waitForEvent('initialized')
]);
const pythonFile = path.join(debugFilesPath, 'multiThread.py');
const breakpointLocation = { path: pythonFile, column: 0, line: 11 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
// hit breakpoint.
await debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
const threads = await debugClient.threadsRequest();
expect(threads.body.threads).of.lengthOf(2, 'incorrect number of threads');
for (const thread of threads.body.threads) {
expect(thread.id).to.be.lessThan(MAX_SIGNED_INT32 + 1, 'ThreadId is not an integer');
}
});
test('Test stack frames', async () => {
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs('stackFrame.py', false)),
debugClient.waitForEvent('initialized')
]);
const pythonFile = path.join(debugFilesPath, 'stackFrame.py');
const breakpointLocation = { path: pythonFile, column: 0, line: 5 };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
// hit breakpoint.
const stackframes = await debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
expect(stackframes.body.stackFrames[0].line).to.be.equal(5);
expect(stackframes.body.stackFrames[0].source!.path).to.be.equal(pythonFile);
expect(stackframes.body.stackFrames[0].name).to.be.equal('foo');
expect(stackframes.body.stackFrames[1].line).to.be.equal(8);
expect(stackframes.body.stackFrames[1].source!.path).to.be.equal(pythonFile);
expect(stackframes.body.stackFrames[1].name).to.be.equal('bar');
expect(stackframes.body.stackFrames[2].line).to.be.equal(10);
expect(stackframes.body.stackFrames[2].source!.path).to.be.equal(pythonFile);
});
});
});