Skip to content

Commit df2d4d3

Browse files
authored
Enable debugger attach test (microsoft#873)
Fix microsoft#598
1 parent 58be8f8 commit df2d4d3

2 files changed

Lines changed: 33 additions & 44 deletions

File tree

src/test/debugger/attach.test.ts

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { expect, use } from 'chai';
5-
import * as chaiAsPromised from 'chai-as-promised';
4+
import { expect } from 'chai';
65
import { ChildProcess } from 'child_process';
76
import * as getFreePort from 'get-port';
8-
import { EOL } from 'os';
97
import * as path from 'path';
10-
import { ThreadEvent } from 'vscode-debugadapter';
118
import { DebugClient } from 'vscode-debugadapter-testsupport';
129
import { createDeferred } from '../../client/common/helpers';
1310
import { BufferDecoder } from '../../client/common/process/decoder';
1411
import { ProcessService } from '../../client/common/process/proc';
1512
import { AttachRequestArguments } from '../../client/debugger/Common/Contracts';
1613
import { initialize } from '../initialize';
1714

18-
use(chaiAsPromised);
15+
// tslint:disable:max-func-body-length no-empty
1916

2017
const fileToDebug = path.join(__dirname, '..', '..', '..', 'src', 'testMultiRootWkspc', 'workspace5', 'remoteDebugger.py');
2118
const ptvsdPath = path.join(__dirname, '..', '..', '..', 'pythonFiles', 'PythonTools');
2219
const DEBUG_ADAPTER = path.join(__dirname, '..', '..', 'client', 'debugger', 'Main.js');
2320

24-
// tslint:disable-next-line:max-func-body-length
2521
suite('Attach Debugger', () => {
2622
let debugClient: DebugClient;
2723
let procToKill: ChildProcess;
28-
suiteSetup(function () {
29-
// tslint:disable-next-line:no-invalid-this
30-
this.skip();
31-
return initialize();
32-
});
24+
suiteSetup(initialize);
3325

3426
setup(async () => {
3527
await new Promise(resolve => setTimeout(resolve, 1000));
@@ -40,11 +32,12 @@ suite('Attach Debugger', () => {
4032
// Wait for a second before starting another test (sometimes, sockets take a while to get closed).
4133
await new Promise(resolve => setTimeout(resolve, 1000));
4234
try {
43-
debugClient.stop();
44-
// tslint:disable-next-line:no-empty
35+
await debugClient.stop().catch(() => { });
4536
} catch (ex) { }
4637
if (procToKill) {
47-
procToKill.kill();
38+
try {
39+
procToKill.kill();
40+
} catch { }
4841
}
4942
});
5043
test('Confirm we are able to attach to a running program', async () => {
@@ -66,35 +59,23 @@ suite('Attach Debugger', () => {
6659
const result = procService.execObservable('python', [fileToDebug, port.toString()], { env: customEnv, cwd: path.dirname(fileToDebug) });
6760
procToKill = result.proc;
6861

69-
const completed = createDeferred();
7062
const expectedOutputs = [
7163
{ value: 'start', deferred: createDeferred() },
72-
{ value: 'Peter Smith', deferred: createDeferred() },
64+
{ value: 'attached', deferred: createDeferred() },
7365
{ value: 'end', deferred: createDeferred() }
7466
];
7567
const startOutputReceived = expectedOutputs[0].deferred.promise;
76-
const firstOutputReceived = expectedOutputs[1].deferred.promise;
77-
const secondOutputReceived = expectedOutputs[2].deferred.promise;
68+
const attachedOutputReceived = expectedOutputs[1].deferred.promise;
69+
const lastOutputReceived = expectedOutputs[2].deferred.promise;
7870

7971
result.out.subscribe(output => {
8072
if (expectedOutputs[0].value === output.out) {
8173
expectedOutputs.shift()!.deferred.resolve();
8274
}
83-
}, ex => {
84-
completed.reject(ex);
85-
}, () => {
86-
completed.resolve();
8775
});
8876

8977
await startOutputReceived;
9078

91-
const threadIdPromise = createDeferred<number>();
92-
debugClient.on('thread', (data: ThreadEvent) => {
93-
if (data.body.reason === 'started') {
94-
threadIdPromise.resolve(data.body.threadId);
95-
}
96-
});
97-
9879
const initializePromise = debugClient.initializeRequest({
9980
adapterID: 'python',
10081
linesStartAt1: true,
@@ -105,21 +86,28 @@ suite('Attach Debugger', () => {
10586
await debugClient.attachRequest(args);
10687
await initializePromise;
10788

108-
// Wait till we get the thread of the program.
109-
const threadId = await threadIdPromise.promise;
110-
expect(threadId).to.be.greaterThan(0, 'ThreadId not received');
89+
// Wait till we attach.
90+
await attachedOutputReceived;
11191

112-
// Continue the program.
113-
await debugClient.continueRequest({ threadId });
92+
// Add a breakpoint.
93+
const breakpointLocation = { path: fileToDebug, column: 0, line: 16 };
94+
await debugClient.setBreakpointsRequest({
95+
lines: [breakpointLocation.line],
96+
breakpoints: [{ line: breakpointLocation.line, column: breakpointLocation.column }],
97+
source: { path: breakpointLocation.path }
98+
});
11499

115-
// Value for input prompt.
116-
result.proc.stdin.write(`Peter Smith${EOL}`);
117-
await firstOutputReceived;
100+
await debugClient.assertStoppedLocation('breakpoint', breakpointLocation);
118101

119-
result.proc.stdin.write(`${EOL}`);
120-
await secondOutputReceived;
121-
await completed.promise;
102+
// Get thread to continue.
103+
const threads = await debugClient.threadsRequest();
104+
expect(threads).to.be.not.equal(undefined, 'no threads response');
105+
expect(threads.body.threads).to.be.lengthOf(1);
106+
107+
// Continue the program.
108+
await debugClient.continueRequest({ threadId: threads.body.threads[0].id });
122109

110+
await lastOutputReceived;
123111
await debugClient.waitForEvent('terminated');
124112
});
125113
});

src/testMultiRootWkspc/workspace5/remoteDebugger.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
sys.stdout.write('start')
66
sys.stdout.flush()
7-
address = ('0.0.0.0', int(sys.argv[1]))
7+
address = ('127.0.0.1', int(sys.argv[1]))
88
ptvsd.enable_attach('super_secret', address)
99
ptvsd.wait_for_attach()
1010

11-
name = input()
12-
sys.stdout.write(name)
11+
sys.stdout.write('attached')
1312
sys.stdout.flush()
14-
input()
13+
# Give the debugger some time to add a breakpoint.
14+
time.sleep(2)
15+
1516
sys.stdout.write('end')
1617
sys.stdout.flush()

0 commit comments

Comments
 (0)