|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:no-any no-http-string |
| 7 | + |
| 8 | +import { expect } from 'chai'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as request from 'request'; |
| 11 | +import { DebugClient } from 'vscode-debugadapter-testsupport'; |
| 12 | +import { DebugProtocol } from 'vscode-debugprotocol/lib/debugProtocol'; |
| 13 | +import { EXTENSION_ROOT_DIR } from '../../client/common/constants'; |
| 14 | +import { IS_WINDOWS } from '../../client/common/platform/constants'; |
| 15 | +import { DEBUGGER_TIMEOUT } from './common/constants'; |
| 16 | +import { DebugClientEx } from './debugClient'; |
| 17 | + |
| 18 | +const testAdapterFilePath = path.join(EXTENSION_ROOT_DIR, 'out', 'client', 'debugger', 'mainV2.js'); |
| 19 | +const debuggerType = 'pythonExperimental'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Creates the debug adapter. |
| 23 | + * We do not need to support code coverage on AppVeyor, lets use the standard test adapter. |
| 24 | + * @returns {DebugClient} |
| 25 | + */ |
| 26 | +export async function createDebugAdapter(coverageDirectory: string): Promise<DebugClient> { |
| 27 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 28 | + let debugClient: DebugClient; |
| 29 | + if (IS_WINDOWS) { |
| 30 | + debugClient = new DebugClient('node', testAdapterFilePath, debuggerType); |
| 31 | + } else { |
| 32 | + debugClient = new DebugClientEx(testAdapterFilePath, debuggerType, coverageDirectory, { cwd: EXTENSION_ROOT_DIR }); |
| 33 | + } |
| 34 | + debugClient.defaultTimeout = DEBUGGER_TIMEOUT; |
| 35 | + await debugClient.start(); |
| 36 | + return debugClient; |
| 37 | +} |
| 38 | + |
| 39 | +export async function continueDebugging(debugClient: DebugClient) { |
| 40 | + const threads = await debugClient.threadsRequest(); |
| 41 | + expect(threads).to.be.not.equal(undefined, 'no threads response'); |
| 42 | + expect(threads.body.threads).to.be.lengthOf(1); |
| 43 | + |
| 44 | + await debugClient.continueRequest({ threadId: threads.body.threads[0].id }); |
| 45 | +} |
| 46 | + |
| 47 | +export type ExpectedVariable = { type: string; name: string; value: string }; |
| 48 | +export async function validateVariablesInFrame(debugClient: DebugClient, |
| 49 | + stackTrace: DebugProtocol.StackTraceResponse, |
| 50 | + expectedVariables: ExpectedVariable[], numberOfScopes?: number) { |
| 51 | + |
| 52 | + const frameId = stackTrace.body.stackFrames[0].id; |
| 53 | + |
| 54 | + const scopes = await debugClient.scopesRequest({ frameId }); |
| 55 | + if (numberOfScopes) { |
| 56 | + expect(scopes.body.scopes).of.length(1, 'Incorrect number of scopes'); |
| 57 | + } |
| 58 | + |
| 59 | + const variablesReference = scopes.body.scopes[0].variablesReference; |
| 60 | + const variables = await debugClient.variablesRequest({ variablesReference }); |
| 61 | + |
| 62 | + for (const expectedVariable of expectedVariables) { |
| 63 | + const variable = variables.body.variables.find(item => item.name === expectedVariable.name)!; |
| 64 | + expect(variable).to.be.not.equal('undefined', `variable '${expectedVariable.name}' is undefined`); |
| 65 | + expect(variable.type).to.be.equal(expectedVariable.type); |
| 66 | + expect(variable.value).to.be.equal(expectedVariable.value); |
| 67 | + } |
| 68 | +} |
| 69 | +export function makeHttpRequest(uri: string): Promise<string> { |
| 70 | + return new Promise<string>((resolve, reject) => { |
| 71 | + request.get(uri, (error: any, response: request.Response, body: any) => { |
| 72 | + if (response.statusCode !== 200) { |
| 73 | + reject(new Error(`Status code = ${response.statusCode}`)); |
| 74 | + } else { |
| 75 | + resolve(body.toString()); |
| 76 | + } |
| 77 | + }); |
| 78 | + }); |
| 79 | +} |
| 80 | +export async function hitHttpBreakpoint(debugClient: DebugClient, uri: string, file: string, line: number): Promise<[DebugProtocol.StackTraceResponse, Promise<string>]> { |
| 81 | + const breakpointLocation = { path: file, column: 1, line }; |
| 82 | + await debugClient.setBreakpointsRequest({ |
| 83 | + lines: [breakpointLocation.line], |
| 84 | + breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }], |
| 85 | + source: { path: breakpointLocation.path } |
| 86 | + }); |
| 87 | + |
| 88 | + // Make the request, we want the breakpoint to be hit. |
| 89 | + const breakpointPromise = debugClient.assertStoppedLocation('breakpoint', breakpointLocation); |
| 90 | + const httpResult = makeHttpRequest(uri); |
| 91 | + return [await breakpointPromise, httpResult]; |
| 92 | +} |
0 commit comments