Skip to content

Commit da52069

Browse files
committed
🔨 use utils
1 parent be15ee2 commit da52069

File tree

2 files changed

+95
-22
lines changed

2 files changed

+95
-22
lines changed

src/test/debugger/module.test.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,21 @@ import * as path from 'path';
99
import { DebugClient } from 'vscode-debugadapter-testsupport';
1010
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
1111
import { noop } from '../../client/common/core.utils';
12-
import { IS_WINDOWS } from '../../client/common/platform/constants';
1312
import { DebugOptions, LaunchRequestArguments } from '../../client/debugger/Common/Contracts';
1413
import { sleep } from '../common';
1514
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
16-
import { DEBUGGER_TIMEOUT } from './common/constants';
17-
import { DebugClientEx } from './debugClient';
15+
import { createDebugAdapter } from './utils';
1816

19-
const testAdapterFilePath = path.join(EXTENSION_ROOT_DIR, 'out', 'client', 'debugger', 'mainV2.js');
2017
const workspaceDirectory = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5');
21-
let testCounter = 0;
2218
const debuggerType = 'pythonExperimental';
2319
suite(`Module Debugging - Misc tests: ${debuggerType}`, () => {
2420
let debugClient: DebugClient;
2521
setup(async function () {
2622
if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) {
2723
this.skip();
2824
}
29-
await new Promise(resolve => setTimeout(resolve, 1000));
30-
debugClient = createDebugAdapter();
31-
debugClient.defaultTimeout = DEBUGGER_TIMEOUT;
32-
await debugClient.start();
25+
const coverageDirectory = path.join(EXTENSION_ROOT_DIR, 'debug_coverage_module');
26+
debugClient = await createDebugAdapter(coverageDirectory);
3327
});
3428
teardown(async () => {
3529
// Wait for a second before starting another test (sometimes, sockets take a while to get closed).
@@ -40,19 +34,6 @@ suite(`Module Debugging - Misc tests: ${debuggerType}`, () => {
4034
} catch (ex) { }
4135
await sleep(1000);
4236
});
43-
/**
44-
* Creates the debug adapter.
45-
* We do not need to support code coverage on AppVeyor, lets use the standard test adapter.
46-
* @returns {DebugClient}
47-
*/
48-
function createDebugAdapter(): DebugClient {
49-
if (IS_WINDOWS) {
50-
return new DebugClient('node', testAdapterFilePath, debuggerType);
51-
} else {
52-
const coverageDirectory = path.join(EXTENSION_ROOT_DIR, `debug_coverage${testCounter += 1}`);
53-
return new DebugClientEx(testAdapterFilePath, debuggerType, coverageDirectory, { cwd: EXTENSION_ROOT_DIR });
54-
}
55-
}
5637
function buildLauncArgs(): LaunchRequestArguments {
5738
const env = {};
5839
// tslint:disable-next-line:no-string-literal

src/test/debugger/utils.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)