33
44'use strict' ;
55
6- // tslint:disable:no-suspicious-comment max-func-body-length no-invalid-this no-var-requires no-require-imports no-any
6+ // 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
77
88import { expect } from 'chai' ;
99import { ChildProcess , spawn } from 'child_process' ;
1010import * as getFreePort from 'get-port' ;
1111import { Socket } from 'net' ;
1212import * as path from 'path' ;
13- import { PassThrough } from 'stream' ;
1413import { Message } from 'vscode-debugadapter/lib/messages' ;
1514import { DebugProtocol } from 'vscode-debugprotocol' ;
1615import { EXTENSION_ROOT_DIR } from '../../client/common/constants' ;
@@ -19,71 +18,79 @@ import { ProtocolParser } from '../../client/debugger/Common/protocolParser';
1918import { ProtocolMessageWriter } from '../../client/debugger/Common/protocolWriter' ;
2019import { PythonDebugger } from '../../client/debugger/mainV2' ;
2120import { createDeferred , sleep } from '../../utils/async' ;
21+ import { noop } from '../../utils/misc' ;
2222import { PYTHON_PATH } from '../common' ;
2323import { IS_MULTI_ROOT_TEST , TEST_DEBUGGER } from '../initialize' ;
2424
25- class Request extends Message implements DebugProtocol . InitializeRequest {
26- // tslint:disable-next-line:no-banned-terms
27- public arguments : any ;
28- constructor ( public command : string , args : any ) {
29- super ( 'request' ) ;
30- this . arguments = args ;
31- }
32- }
33-
34- const fileToDebug = path . join ( EXTENSION_ROOT_DIR , 'src' , 'testMultiRootWkspc' , 'workspace5' , 'remoteDebugger-start-with-ptvsd.py' ) ;
25+ const fileToDebug = path . join ( EXTENSION_ROOT_DIR , 'src' , 'testMultiRootWkspc' , 'workspace5' , 'remoteDebugger-start-with-ptvsd-nowait.py' ) ;
3526
36- suite ( 'Debugging - Capabilities' , ( ) => {
27+ suite ( 'Debugging - Capabilities' , function ( ) {
28+ this . timeout ( 30000 ) ;
3729 let disposables : { dispose ?: Function ; destroy ?: Function } [ ] ;
3830 let proc : ChildProcess ;
39- setup ( async function ( ) {
40- this . skip ( ) ;
41- return ;
42-
31+ setup ( function ( ) {
32+ return this . skip ( ) ;
4333 if ( ! IS_MULTI_ROOT_TEST || ! TEST_DEBUGGER ) {
4434 this . skip ( ) ;
4535 }
46- this . timeout ( 30000 ) ;
4736 disposables = [ ] ;
4837 } ) ;
4938 teardown ( ( ) => {
5039 disposables . forEach ( disposable => {
5140 try {
5241 disposable . dispose ! ( ) ;
53- // tslint:disable-next-line:no-empty
54- } catch { }
42+ } catch {
43+ noop ( ) ;
44+ }
5545 try {
5646 disposable . destroy ! ( ) ;
57- // tslint:disable-next-line:no-empty
58- } catch { }
47+ } catch {
48+ noop ( ) ;
49+ }
5950 } ) ;
6051 try {
6152 proc . kill ( ) ;
62- // tslint:disable-next-line:no-empty
63- } catch { }
53+ } catch {
54+ noop ( ) ;
55+ }
6456 } ) ;
65- test ( 'Compare capabilities' , async ( ) => {
66- const protocolWriter = new ProtocolMessageWriter ( ) ;
67- const initializeRequest : DebugProtocol . InitializeRequest = new Request ( 'initialize' , { pathFormat : 'path' } ) ;
57+ function createRequest ( cmd : string , requestArgs : any ) {
58+ return new class extends Message implements DebugProtocol . InitializeRequest {
59+ public arguments : any ;
60+ constructor ( public command : string , args : any ) {
61+ super ( 'request' ) ;
62+ this . arguments = args ;
63+ }
64+ } ( cmd , requestArgs ) ;
65+ }
66+ function createDebugSession ( ) {
67+ return new class extends PythonDebugger {
68+ constructor ( ) {
69+ super ( { } as any ) ;
70+ }
6871
69- const debugClient = new PythonDebugger ( undefined as any ) ;
70- const inStream = new PassThrough ( ) ;
71- const outStream = new PassThrough ( ) ;
72- disposables . push ( inStream ) ;
73- disposables . push ( outStream ) ;
74- debugClient . start ( inStream , outStream ) ;
75- const debugClientProtocolParser = new ProtocolParser ( ) ;
76- debugClientProtocolParser . connect ( outStream ) ;
77- disposables . push ( debugClientProtocolParser ) ;
78- const expectedResponsePromise = new Promise < DebugProtocol . InitializeResponse > ( resolve => debugClientProtocolParser . once ( 'response_initialize' , resolve ) ) ;
79- protocolWriter . write ( inStream , initializeRequest ) ;
80- const expectedResponse = await expectedResponsePromise ;
72+ public getInitializeResponseFromDebugAdapter ( ) {
73+ let initializeResponse = {
74+ body : { }
75+ } as DebugProtocol . InitializeResponse ;
76+ this . sendResponse = resp => initializeResponse = resp ;
8177
78+ this . initializeRequest ( initializeResponse , { supportsRunInTerminalRequest : true , adapterID : '' } ) ;
79+ return initializeResponse ;
80+ }
81+ } ( ) ;
82+ }
83+ test ( 'Compare capabilities' , async ( ) => {
84+ const customDebugger = createDebugSession ( ) ;
85+ const expectedResponse = customDebugger . getInitializeResponseFromDebugAdapter ( ) ;
86+
87+ const protocolWriter = new ProtocolMessageWriter ( ) ;
88+ const initializeRequest : DebugProtocol . InitializeRequest = createRequest ( 'initialize' , { pathFormat : 'path' } ) ;
8289 const host = 'localhost' ;
8390 const port = await getFreePort ( { host, port : 3000 } ) ;
8491 const env = { ...process . env } ;
8592 env . PYTHONPATH = PTVSD_PATH ;
86- proc = spawn ( PYTHON_PATH , [ '-m' , 'ptvsd' , '--server' , '--port' , `${ port } ` , '--file' , fileToDebug ] , { cwd : path . dirname ( fileToDebug ) , env } ) ;
93+ proc = spawn ( PYTHON_PATH , [ '-m' , 'ptvsd' , '--server' , '--wait' , '-- port', `${ port } ` , '--file' , fileToDebug ] , { cwd : path . dirname ( fileToDebug ) , env } ) ;
8794 await sleep ( 3000 ) ;
8895
8996 const connected = createDeferred ( ) ;
@@ -95,9 +102,29 @@ suite('Debugging - Capabilities', () => {
95102 protocolParser . connect ( socket ! ) ;
96103 disposables . push ( protocolParser ) ;
97104 const actualResponsePromise = new Promise < DebugProtocol . InitializeResponse > ( resolve => protocolParser . once ( 'response_initialize' , resolve ) ) ;
98- protocolWriter . write ( socket ! , initializeRequest ) ;
105+ protocolWriter . write ( socket , initializeRequest ) ;
99106 const actualResponse = await actualResponsePromise ;
100107
108+ const attachRequest : DebugProtocol . AttachRequest = createRequest ( 'attach' , {
109+ name : 'attach' ,
110+ request : 'attach' ,
111+ type : 'python' ,
112+ port : port ,
113+ host : 'localhost' ,
114+ logToFile : false ,
115+ debugOptions : [ ]
116+ } ) ;
117+ const attached = new Promise ( resolve => protocolParser . once ( 'response_attach' , resolve ) ) ;
118+ protocolWriter . write ( socket , attachRequest ) ;
119+ await attached ;
120+
121+ const configRequest : DebugProtocol . ConfigurationDoneRequest = createRequest ( 'configurationDone' , { } ) ;
122+ const configured = new Promise ( resolve => protocolParser . once ( 'response_configurationDone' , resolve ) ) ;
123+ protocolWriter . write ( socket , configRequest ) ;
124+ await configured ;
125+
126+ protocolParser . dispose ( ) ;
127+
101128 // supportsDebuggerProperties is not documented, most probably a VS specific item.
102129 const body : any = actualResponse . body ;
103130 delete body . supportsDebuggerProperties ;
0 commit comments