Skip to content

Commit

Permalink
🔨 use utils
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Apr 3, 2018
1 parent be15ee2 commit da52069
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 22 deletions.
25 changes: 3 additions & 22 deletions src/test/debugger/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,21 @@ import * as path from 'path';
import { DebugClient } from 'vscode-debugadapter-testsupport';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { noop } from '../../client/common/core.utils';
import { IS_WINDOWS } from '../../client/common/platform/constants';
import { DebugOptions, LaunchRequestArguments } from '../../client/debugger/Common/Contracts';
import { sleep } from '../common';
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
import { DEBUGGER_TIMEOUT } from './common/constants';
import { DebugClientEx } from './debugClient';
import { createDebugAdapter } from './utils';

const testAdapterFilePath = path.join(EXTENSION_ROOT_DIR, 'out', 'client', 'debugger', 'mainV2.js');
const workspaceDirectory = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5');
let testCounter = 0;
const debuggerType = 'pythonExperimental';
suite(`Module Debugging - Misc tests: ${debuggerType}`, () => {
let debugClient: DebugClient;
setup(async function () {
if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) {
this.skip();
}
await new Promise(resolve => setTimeout(resolve, 1000));
debugClient = createDebugAdapter();
debugClient.defaultTimeout = DEBUGGER_TIMEOUT;
await debugClient.start();
const coverageDirectory = path.join(EXTENSION_ROOT_DIR, 'debug_coverage_module');
debugClient = await createDebugAdapter(coverageDirectory);
});
teardown(async () => {
// Wait for a second before starting another test (sometimes, sockets take a while to get closed).
Expand All @@ -40,19 +34,6 @@ suite(`Module Debugging - Misc tests: ${debuggerType}`, () => {
} catch (ex) { }
await sleep(1000);
});
/**
* Creates the debug adapter.
* We do not need to support code coverage on AppVeyor, lets use the standard test adapter.
* @returns {DebugClient}
*/
function createDebugAdapter(): DebugClient {
if (IS_WINDOWS) {
return new DebugClient('node', testAdapterFilePath, debuggerType);
} else {
const coverageDirectory = path.join(EXTENSION_ROOT_DIR, `debug_coverage${testCounter += 1}`);
return new DebugClientEx(testAdapterFilePath, debuggerType, coverageDirectory, { cwd: EXTENSION_ROOT_DIR });
}
}
function buildLauncArgs(): LaunchRequestArguments {
const env = {};
// tslint:disable-next-line:no-string-literal
Expand Down
92 changes: 92 additions & 0 deletions src/test/debugger/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

// tslint:disable:no-any no-http-string

import { expect } from 'chai';
import * as path from 'path';
import * as request from 'request';
import { DebugClient } from 'vscode-debugadapter-testsupport';
import { DebugProtocol } from 'vscode-debugprotocol/lib/debugProtocol';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { IS_WINDOWS } from '../../client/common/platform/constants';
import { DEBUGGER_TIMEOUT } from './common/constants';
import { DebugClientEx } from './debugClient';

const testAdapterFilePath = path.join(EXTENSION_ROOT_DIR, 'out', 'client', 'debugger', 'mainV2.js');
const debuggerType = 'pythonExperimental';

/**
* Creates the debug adapter.
* We do not need to support code coverage on AppVeyor, lets use the standard test adapter.
* @returns {DebugClient}
*/
export async function createDebugAdapter(coverageDirectory: string): Promise<DebugClient> {
await new Promise(resolve => setTimeout(resolve, 1000));
let debugClient: DebugClient;
if (IS_WINDOWS) {
debugClient = new DebugClient('node', testAdapterFilePath, debuggerType);
} else {
debugClient = new DebugClientEx(testAdapterFilePath, debuggerType, coverageDirectory, { cwd: EXTENSION_ROOT_DIR });
}
debugClient.defaultTimeout = DEBUGGER_TIMEOUT;
await debugClient.start();
return debugClient;
}

export async function continueDebugging(debugClient: DebugClient) {
const threads = await debugClient.threadsRequest();
expect(threads).to.be.not.equal(undefined, 'no threads response');
expect(threads.body.threads).to.be.lengthOf(1);

await debugClient.continueRequest({ threadId: threads.body.threads[0].id });
}

export type ExpectedVariable = { type: string; name: string; value: string };
export async function validateVariablesInFrame(debugClient: DebugClient,
stackTrace: DebugProtocol.StackTraceResponse,
expectedVariables: ExpectedVariable[], numberOfScopes?: number) {

const frameId = stackTrace.body.stackFrames[0].id;

const scopes = await debugClient.scopesRequest({ frameId });
if (numberOfScopes) {
expect(scopes.body.scopes).of.length(1, 'Incorrect number of scopes');
}

const variablesReference = scopes.body.scopes[0].variablesReference;
const variables = await debugClient.variablesRequest({ variablesReference });

for (const expectedVariable of expectedVariables) {
const variable = variables.body.variables.find(item => item.name === expectedVariable.name)!;
expect(variable).to.be.not.equal('undefined', `variable '${expectedVariable.name}' is undefined`);
expect(variable.type).to.be.equal(expectedVariable.type);
expect(variable.value).to.be.equal(expectedVariable.value);
}
}
export function makeHttpRequest(uri: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
request.get(uri, (error: any, response: request.Response, body: any) => {
if (response.statusCode !== 200) {
reject(new Error(`Status code = ${response.statusCode}`));
} else {
resolve(body.toString());
}
});
});
}
export async function hitHttpBreakpoint(debugClient: DebugClient, uri: string, file: string, line: number): Promise<[DebugProtocol.StackTraceResponse, Promise<string>]> {
const breakpointLocation = { path: file, column: 1, line };
await debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});

// Make the request, we want the breakpoint to be hit.
const breakpointPromise = debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
const httpResult = makeHttpRequest(uri);
return [await breakpointPromise, httpResult];
}

0 comments on commit da52069

Please sign in to comment.