forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattach.ptvsd.test.ts
More file actions
145 lines (131 loc) · 7.24 KB
/
attach.ptvsd.test.ts
File metadata and controls
145 lines (131 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import '../../client/common/extensions';
import { ChildProcess, spawn } from 'child_process';
import * as getFreePort from 'get-port';
import * as path from 'path';
import { instance, mock } from 'ts-mockito';
import * as TypeMoq from 'typemoq';
import { DebugConfiguration, Uri } from 'vscode';
import { DebugClient } from 'vscode-debugadapter-testsupport';
import { IDocumentManager, IWorkspaceService } from '../../client/common/application/types';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { IS_WINDOWS } from '../../client/common/platform/constants';
import { FileSystem } from '../../client/common/platform/fileSystem';
import { IPlatformService } from '../../client/common/platform/types';
import { IConfigurationService } from '../../client/common/types';
import { MultiStepInputFactory } from '../../client/common/utils/multiStepInput';
import { DebuggerTypeName, PTVSD_PATH } from '../../client/debugger/constants';
import { PythonDebugConfigurationService } from '../../client/debugger/extension/configuration/debugConfigurationService';
import { AttachConfigurationResolver } from '../../client/debugger/extension/configuration/resolvers/attach';
import { IDebugConfigurationProviderFactory, IDebugConfigurationResolver } from '../../client/debugger/extension/configuration/types';
import { AttachRequestArguments, DebugOptions, LaunchRequestArguments } from '../../client/debugger/types';
import { IServiceContainer } from '../../client/ioc/types';
import { PYTHON_PATH, sleep } from '../common';
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
import { continueDebugging, createDebugAdapter } from './utils';
// tslint:disable:no-invalid-this max-func-body-length no-empty no-increment-decrement no-unused-variable no-console
const fileToDebug = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5', 'remoteDebugger-start-with-ptvsd.py');
suite('Debugging - Attach Debugger', () => {
let debugClient: DebugClient;
let proc: ChildProcess;
setup(async function () {
if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) {
this.skip();
}
this.timeout(30000);
debugClient = await createDebugAdapter();
});
teardown(async () => {
// Wait for a second before starting another test (sometimes, sockets take a while to get closed).
await sleep(1000);
try {
await debugClient.stop().catch(() => { });
} catch (ex) { }
if (proc) {
try {
proc.kill();
} catch { }
}
});
async function testAttachingToRemoteProcess(localRoot: string, remoteRoot: string, isLocalHostWindows: boolean) {
const localHostPathSeparator = isLocalHostWindows ? '\\' : '/';
const port = await getFreePort({ host: 'localhost', port: 3000 });
const env = { ...process.env };
// Set the path for PTVSD to be picked up.
// tslint:disable-next-line:no-string-literal
env['PYTHONPATH'] = PTVSD_PATH;
const pythonArgs = ['-m', 'ptvsd', '--host', 'localhost', '--wait', '--port', `${port}`, fileToDebug.fileToCommandArgument()];
proc = spawn(PYTHON_PATH, pythonArgs, { env: env, cwd: path.dirname(fileToDebug) });
const exited = new Promise(resolve => proc.once('close', resolve));
await sleep(3000);
// Send initialize, attach
const initializePromise = debugClient.initializeRequest({
adapterID: DebuggerTypeName,
linesStartAt1: true,
columnsStartAt1: true,
supportsRunInTerminalRequest: true,
pathFormat: 'path',
supportsVariableType: true,
supportsVariablePaging: true
});
const options: AttachRequestArguments & DebugConfiguration = {
name: 'attach',
request: 'attach',
localRoot,
remoteRoot,
type: DebuggerTypeName,
port: port,
host: 'localhost',
logToFile: false,
debugOptions: [DebugOptions.RedirectOutput]
};
const platformService = TypeMoq.Mock.ofType<IPlatformService>();
platformService.setup(p => p.isWindows).returns(() => isLocalHostWindows);
const serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
serviceContainer.setup(c => c.get(IPlatformService, TypeMoq.It.isAny())).returns(() => platformService.object);
const workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
const documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
const configurationService = TypeMoq.Mock.ofType<IConfigurationService>();
const launchResolver = TypeMoq.Mock.ofType<IDebugConfigurationResolver<LaunchRequestArguments>>();
const attachResolver = new AttachConfigurationResolver(workspaceService.object, documentManager.object, platformService.object, configurationService.object);
const providerFactory = TypeMoq.Mock.ofType<IDebugConfigurationProviderFactory>().object;
const fs = mock(FileSystem);
const multistepFactory = mock(MultiStepInputFactory);
const configProvider = new PythonDebugConfigurationService(attachResolver, launchResolver.object, providerFactory,
instance(multistepFactory), instance(fs));
await configProvider.resolveDebugConfiguration({ index: 0, name: 'root', uri: Uri.file(localRoot) }, options);
const attachPromise = debugClient.attachRequest(options);
await Promise.all([
initializePromise,
attachPromise,
debugClient.waitForEvent('initialized')
]);
const stdOutPromise = debugClient.assertOutput('stdout', 'this is stdout');
const stdErrPromise = debugClient.assertOutput('stderr', 'this is stderr');
// Don't use path utils, as we're building the paths manually (mimic windows paths on unix test servers and vice versa).
const localFileName = `${localRoot}${localHostPathSeparator}${path.basename(fileToDebug)}`;
const breakpointLocation = { path: localFileName, column: 1, line: 12 };
const breakpointPromise = debugClient.setBreakpointsRequest({
lines: [breakpointLocation.line],
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
source: { path: breakpointLocation.path }
});
const exceptionBreakpointPromise = debugClient.setExceptionBreakpointsRequest({ filters: [] });
const breakpointStoppedPromise = debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
await Promise.all([
breakpointPromise, exceptionBreakpointPromise,
debugClient.configurationDoneRequest(), debugClient.threadsRequest(),
stdOutPromise, stdErrPromise,
breakpointStoppedPromise
]);
await continueDebugging(debugClient);
await exited;
}
test('Confirm we are able to attach to a running program', async () => {
await testAttachingToRemoteProcess(path.dirname(fileToDebug), path.dirname(fileToDebug), IS_WINDOWS);
})
// Retry as tests can timeout on server due to connectivity issues.
.retries(3);
});