|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:no-suspicious-comment max-func-body-length no-invalid-this no-var-requires no-require-imports no-any |
| 7 | + |
| 8 | +import { expect } from 'chai'; |
| 9 | +import { ChildProcess, spawn } from 'child_process'; |
| 10 | +import * as getFreePort from 'get-port'; |
| 11 | +import { connect, Socket } from 'net'; |
| 12 | +import * as path from 'path'; |
| 13 | +import { PassThrough } from 'stream'; |
| 14 | +import { Message } from 'vscode-debugadapter/lib/messages'; |
| 15 | +import { DebugProtocol } from 'vscode-debugprotocol'; |
| 16 | +import { EXTENSION_ROOT_DIR } from '../../client/common/constants'; |
| 17 | +import { createDeferred } from '../../client/common/helpers'; |
| 18 | +import { ProtocolParser } from '../../client/debugger/Common/protocolParser'; |
| 19 | +import { ProtocolMessageWriter } from '../../client/debugger/Common/protocolWriter'; |
| 20 | +import { PythonDebugger } from '../../client/debugger/mainV2'; |
| 21 | +import { sleep } from '../common'; |
| 22 | +import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize'; |
| 23 | + |
| 24 | +class Request extends Message implements DebugProtocol.InitializeRequest { |
| 25 | + // tslint:disable-next-line:no-banned-terms |
| 26 | + public arguments: any; |
| 27 | + constructor(public command: string, args: any) { |
| 28 | + super('request'); |
| 29 | + this.arguments = args; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +suite('Debugging - Capabilities', () => { |
| 34 | + let disposables: { dispose?: Function; destroy?: Function }[]; |
| 35 | + let proc: ChildProcess; |
| 36 | + setup(async function () { |
| 37 | + if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) { |
| 38 | + this.skip(); |
| 39 | + } |
| 40 | + disposables = []; |
| 41 | + }); |
| 42 | + teardown(() => { |
| 43 | + disposables.forEach(disposable => { |
| 44 | + try { |
| 45 | + disposable.dispose!(); |
| 46 | + // tslint:disable-next-line:no-empty |
| 47 | + } catch { } |
| 48 | + try { |
| 49 | + disposable.destroy!(); |
| 50 | + // tslint:disable-next-line:no-empty |
| 51 | + } catch { } |
| 52 | + }); |
| 53 | + try { |
| 54 | + proc.kill(); |
| 55 | + // tslint:disable-next-line:no-empty |
| 56 | + } catch { } |
| 57 | + }); |
| 58 | + test('Compare capabilities', async () => { |
| 59 | + const protocolWriter = new ProtocolMessageWriter(); |
| 60 | + const initializeRequest: DebugProtocol.InitializeRequest = new Request('initialize', { pathFormat: 'path' }); |
| 61 | + |
| 62 | + const debugClient = new PythonDebugger(undefined as any); |
| 63 | + const inStream = new PassThrough(); |
| 64 | + const outStream = new PassThrough(); |
| 65 | + disposables.push(inStream); |
| 66 | + disposables.push(outStream); |
| 67 | + debugClient.start(inStream, outStream); |
| 68 | + const debugClientProtocolParser = new ProtocolParser(); |
| 69 | + debugClientProtocolParser.connect(outStream); |
| 70 | + disposables.push(debugClientProtocolParser); |
| 71 | + const expectedResponsePromise = new Promise<DebugProtocol.InitializeResponse>(resolve => debugClientProtocolParser.once('response_initialize', resolve)); |
| 72 | + protocolWriter.write(inStream, initializeRequest); |
| 73 | + const expectedResponse = await expectedResponsePromise; |
| 74 | + |
| 75 | + const host = 'localhost'; |
| 76 | + const port = await getFreePort({ host }); |
| 77 | + const env = { ...process.env }; |
| 78 | + env.PYTHONPATH = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'ptvsd'); |
| 79 | + proc = spawn('python', ['-m', 'ptvsd', '--server', '--port', `${port}`, '--file', 'someFile.py'], { cwd: __dirname, env }); |
| 80 | + // Wait for the socket server to start. |
| 81 | + // Keep trying till we timeout. |
| 82 | + let socket: Socket | undefined; |
| 83 | + for (let index = 0; index < 1000; index += 1) { |
| 84 | + try { |
| 85 | + const connected = createDeferred(); |
| 86 | + socket = connect({ port, host }, () => connected.resolve(socket)); |
| 87 | + socket.on('error', connected.reject.bind(connected)); |
| 88 | + await connected.promise; |
| 89 | + break; |
| 90 | + } catch { |
| 91 | + await sleep(500); |
| 92 | + } |
| 93 | + } |
| 94 | + const protocolParser = new ProtocolParser(); |
| 95 | + protocolParser.connect(socket!); |
| 96 | + disposables.push(protocolParser); |
| 97 | + const actualResponsePromise = new Promise<DebugProtocol.InitializeResponse>(resolve => protocolParser.once('response_initialize', resolve)); |
| 98 | + protocolWriter.write(socket!, initializeRequest); |
| 99 | + const actualResponse = await actualResponsePromise; |
| 100 | + |
| 101 | + expect(actualResponse.body).to.deep.equal(expectedResponse.body); |
| 102 | + }); |
| 103 | +}); |
0 commit comments