forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapabilities.test.ts
More file actions
132 lines (119 loc) · 5.39 KB
/
capabilities.test.ts
File metadata and controls
132 lines (119 loc) · 5.39 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-suspicious-comment max-func-body-length no-invalid-this no-var-requires no-require-imports no-any no-object-literal-type-assertion no-banned-terms
import { expect } from 'chai';
import { ChildProcess, spawn } from 'child_process';
import * as getFreePort from 'get-port';
import { Socket } from 'net';
import * as path from 'path';
import { Message } from 'vscode-debugadapter/lib/messages';
import { DebugProtocol } from 'vscode-debugprotocol';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { createDeferred, sleep } from '../../client/common/utils/async';
import { noop } from '../../client/common/utils/misc';
import { PTVSD_PATH } from '../../client/debugger/constants';
import { ProtocolParser } from '../../client/debugger/debugAdapter/Common/protocolParser';
import { ProtocolMessageWriter } from '../../client/debugger/debugAdapter/Common/protocolWriter';
import { PythonDebugger } from '../../client/debugger/debugAdapter/main';
import { PYTHON_PATH } from '../common';
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
const fileToDebug = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5', 'remoteDebugger-start-with-ptvsd-nowait.py');
suite('Debugging - Capabilities', function () {
this.timeout(30000);
let disposables: { dispose?: Function; destroy?: Function }[];
let proc: ChildProcess;
setup(function () {
if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) {
this.skip();
}
disposables = [];
});
teardown(() => {
disposables.forEach(disposable => {
try {
disposable.dispose!();
} catch {
noop();
}
try {
disposable.destroy!();
} catch {
noop();
}
});
try {
proc.kill();
} catch {
noop();
}
});
function createRequest(cmd: string, requestArgs: any) {
return new class extends Message implements DebugProtocol.InitializeRequest {
public arguments: any;
constructor(public command: string, args: any) {
super('request');
this.arguments = args;
}
}(cmd, requestArgs);
}
function createDebugSession() {
return new class extends PythonDebugger {
constructor() {
super({} as any);
}
public getInitializeResponseFromDebugAdapter() {
let initializeResponse = {
body: {}
} as DebugProtocol.InitializeResponse;
this.sendResponse = resp => initializeResponse = resp;
this.initializeRequest(initializeResponse, { supportsRunInTerminalRequest: true, adapterID: '' });
return initializeResponse;
}
}();
}
test('Compare capabilities', async () => {
const customDebugger = createDebugSession();
const expectedResponse = customDebugger.getInitializeResponseFromDebugAdapter();
const protocolWriter = new ProtocolMessageWriter();
const initializeRequest: DebugProtocol.InitializeRequest = createRequest('initialize', { pathFormat: 'path' });
const host = 'localhost';
const port = await getFreePort({ host, port: 3000 });
const env = { ...process.env };
env.PYTHONPATH = PTVSD_PATH;
proc = spawn(PYTHON_PATH, ['-m', 'ptvsd', '--host', 'localhost', '--wait', '--port', `${port}`, '--file', fileToDebug], { cwd: path.dirname(fileToDebug), env });
await sleep(3000);
const connected = createDeferred();
const socket = new Socket();
socket.on('error', connected.reject.bind(connected));
socket.connect({ port, host }, () => connected.resolve(socket));
await connected.promise;
const protocolParser = new ProtocolParser();
protocolParser.connect(socket!);
disposables.push(protocolParser);
const actualResponsePromise = new Promise<DebugProtocol.InitializeResponse>(resolve => protocolParser.once('response_initialize', resolve));
protocolWriter.write(socket, initializeRequest);
const actualResponse = await actualResponsePromise;
const attachRequest: DebugProtocol.AttachRequest = createRequest('attach', {
name: 'attach',
request: 'attach',
type: 'python',
port: port,
host: 'localhost',
logToFile: false,
debugOptions: []
});
const attached = new Promise(resolve => protocolParser.once('response_attach', resolve));
protocolWriter.write(socket, attachRequest);
await attached;
const configRequest: DebugProtocol.ConfigurationDoneRequest = createRequest('configurationDone', {});
const configured = new Promise(resolve => protocolParser.once('response_configurationDone', resolve));
protocolWriter.write(socket, configRequest);
await configured;
protocolParser.dispose();
// supportsDebuggerProperties is not documented, most probably a VS specific item.
const body: any = actualResponse.body;
delete body.supportsDebuggerProperties;
expect(actualResponse.body).to.deep.equal(expectedResponse.body);
});
});