Repository navigation
Expand file tree
/
Copy pathadapter.ts
More file actions
455 lines (393 loc) · 20.3 KB
/
Copy pathadapter.ts
File metadata and controls
455 lines (393 loc) · 20.3 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
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import {DebugClient} from 'vscode-debugadapter-testsupport';
import {DebugProtocol} from 'vscode-debugprotocol';
chai.use(chaiAsPromised);
const assert = chai.assert;
describe('PHP Debug Adapter', () => {
const TEST_PROJECT = path.normalize(__dirname + '/../../testproject');
let client: DebugClient;
beforeEach('start debug adapter', async () => {
client = new DebugClient('node', path.normalize(__dirname + '/../phpDebug'), 'php');
client.defaultTimeout = 10000;
await client.start(process.env.VSCODE_DEBUG_PORT && parseInt(process.env.VSCODE_DEBUG_PORT));
});
afterEach('stop debug adapter', () =>
client.stop()
);
describe('initialization', () => {
it('should return supported features', async () => {
const response = await client.initializeRequest();
assert.equal(response.body.supportsConfigurationDoneRequest, true);
assert.equal(response.body.supportsEvaluateForHovers, false);
assert.equal(response.body.supportsConditionalBreakpoints, true);
assert.equal(response.body.supportsFunctionBreakpoints, true);
});
});
describe('launch as CLI', () => {
const program = path.join(TEST_PROJECT, 'hello_world.php');
it('should error on non-existing file', () =>
assert.isRejected(client.launch({program: 'thisfiledoesnotexist.php'}))
);
it('should run program to the end', () =>
Promise.all([
client.launch({program}),
client.configurationSequence(),
client.waitForEvent('terminated')
])
);
it('should stop on entry', async () => {
const [event] = await Promise.all([
client.waitForEvent('stopped'),
client.launch({program, stopOnEntry: true}),
client.configurationSequence()
]);
assert.propertyVal(event.body, 'reason', 'entry');
});
it('should not stop if launched without debugging', () =>
Promise.all([
client.launch({program, stopOnEntry: true, noDebug: true}),
client.waitForEvent('terminated')
])
);
});
describe('continuation commands', () => {
const program = path.join(TEST_PROJECT, 'function.php');
it('should handle run');
it('should handle step_over');
it('should handle step_in');
it('should handle step_out');
it('should error on pause request', () =>
assert.isRejected(client.pauseRequest({threadId: 1}))
);
it('should handle disconnect', async () => {
await Promise.all([
client.launch({program, stopOnEntry: true}),
client.waitForEvent('initialized')
]);
await client.disconnectRequest();
});
});
async function assertStoppedLocation(reason: 'entry' | 'breakpoint' | 'exception', path: string, line: number): Promise<{threadId: number, frame: DebugProtocol.StackFrame}> {
const event = await client.waitForEvent('stopped') as DebugProtocol.StoppedEvent;
assert.propertyVal(event.body, 'reason', reason);
const threadId = event.body.threadId;
const response = await client.stackTraceRequest({threadId});
const frame = response.body.stackFrames[0];
let expectedPath = path;
let actualPath = frame.source.path;
if (process.platform === 'win32') {
expectedPath = expectedPath.toLowerCase();
actualPath = actualPath.toLowerCase();
}
assert.equal(actualPath, expectedPath, 'stopped location: path mismatch');
assert.equal(frame.line, line, 'stopped location: line mismatch');
return {threadId, frame};
}
describe('breakpoints', () => {
const program = path.join(TEST_PROJECT, 'hello_world.php');
describe('line breakpoints', () => {
async function testBreakpointHit(program: string, line: number): Promise<void> {
await Promise.all([client.launch({program}), client.waitForEvent('initialized')]);
const breakpoint = (await client.setBreakpointsRequest({breakpoints: [{line}], source: {path: program}})).body.breakpoints[0];
assert.isTrue(breakpoint.verified, 'breakpoint verification mismatch: verified');
assert.equal(breakpoint.line, line, 'breakpoint verification mismatch: line');
await Promise.all([
client.configurationDoneRequest(),
assertStoppedLocation('breakpoint', program, line)
]);
}
it('should stop on a breakpoint', () =>
testBreakpointHit(program, 4)
);
it('should stop on a breakpoint in file with spaces in its name', () =>
testBreakpointHit(path.join(TEST_PROJECT, 'folder with spaces', 'file with spaces.php'), 4)
);
it('should stop on a breakpoint identical to the entrypoint', () =>
testBreakpointHit(program, 3)
);
});
describe('exception breakpoints', () => {
const program = path.join(TEST_PROJECT, 'error.php');
beforeEach(() => Promise.all([
client.launch({program}),
client.waitForEvent('initialized')
]));
it('should support stopping only on a notice', async () => {
await client.setExceptionBreakpointsRequest({filters: ['Notice']});
const [, {threadId}] = await Promise.all([
client.configurationDoneRequest(),
assertStoppedLocation('exception', program, 6)
]);
await Promise.all([
client.continueRequest({threadId}),
client.waitForEvent('terminated')
]);
});
it('should support stopping only on a warning', async () => {
await client.setExceptionBreakpointsRequest({filters: ['Warning']});
const [{threadId}] = await Promise.all([
assertStoppedLocation('exception', program, 9),
client.configurationDoneRequest()
]);
await Promise.all([
client.continueRequest({threadId}),
client.waitForEvent('terminated')
]);
});
it('should support stopping only on an exception', async () => {
await client.setExceptionBreakpointsRequest({filters: ['Exception']});
const [, {threadId}] = await Promise.all([
client.configurationDoneRequest(),
assertStoppedLocation('exception', program, 12)
]);
await Promise.all([
client.continueRequest({threadId}),
client.waitForEvent('terminated')
]);
});
it('should support stopping on everything', async () => {
await client.setExceptionBreakpointsRequest({filters: ['*']});
// Notice
const [, {threadId}] = await Promise.all([
client.configurationDoneRequest(),
assertStoppedLocation('exception', program, 6)
]);
// Warning
await Promise.all([
client.continueRequest({threadId}),
assertStoppedLocation('exception', program, 9)
]);
// Exception
await Promise.all([
client.continueRequest({threadId}),
assertStoppedLocation('exception', program, 12)
]);
// Fatal error: uncaught exception
await Promise.all([
client.continueRequest({threadId}),
assertStoppedLocation('exception', program, 12)
]);
await Promise.all([
client.continueRequest({threadId}),
client.waitForEvent('terminated')
]);
});
it('should report the error in a virtual error scope', async () => {
await client.setExceptionBreakpointsRequest({filters: ['*']});
const [, {body: {threadId}}] = await Promise.all([
client.configurationDoneRequest(),
client.waitForEvent('stopped') as Promise<DebugProtocol.StoppedEvent>
]);
async function getErrorScope(): Promise<{name: string, type?: string, message?: string, code?: string}> {
const frameId = (await client.stackTraceRequest({threadId})).body.stackFrames[0].id;
const errorScope = (await client.scopesRequest({frameId})).body.scopes[0];
const variables = (await client.variablesRequest({variablesReference: errorScope.variablesReference})).body.variables;
const errorInfo: {name: string, type?: string, message?: string, code?: string} = {name: errorScope.name};
const type = variables.find(variable => variable.name === 'type');
if (type) {
errorInfo.type = type.value;
}
const message = variables.find(variable => variable.name === 'message');
if (message) {
errorInfo.message = message.value;
}
const code = variables.find(variable => variable.name === 'code');
if (code) {
errorInfo.code = code.value;
}
return errorInfo;
}
assert.deepEqual(await getErrorScope(), {
name: 'Notice',
type: 'Notice',
message: '"Undefined index: undefined_index"',
code: '8'
});
await Promise.all([
client.continueRequest({threadId}),
client.waitForEvent('stopped')
]);
assert.deepEqual(await getErrorScope(), {
name: 'Warning',
type: 'Warning',
message: '"Illegal offset type"',
code: '2'
});
await Promise.all([
client.continueRequest({threadId}),
client.waitForEvent('stopped')
]);
assert.deepEqual(await getErrorScope(), {
name: 'Exception',
type: 'Exception',
message: '"this is an exception"'
});
await Promise.all([
client.continueRequest({threadId}),
client.waitForEvent('stopped')
]);
const fatalErrorScope = await getErrorScope();
assert.propertyVal(fatalErrorScope, 'name', 'Fatal error');
assert.propertyVal(fatalErrorScope, 'type', 'Fatal error');
assert.match(fatalErrorScope.message, /^"Uncaught Exception/i);
assert.match(fatalErrorScope.message, /this is an exception/);
assert.match(fatalErrorScope.message, /"$/);
});
});
describe('conditional breakpoints', () => {
const program = path.join(TEST_PROJECT, 'variables.php');
it('should stop on a conditional breakpoint when condition is true', async () => {
await Promise.all([
client.launch({program}),
client.waitForEvent('initialized')
]);
const bp = (await client.setBreakpointsRequest({breakpoints: [{line: 10, condition: '$anInt === 123'}], source: {path: program}})).body.breakpoints[0];
assert.equal(bp.verified, true, 'breakpoint verification mismatch: verified');
assert.equal(bp.line, 10, 'breakpoint verification mismatch: line');
const [, {frame}] = await Promise.all([
client.configurationDoneRequest(),
assertStoppedLocation('breakpoint', program, 10)
]);
const result = (await client.evaluateRequest({context: 'watch', frameId: frame.id, expression: '$anInt'})).body.result;
assert.equal(result, 123);
});
it('should not stop on a conditional breakpoint when condition is false', async () => {
await Promise.all([
client.launch({program}),
client.waitForEvent('initialized')
]);
const bp = (await client.setBreakpointsRequest({breakpoints: [{line: 10, condition: '$anInt !== 123'}], source: {path: program}})).body.breakpoints[0];
assert.equal(bp.verified, true, 'breakpoint verification mismatch: verified');
assert.equal(bp.line, 10, 'breakpoint verification mismatch: line');
await Promise.all([
client.configurationDoneRequest(),
client.waitForEvent('terminated')
]);
});
});
describe('function breakpoints', () => {
const program = path.join(TEST_PROJECT, 'function.php');
it('should stop on a function breakpoint', async () => {
await client.launch({program});
await client.waitForEvent('initialized');
const breakpoint = (await client.setFunctionBreakpointsRequest({breakpoints: [{name: 'a_function'}]})).body.breakpoints[0];
assert.strictEqual(breakpoint.verified, true);
await Promise.all([
client.configurationDoneRequest(),
assertStoppedLocation('breakpoint', program, 5)
]);
});
});
});
describe('variables', () => {
const program = path.join(TEST_PROJECT, 'variables.php');
let localScope: DebugProtocol.Scope;
let superglobalsScope: DebugProtocol.Scope;
let constantsScope: DebugProtocol.Scope;
beforeEach(async () => {
await Promise.all([
client.launch({program}),
client.waitForEvent('initialized')
]);
await client.setBreakpointsRequest({source: {path: program}, breakpoints: [{line: 17}]});
const [, event] = await Promise.all([
client.configurationDoneRequest(),
client.waitForEvent('stopped') as Promise<DebugProtocol.StoppedEvent>
]);
const stackFrame = (await client.stackTraceRequest({threadId: event.body.threadId})).body.stackFrames[0];
const scopes = (await client.scopesRequest({frameId: stackFrame.id})).body.scopes;
localScope = scopes.find(scope => scope.name === 'Locals');
superglobalsScope = scopes.find(scope => scope.name === 'Superglobals');
constantsScope = scopes.find(scope => scope.name === 'User defined constants');
});
it('should report scopes correctly', () => {
assert.isDefined(localScope, 'Locals');
assert.isDefined(superglobalsScope, 'Superglobals');
assert.isDefined(constantsScope, 'User defined constants');
});
describe('local variables', () => {
let localVariables: DebugProtocol.Variable[];
beforeEach(async () => {
localVariables = (await client.variablesRequest({variablesReference: localScope.variablesReference})).body.variables;
});
it('should report local scalar variables correctly', async () => {
const variables: {[name: string]: string} = Object.create(null);
for (const variable of localVariables) {
variables[variable.name] = variable.value;
}
assert.propertyVal(variables, '$aBoolean', 'true');
assert.propertyVal(variables, '$aFloat', '1.23');
assert.propertyVal(variables, '$aString', '"123"');
assert.propertyVal(variables, '$anEmptyString', '""');
assert.propertyVal(variables, '$aVeryLongString', '"' + 'lol'.repeat(10000) + '"');
assert.propertyVal(variables, '$anInt', '123');
assert.propertyVal(variables, '$nullValue', 'null');
assert.propertyVal(variables, '$variableThatsNotSet', 'uninitialized');
});
it('should report arrays correctly', async () => {
const anArray = localVariables.find(variable => variable.name === '$anArray');
assert.isDefined(anArray);
assert.propertyVal(anArray, 'value', 'array(2)');
assert.property(anArray, 'variablesReference');
const items = (await client.variablesRequest({variablesReference: anArray.variablesReference})).body.variables;
assert.lengthOf(items, 2);
assert.propertyVal(items[0], 'name', '0');
assert.propertyVal(items[0], 'value', '1');
assert.propertyVal(items[1], 'name', 'test');
assert.propertyVal(items[1], 'value', '2');
});
it('should report large arrays correctly', async () => {
const aLargeArray = localVariables.find(variable => variable.name === '$aLargeArray');
assert.isDefined(aLargeArray);
assert.propertyVal(aLargeArray, 'value', 'array(100)');
assert.property(aLargeArray, 'variablesReference');
const largeArrayItems = (await client.variablesRequest({variablesReference: aLargeArray.variablesReference})).body.variables;
assert.lengthOf(largeArrayItems, 100);
assert.propertyVal(largeArrayItems[0], 'name', '0');
assert.propertyVal(largeArrayItems[0], 'value', '"test"');
assert.propertyVal(largeArrayItems[99], 'name', '99');
assert.propertyVal(largeArrayItems[99], 'value', '"test"');
});
it('should report keys with spaces correctly', async () => {
const arrayWithSpaceKey = localVariables.find(variable => variable.name === '$arrayWithSpaceKey');
assert.isDefined(arrayWithSpaceKey);
assert.propertyVal(arrayWithSpaceKey, 'value', 'array(1)');
assert.property(arrayWithSpaceKey, 'variablesReference');
const arrayWithSpaceKeyItems = (await client.variablesRequest({variablesReference: arrayWithSpaceKey.variablesReference})).body.variables;
assert.lengthOf(arrayWithSpaceKeyItems, 1);
assert.propertyVal(arrayWithSpaceKeyItems[0], 'name', 'space key');
assert.propertyVal(arrayWithSpaceKeyItems[0], 'value', '1');
});
});
it('should report user defined constants correctly', async () => {
const constants = (await client.variablesRequest({variablesReference: constantsScope.variablesReference})).body.variables;
assert.lengthOf(constants, 1);
assert.propertyVal(constants[0], 'name', 'TEST_CONSTANT');
assert.propertyVal(constants[0], 'value', '123');
});
});
describe('virtual sources', () => {
it('should break on an exception inside eval code');
it('should return the eval code with a source request');
});
describe('parallel requests', () => {
it('should report multiple requests as threads');
});
describe('evaluation', () => {
it('should return the eval result');
it('should return variable references for structured results');
});
describe.skip('output events', () => {
const program = path.join(TEST_PROJECT, 'output.php');
it('stdout and stderr events should be complete and in correct order', async () => {
await Promise.all([
client.launch({program}),
client.configurationSequence()
]);
await client.assertOutput('stdout', 'stdout output 1\nstdout output 2');
await client.assertOutput('stderr', 'stderr output 1\nstderr output 2');
});
});
});