|
1 | | -import * as getFreePort from 'get-port'; |
2 | | -import { inject, injectable } from 'inversify'; |
3 | | -import * as os from 'os'; |
| 1 | +import { injectable } from 'inversify'; |
4 | 2 | import { debug, Uri, workspace } from 'vscode'; |
5 | | -import { PythonSettings } from '../../common/configSettings'; |
6 | | -import { IPythonExecutionFactory } from '../../common/process/types'; |
7 | | -import { createDeferred } from './../../common/helpers'; |
8 | 3 | import { ITestDebugLauncher, launchOptions } from './types'; |
9 | 4 |
|
10 | | -const HAND_SHAKE = `READY${os.EOL}`; |
11 | | - |
12 | 5 | @injectable() |
13 | 6 | export class DebugLauncher implements ITestDebugLauncher { |
14 | | - constructor( @inject(IPythonExecutionFactory) private pythonExecutionFactory: IPythonExecutionFactory) { } |
15 | | - public async getLaunchOptions(resource?: Uri): Promise<{ port: number, host: string }> { |
16 | | - const pythonSettings = PythonSettings.getInstance(resource); |
17 | | - const port = await getFreePort({ host: 'localhost', port: pythonSettings.unitTest.debugPort }); |
18 | | - const host = typeof pythonSettings.unitTest.debugHost === 'string' && pythonSettings.unitTest.debugHost.trim().length > 0 ? pythonSettings.unitTest.debugHost.trim() : 'localhost'; |
19 | | - return { port, host }; |
20 | | - } |
21 | 7 | public async launchDebugger(options: launchOptions) { |
| 8 | + if (options.token && options.token!.isCancellationRequested) { |
| 9 | + return; |
| 10 | + } |
22 | 11 | const cwdUri = options.cwd ? Uri.file(options.cwd) : undefined; |
23 | | - return this.pythonExecutionFactory.create(cwdUri) |
24 | | - .then(executionService => { |
25 | | - // tslint:disable-next-line:no-any |
26 | | - const def = createDeferred<void>(); |
27 | | - // tslint:disable-next-line:no-any |
28 | | - const launchDef = createDeferred<void>(); |
29 | | - |
30 | | - let outputChannelShown = false; |
31 | | - let accumulatedData: string = ''; |
32 | | - const result = executionService.execObservable(options.args, { cwd: options.cwd, mergeStdOutErr: true, token: options.token }); |
33 | | - result.out.subscribe(output => { |
34 | | - let data = output.out; |
35 | | - if (!launchDef.resolved) { |
36 | | - accumulatedData += output.out; |
37 | | - if (!accumulatedData.startsWith(HAND_SHAKE)) { |
38 | | - return; |
39 | | - } |
40 | | - // Socket server has started, lets start the vs debugger. |
41 | | - launchDef.resolve(); |
42 | | - data = accumulatedData.substring(HAND_SHAKE.length); |
43 | | - } |
44 | | - |
45 | | - if (!outputChannelShown) { |
46 | | - outputChannelShown = true; |
47 | | - options.outChannel!.show(); |
48 | | - } |
49 | | - options.outChannel!.append(data); |
50 | | - }, error => { |
51 | | - if (!def.completed) { |
52 | | - def.reject(error); |
53 | | - } |
54 | | - }, () => { |
55 | | - // Complete only when the process has completed. |
56 | | - if (!def.completed) { |
57 | | - def.resolve(); |
58 | | - } |
59 | | - }); |
60 | | - |
61 | | - launchDef.promise |
62 | | - .then(() => { |
63 | | - if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) { |
64 | | - throw new Error('Please open a workspace'); |
65 | | - } |
66 | | - let workspaceFolder = workspace.getWorkspaceFolder(cwdUri!); |
67 | | - if (!workspaceFolder) { |
68 | | - workspaceFolder = workspace.workspaceFolders[0]; |
69 | | - } |
70 | | - return debug.startDebugging(workspaceFolder, { |
71 | | - name: 'Debug Unit Test', |
72 | | - type: 'python', |
73 | | - request: 'attach', |
74 | | - localRoot: options.cwd, |
75 | | - remoteRoot: options.cwd, |
76 | | - port: options.port, |
77 | | - secret: 'my_secret', |
78 | | - host: options.host |
79 | | - }); |
80 | | - }) |
81 | | - .catch(reason => { |
82 | | - if (!def.completed) { |
83 | | - def.reject(reason); |
84 | | - } |
85 | | - }); |
86 | 12 |
|
87 | | - return def.promise; |
88 | | - }); |
| 13 | + if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) { |
| 14 | + throw new Error('Please open a workspace'); |
| 15 | + } |
| 16 | + let workspaceFolder = workspace.getWorkspaceFolder(cwdUri!); |
| 17 | + if (!workspaceFolder) { |
| 18 | + workspaceFolder = workspace.workspaceFolders[0]; |
| 19 | + } |
| 20 | + const args = options.args.slice(); |
| 21 | + const program = args.shift(); |
| 22 | + return debug.startDebugging(workspaceFolder, { |
| 23 | + name: 'Debug Unit Test', |
| 24 | + type: 'python', |
| 25 | + request: 'launch', |
| 26 | + program, |
| 27 | + cwd: cwdUri ? cwdUri.fsPath : workspaceFolder.uri.fsPath, |
| 28 | + args, |
| 29 | + console: 'none', |
| 30 | + debugOptions: ['RedirectOutput'] |
| 31 | + }).then(() => void (0)); |
89 | 32 | } |
90 | 33 | } |
0 commit comments