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
199 lines (184 loc) · 7.86 KB
/
debugLauncher.ts
File metadata and controls
199 lines (184 loc) · 7.86 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { inject, injectable, named } from 'inversify';
import { parse } from 'jsonc-parser';
import * as path from 'path';
import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
import { IApplicationShell, IDebugService, IWorkspaceService } from '../../common/application/types';
import { EXTENSION_ROOT_DIR } from '../../common/constants';
import { traceError } from '../../common/logger';
import { IFileSystem } from '../../common/platform/types';
import { IConfigurationService, IPythonSettings } from '../../common/types';
import { noop } from '../../common/utils/misc';
import { DebuggerTypeName } from '../../debugger/constants';
import { IDebugConfigurationResolver } from '../../debugger/extension/configuration/types';
import { LaunchRequestArguments } from '../../debugger/types';
import { IServiceContainer } from '../../ioc/types';
import { ITestDebugConfig, ITestDebugLauncher, LaunchOptions, TestProvider } from './types';
@injectable()
export class DebugLauncher implements ITestDebugLauncher {
private readonly configService: IConfigurationService;
private readonly workspaceService: IWorkspaceService;
private readonly fs: IFileSystem;
constructor(
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(IDebugConfigurationResolver) @named('launch') private readonly launchResolver: IDebugConfigurationResolver<LaunchRequestArguments>
) {
this.configService = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
this.fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
}
public async launchDebugger(options: LaunchOptions) {
if (options.token && options.token!.isCancellationRequested) {
return;
}
const workspaceFolder = this.resolveWorkspaceFolder(options.cwd);
const launchArgs = await this.getLaunchArgs(
options,
workspaceFolder,
this.configService.getSettings(workspaceFolder.uri)
);
const debugManager = this.serviceContainer.get<IDebugService>(IDebugService);
return debugManager.startDebugging(workspaceFolder, launchArgs)
.then(noop, ex => traceError('Failed to start debugging tests', ex));
}
public async readAllDebugConfigs(workspaceFolder: WorkspaceFolder): Promise<DebugConfiguration[]> {
const filename = path.join(workspaceFolder.uri.fsPath, '.vscode', 'launch.json');
if (!(await this.fs.fileExists(filename))) {
return [];
}
try {
const text = await this.fs.readFile(filename);
const parsed = parse(text, [], { allowTrailingComma: true, disallowComments: false });
if (!parsed.version || !parsed.configurations || !Array.isArray(parsed.configurations)) {
throw Error('malformed launch.json');
}
// We do not bother ensuring each item is a DebugConfiguration...
return parsed.configurations;
} catch (exc) {
traceError('could not get debug config', exc);
const appShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
await appShell.showErrorMessage('Could not load unit test config from launch.json');
return [];
}
}
private resolveWorkspaceFolder(cwd: string): WorkspaceFolder {
if (!this.workspaceService.hasWorkspaceFolders) {
throw new Error('Please open a workspace');
}
const cwdUri = cwd ? Uri.file(cwd) : undefined;
let workspaceFolder = this.workspaceService.getWorkspaceFolder(cwdUri);
if (!workspaceFolder) {
workspaceFolder = this.workspaceService.workspaceFolders![0];
}
return workspaceFolder;
}
private async getLaunchArgs(
options: LaunchOptions,
workspaceFolder: WorkspaceFolder,
configSettings: IPythonSettings
): Promise<LaunchRequestArguments> {
let debugConfig = await this.readDebugConfig(workspaceFolder);
if (!debugConfig) {
debugConfig = {
name: 'Debug Unit Test',
type: 'python',
request: 'test',
subProcess: true
};
}
if (!debugConfig.rules) {
debugConfig.rules = [];
}
debugConfig.rules.push({
path: path.join(EXTENSION_ROOT_DIR, 'pythonFiles'),
include: false
});
this.applyDefaults(debugConfig!, workspaceFolder, configSettings);
return this.convertConfigToArgs(debugConfig!, workspaceFolder, options);
}
private async readDebugConfig(workspaceFolder: WorkspaceFolder): Promise<ITestDebugConfig | undefined> {
const configs = await this.readAllDebugConfigs(workspaceFolder);
for (const cfg of configs) {
if (!cfg.name || cfg.type !== DebuggerTypeName || cfg.request !== 'test') {
continue;
}
// Return the first one.
return cfg as ITestDebugConfig;
}
return undefined;
}
private applyDefaults(
cfg: ITestDebugConfig,
workspaceFolder: WorkspaceFolder,
configSettings: IPythonSettings
) {
// cfg.pythonPath is handled by LaunchConfigurationResolver.
// Default value of justMyCode is not provided intentionally, for now we derive its value required for launchArgs using debugStdLib
// Have to provide it if and when we remove complete support for debugStdLib
if (!cfg.console) {
cfg.console = 'internalConsole';
}
if (!cfg.cwd) {
cfg.cwd = workspaceFolder.uri.fsPath;
}
if (!cfg.env) {
cfg.env = {};
}
if (!cfg.envFile) {
cfg.envFile = configSettings.envFile;
}
if (cfg.stopOnEntry === undefined) {
cfg.stopOnEntry = false;
}
cfg.showReturnValue = cfg.showReturnValue !== false;
if (cfg.redirectOutput === undefined) {
cfg.redirectOutput = true;
}
if (cfg.debugStdLib === undefined) {
cfg.debugStdLib = false;
}
if (cfg.subProcess === undefined) {
cfg.subProcess = true;
}
}
private async convertConfigToArgs(
debugConfig: ITestDebugConfig,
workspaceFolder: WorkspaceFolder,
options: LaunchOptions
): Promise<LaunchRequestArguments> {
const configArgs = debugConfig as LaunchRequestArguments;
configArgs.program = this.getTestLauncherScript(options.testProvider);
configArgs.args = this.fixArgs(options.args, options.testProvider);
// We leave configArgs.request as "test" so it will be sent in telemetry.
const launchArgs = await this.launchResolver.resolveDebugConfiguration(
workspaceFolder,
configArgs,
options.token
);
if (!launchArgs) {
throw Error(`Invalid debug config "${debugConfig.name}"`);
}
launchArgs.request = 'launch';
return launchArgs!;
}
private fixArgs(args: string[], testProvider: TestProvider): string[] {
if (testProvider === 'unittest') {
return args.filter(item => item !== '--debug');
} else {
return args;
}
}
private getTestLauncherScript(testProvider: TestProvider) {
switch (testProvider) {
case 'unittest': {
return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'visualstudio_py_testlauncher.py');
}
case 'pytest':
case 'nosetest': {
return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'testlauncher.py');
}
default: {
throw new Error(`Unknown test provider '${testProvider}'`);
}
}
}
}