forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugLauncher.ts
More file actions
73 lines (70 loc) · 3.4 KB
/
Copy pathdebugLauncher.ts
File metadata and controls
73 lines (70 loc) · 3.4 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
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Uri } from 'vscode';
import { IDebugService, IWorkspaceService } from '../../common/application/types';
import { EXTENSION_ROOT_DIR } from '../../common/constants';
import { IConfigurationService } from '../../common/types';
import { DebugOptions } from '../../debugger/Common/Contracts';
import { IServiceContainer } from '../../ioc/types';
import { ITestDebugLauncher, LaunchOptions, TestProvider } from './types';
@injectable()
export class DebugLauncher implements ITestDebugLauncher {
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) { }
public async launchDebugger(options: LaunchOptions) {
if (options.token && options.token!.isCancellationRequested) {
return;
}
const cwdUri = options.cwd ? Uri.file(options.cwd) : undefined;
const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
if (!workspaceService.hasWorkspaceFolders) {
throw new Error('Please open a workspace');
}
let workspaceFolder = workspaceService.getWorkspaceFolder(cwdUri!);
if (!workspaceFolder) {
workspaceFolder = workspaceService.workspaceFolders![0];
}
const cwd = cwdUri ? cwdUri.fsPath : workspaceFolder.uri.fsPath;
const configSettings = this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(Uri.file(cwd));
const useExperimentalDebugger = configSettings.unitTest.useExperimentalDebugger === true;
const debugManager = this.serviceContainer.get<IDebugService>(IDebugService);
const debuggerType = useExperimentalDebugger ? 'pythonExperimental' : 'python';
const debugArgs = this.fixArgs(options.args, options.testProvider, useExperimentalDebugger);
const program = this.getTestLauncherScript(options.testProvider, useExperimentalDebugger);
return debugManager.startDebugging(workspaceFolder, {
name: 'Debug Unit Test',
type: debuggerType,
request: 'launch',
program,
cwd,
args: debugArgs,
console: 'none',
envFile: configSettings.envFile,
debugOptions: [DebugOptions.RedirectOutput]
}).then(() => void (0));
}
private fixArgs(args: string[], testProvider: TestProvider, useExperimentalDebugger: boolean): string[] {
if (testProvider === 'unittest' && useExperimentalDebugger) {
return args.filter(item => item !== '--debug');
} else {
return args;
}
}
private getTestLauncherScript(testProvider: TestProvider, useExperimentalDebugger: boolean) {
switch (testProvider) {
case 'unittest': {
return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'PythonTools', 'visualstudio_py_testlauncher.py');
}
case 'pytest':
case 'nosetest': {
if (useExperimentalDebugger) {
return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'testlauncher.py');
} else {
return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'PythonTools', 'testlauncher.py');
}
}
default: {
throw new Error(`Unknown test provider '${testProvider}'`);
}
}
}
}