|
1 | | -import { injectable } from 'inversify'; |
2 | | -import { debug, Uri, workspace } from 'vscode'; |
3 | | -import { ITestDebugLauncher, launchOptions } from './types'; |
| 1 | +import { inject, injectable } from 'inversify'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { Uri } from 'vscode'; |
| 4 | +import { IDebugService, IWorkspaceService } from '../../common/application/types'; |
| 5 | +import { EXTENSION_ROOT_DIR } from '../../common/constants'; |
| 6 | +import { IConfigurationService } from '../../common/types'; |
| 7 | +import { IServiceContainer } from '../../ioc/types'; |
| 8 | +import { ITestDebugLauncher, LaunchOptions, TestProvider } from './types'; |
4 | 9 |
|
5 | 10 | @injectable() |
6 | 11 | export class DebugLauncher implements ITestDebugLauncher { |
7 | | - public async launchDebugger(options: launchOptions) { |
| 12 | + constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) { } |
| 13 | + public async launchDebugger(options: LaunchOptions) { |
8 | 14 | if (options.token && options.token!.isCancellationRequested) { |
9 | 15 | return; |
10 | 16 | } |
11 | 17 | const cwdUri = options.cwd ? Uri.file(options.cwd) : undefined; |
12 | | - |
13 | | - if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) { |
| 18 | + const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService); |
| 19 | + if (!workspaceService.hasWorkspaceFolders) { |
14 | 20 | throw new Error('Please open a workspace'); |
15 | 21 | } |
16 | | - let workspaceFolder = workspace.getWorkspaceFolder(cwdUri!); |
| 22 | + let workspaceFolder = workspaceService.getWorkspaceFolder(cwdUri!); |
17 | 23 | if (!workspaceFolder) { |
18 | | - workspaceFolder = workspace.workspaceFolders[0]; |
| 24 | + workspaceFolder = workspaceService.workspaceFolders![0]; |
19 | 25 | } |
20 | | - const args = options.args.slice(); |
21 | | - const program = args.shift(); |
22 | | - return debug.startDebugging(workspaceFolder, { |
| 26 | + |
| 27 | + const cwd = cwdUri ? cwdUri.fsPath : workspaceFolder.uri.fsPath; |
| 28 | + const configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(Uri.file(cwd)); |
| 29 | + const useExperimentalDebugger = configurationService.unitTest.useExperimentalDebugger === true; |
| 30 | + const debugManager = this.serviceContainer.get<IDebugService>(IDebugService); |
| 31 | + const debuggerType = useExperimentalDebugger ? 'pythonExperimental' : 'python'; |
| 32 | + const debugArgs = this.fixArgs(options.args, options.testProvider, useExperimentalDebugger); |
| 33 | + const program = this.getTestLauncherScript(options.testProvider, useExperimentalDebugger); |
| 34 | + |
| 35 | + return debugManager.startDebugging(workspaceFolder, { |
23 | 36 | name: 'Debug Unit Test', |
24 | | - type: 'python', |
| 37 | + type: debuggerType, |
25 | 38 | request: 'launch', |
26 | 39 | program, |
27 | | - cwd: cwdUri ? cwdUri.fsPath : workspaceFolder.uri.fsPath, |
28 | | - args, |
| 40 | + cwd, |
| 41 | + args: debugArgs, |
29 | 42 | console: 'none', |
30 | 43 | debugOptions: ['RedirectOutput'] |
31 | 44 | }).then(() => void (0)); |
32 | 45 | } |
| 46 | + private fixArgs(args: string[], testProvider: TestProvider, useExperimentalDebugger: boolean): string[] { |
| 47 | + if (testProvider === 'unittest' && useExperimentalDebugger) { |
| 48 | + return args.filter(item => item !== '--debug'); |
| 49 | + } else { |
| 50 | + return args; |
| 51 | + } |
| 52 | + } |
| 53 | + private getTestLauncherScript(testProvider: TestProvider, useExperimentalDebugger: boolean) { |
| 54 | + switch (testProvider) { |
| 55 | + case 'unittest': { |
| 56 | + return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'PythonTools', 'visualstudio_py_testlauncher.py'); |
| 57 | + } |
| 58 | + case 'pytest': |
| 59 | + case 'nosetest': { |
| 60 | + if (useExperimentalDebugger) { |
| 61 | + return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'testlauncher.py'); |
| 62 | + } else { |
| 63 | + return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'PythonTools', 'testlauncher.py'); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + default: { |
| 68 | + throw new Error(`Unknown test provider '${testProvider}'`); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
33 | 72 | } |
0 commit comments