forked from microsoft/vscode-python-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.unit.test.ts
More file actions
367 lines (303 loc) · 17.4 KB
/
factory.unit.test.ts
File metadata and controls
367 lines (303 loc) · 17.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* eslint-disable @typescript-eslint/naming-convention */
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import * as sinon from 'sinon';
import { SemVer } from 'semver';
import { instance, mock, when } from 'ts-mockito';
import {
DebugAdapterExecutable,
DebugAdapterServer,
DebugConfiguration,
DebugSession,
Uri,
WorkspaceFolder,
} from 'vscode';
import { IPersistentStateFactory } from '../../../extension/common/types';
import { DebugAdapterDescriptorFactory, debugStateKeys } from '../../../extension/debugger/adapter/factory';
import { IDebugAdapterDescriptorFactory } from '../../../extension/debugger/types';
import { EventName } from '../../../extension/telemetry/constants';
import { PersistentState, PersistentStateFactory } from '../../../extension/common/persistentState';
import { EXTENSION_ROOT_DIR } from '../../../extension/common/constants';
import { Architecture } from '../../../extension/common/platform';
import * as pythonApi from '../../../extension/common/python';
import * as telemetry from '../../../extension/telemetry';
import * as telemetryReporter from '../../../extension/telemetry/reporter';
import * as vscodeApi from '../../../extension/common/vscodeapi';
import { DebugConfigStrings } from '../../../extension/common/utils/localize';
import { PythonEnvironment } from '../../../extension/envExtApi';
import { buildPythonEnvironmentWithActivatedRun } from '../common/helpers';
use(chaiAsPromised);
suite('Debugging - Adapter Factory', () => {
let factory: IDebugAdapterDescriptorFactory;
let stateFactory: IPersistentStateFactory;
let state: PersistentState<boolean | undefined>;
let showErrorMessageStub: sinon.SinonStub;
let resolveEnvironmentStub: sinon.SinonStub;
let getInterpreterDetailsStub: sinon.SinonStub;
let getTelemetryReporterStub: sinon.SinonStub;
let reporter: any;
const nodeExecutable = undefined;
const debugAdapterPath = path.join(EXTENSION_ROOT_DIR, 'bundled', 'libs', 'debugpy', 'adapter');
const pythonPath = 'path/to/python/interpreter';
function createInterpreter(executable: string, version: string): PythonEnvironment {
return {
envId: { id: executable, managerId: 'Venv' },
name: `Python ${version}`,
displayName: `Python ${version}`,
displayPath: executable,
version,
environmentPath: Uri.file(executable),
execInfo: {
run: {
executable,
args: [],
},
activatedRun: {
executable,
args: [],
},
},
sysPrefix: '',
};
}
const interpreter: PythonEnvironment = createInterpreter(pythonPath, '3.7.4-test');
const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST;
const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST;
class Reporter {
public static eventNames: string[] = [];
public static properties: Record<string, string>[] = [];
public static measures: {}[] = [];
public sendTelemetryEvent(eventName: string, properties?: {}, measures?: {}) {
Reporter.eventNames.push(eventName);
Reporter.properties.push(properties!);
Reporter.measures.push(measures!);
}
}
setup(() => {
process.env.VSC_PYTHON_UNIT_TEST = undefined;
process.env.VSC_PYTHON_CI_TEST = undefined;
reporter = new Reporter();
stateFactory = mock(PersistentStateFactory);
state = mock(PersistentState) as PersistentState<boolean | undefined>;
showErrorMessageStub = sinon.stub(vscodeApi, 'showErrorMessage');
resolveEnvironmentStub = sinon.stub(pythonApi, 'resolveEnvironment');
getInterpreterDetailsStub = sinon.stub(pythonApi, 'getInterpreterDetails');
getTelemetryReporterStub = sinon.stub(telemetryReporter, 'getTelemetryReporter');
when(
stateFactory.createGlobalPersistentState<boolean | undefined>(debugStateKeys.doNotShowAgain, false),
).thenReturn(instance(state));
getTelemetryReporterStub.returns(reporter);
factory = new DebugAdapterDescriptorFactory(instance(stateFactory));
});
teardown(() => {
process.env.VSC_PYTHON_UNIT_TEST = oldValueOfVSC_PYTHON_UNIT_TEST;
process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST;
Reporter.properties = [];
Reporter.eventNames = [];
Reporter.measures = [];
telemetry.clearTelemetryReporter();
sinon.restore();
});
function createSession(config: Partial<DebugConfiguration>, workspaceFolder?: WorkspaceFolder): DebugSession {
return {
configuration: { name: '', request: 'launch', type: 'python', ...config },
id: '',
name: 'python',
type: 'python',
workspaceFolder,
customRequest: () => Promise.resolve(),
getDebugProtocolBreakpoint: () => Promise.resolve(undefined),
};
}
test('Return the value of configuration.pythonPath as the current python path if it exists', async () => {
const session = createSession({ pythonPath });
const debugExecutable = new DebugAdapterExecutable(pythonPath, [debugAdapterPath]);
resolveEnvironmentStub.withArgs(pythonPath).resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Return the path of the active interpreter as the current python path, it exists and configuration.pythonPath is not defined', async () => {
const session = createSession({});
const debugExecutable = new DebugAdapterExecutable(pythonPath, [debugAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Display a message if no python interpreter is set', async () => {
getInterpreterDetailsStub.resolves(undefined);
const session = createSession({});
const promise = factory.createDebugAdapterDescriptor(session, nodeExecutable);
await expect(promise).to.eventually.be.rejectedWith(DebugConfigStrings.debugStopped);
//check error message
sinon.assert.calledOnce(showErrorMessageStub);
});
test('Display a message if python version is less than 3.7', async () => {
const session = createSession({});
const deprecatedInterpreter: PythonEnvironment = {
...createInterpreter(pythonPath, '3.6.12-test'),
// Provide semver-like object for version check path while keeping string version for our helper.
architecture: Architecture.Unknown,
// Keep a SemVer instance separately if code relies on it (factory only parses string).
semVer: new SemVer('3.6.12-test'),
} as any;
when(state.value).thenReturn(false);
getInterpreterDetailsStub.resolves({ path: [deprecatedInterpreter.execInfo.run.executable] });
resolveEnvironmentStub.resolves(deprecatedInterpreter);
await factory.createDebugAdapterDescriptor(session, nodeExecutable);
sinon.assert.calledOnce(showErrorMessageStub);
});
test('Return Debug Adapter server if request is "attach", and port is specified directly', async () => {
const session = createSession({ request: 'attach', port: 5678, host: 'localhost' });
const debugServer = new DebugAdapterServer(session.configuration.port, session.configuration.host);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
// Interpreter not needed for host/port
assert.deepStrictEqual(descriptor, debugServer);
});
test('Return Debug Adapter server if request is "attach", and connect is specified', async () => {
const session = createSession({ request: 'attach', connect: { port: 5678, host: 'localhost' } });
const debugServer = new DebugAdapterServer(
session.configuration.connect.port,
session.configuration.connect.host,
);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
// Interpreter not needed for connect
assert.deepStrictEqual(descriptor, debugServer);
});
test('Return Debug Adapter server if request is "attach", and connect is specified with port as string', async () => {
const session = createSession({ request: 'attach', connect: { port: '5678', host: 'localhost' } });
const debugServer = new DebugAdapterServer(5678, session.configuration.connect.host);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
// Interpreter not needed for connect
assert.deepStrictEqual(descriptor, debugServer);
});
test('Return Debug Adapter executable if request is "attach", and listen is specified', async () => {
const session = createSession({ request: 'attach', listen: { port: 5678, host: 'localhost' } });
const debugExecutable = new DebugAdapterExecutable(pythonPath, [debugAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Throw error if request is "attach", and neither port, processId, listen, nor connect is specified', async () => {
const session = createSession({
request: 'attach',
port: undefined,
processId: undefined,
listen: undefined,
connect: undefined,
});
const promise = factory.createDebugAdapterDescriptor(session, nodeExecutable);
await expect(promise).to.eventually.be.rejectedWith(
'"request":"attach" requires either "connect", "listen", or "processId"',
);
});
test('Pass the --log-dir argument to debug adapter if configuration.logToFile is set', async () => {
const session = createSession({ logToFile: true });
const debugExecutable = new DebugAdapterExecutable(pythonPath, [
debugAdapterPath,
'--log-dir',
EXTENSION_ROOT_DIR,
]);
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.withArgs(interpreter.execInfo.run.executable).resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test("Don't pass the --log-dir argument to debug adapter if configuration.logToFile is not set", async () => {
const session = createSession({});
const debugExecutable = new DebugAdapterExecutable(pythonPath, [debugAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.withArgs(interpreter.execInfo.run.executable).resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test("Don't pass the --log-dir argument to debugger if configuration.logToFile is set to false", async () => {
const session = createSession({ logToFile: false });
const debugExecutable = new DebugAdapterExecutable(pythonPath, [debugAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.withArgs(interpreter.execInfo.run.executable).resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Send attach to local process telemetry if attaching to a local process', async () => {
const session = createSession({ request: 'attach', processId: 1234 });
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.withArgs(interpreter.execInfo.run.executable).resolves(interpreter);
await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.ok(Reporter.eventNames.includes(EventName.DEBUGGER_ATTACH_TO_LOCAL_PROCESS));
});
test("Don't send any telemetry if not attaching to a local process", async () => {
const session = createSession({});
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.withArgs(interpreter.execInfo.run.executable).resolves(interpreter);
await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.ok(Reporter.eventNames.includes(EventName.DEBUG_ADAPTER_USING_WHEELS_PATH));
});
test('Use "debugAdapterPath" when specified', async () => {
const customAdapterPath = 'custom/debug/adapter/path';
const session = createSession({ debugAdapterPath: customAdapterPath });
const debugExecutable = new DebugAdapterExecutable(pythonPath, [customAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.withArgs(interpreter.execInfo.run.executable).resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Add quotes to interpreter path with spaces', async () => {
const customAdapterPath = 'custom/debug/adapter/customAdapterPath';
const session = createSession({ debugAdapterPath: customAdapterPath });
const interpreterPathSpaces = 'path/to/python interpreter with spaces';
const interpreterPathSpacesQuoted = `"${interpreterPathSpaces}"`;
const debugExecutable = new DebugAdapterExecutable(interpreterPathSpacesQuoted, [customAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreterPathSpaces] });
const interpreterSpacePath: PythonEnvironment = createInterpreter(interpreterPathSpaces, '3.7.4-test');
// Add architecture for completeness.
(interpreterSpacePath as any).architecture = Architecture.Unknown;
resolveEnvironmentStub.withArgs(interpreterPathSpaces).resolves(interpreterSpacePath);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Use "debugAdapterPython" when specified', async () => {
const session = createSession({ debugAdapterPython: '/bin/custompy' });
const debugExecutable = new DebugAdapterExecutable('/bin/custompy', [debugAdapterPath]);
const customInterpreter: PythonEnvironment = createInterpreter('/bin/custompy', '3.7.4-test');
(customInterpreter as any).architecture = Architecture.Unknown;
resolveEnvironmentStub.withArgs('/bin/custompy').resolves(customInterpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Do not use "python" to spawn the debug adapter', async () => {
const session = createSession({ python: '/bin/custompy' });
const debugExecutable = new DebugAdapterExecutable(pythonPath, [debugAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreter.execInfo.run.executable] });
resolveEnvironmentStub.withArgs(interpreter.execInfo.run.executable).resolves(interpreter);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Use run.executable rather than activatedRun.executable for interpreter identification', async () => {
// Simulates environment managers like pixi/conda that set activatedRun to a wrapper
// command (e.g. "pixi run python") while run.executable is the actual Python binary.
const actualPythonPath = 'path/to/actual/python3';
const wrapperCommand = 'pixi';
const interpreterWithWrapper = buildPythonEnvironmentWithActivatedRun(
actualPythonPath,
wrapperCommand,
'3.10.0',
['run', 'python'],
);
const session = createSession({});
// The debug adapter should use the actual Python binary, not the wrapper
const debugExecutable = new DebugAdapterExecutable(interpreterWithWrapper.execInfo.run.executable, [
debugAdapterPath,
]);
getInterpreterDetailsStub.resolves({ path: [interpreterWithWrapper.execInfo.run.executable] });
resolveEnvironmentStub.resolves(interpreterWithWrapper);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);
assert.deepStrictEqual(descriptor, debugExecutable);
});
});