forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpythonExecutionFactory.unit.test.ts
More file actions
493 lines (423 loc) · 25.7 KB
/
pythonExecutionFactory.unit.test.ts
File metadata and controls
493 lines (423 loc) · 25.7 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
import { expect } from 'chai';
import { SemVer } from 'semver';
import * as sinon from 'sinon';
import { anyString, anything, instance, mock, reset, verify, when } from 'ts-mockito';
import * as typemoq from 'typemoq';
import { Uri } from 'vscode';
import { PythonSettings } from '../../../client/common/configSettings';
import { ConfigurationService } from '../../../client/common/configuration/service';
import { BufferDecoder } from '../../../client/common/process/decoder';
import { ProcessLogger } from '../../../client/common/process/logger';
import { ProcessServiceFactory } from '../../../client/common/process/processFactory';
import { CONDA_RUN_VERSION, PythonExecutionFactory } from '../../../client/common/process/pythonExecutionFactory';
import {
IBufferDecoder,
IProcessLogger,
IProcessService,
IProcessServiceFactory,
IPythonExecutionService,
} from '../../../client/common/process/types';
import {
IConfigurationService,
IDisposableRegistry,
IExperimentService,
IInterpreterPathProxyService,
} from '../../../client/common/types';
import { Architecture } from '../../../client/common/utils/platform';
import { EnvironmentActivationService } from '../../../client/interpreter/activation/service';
import { IEnvironmentActivationService } from '../../../client/interpreter/activation/types';
import {
IComponentAdapter,
ICondaLocatorService,
ICondaService,
IInterpreterService,
} from '../../../client/interpreter/contracts';
import { InterpreterService } from '../../../client/interpreter/interpreterService';
import { ServiceContainer } from '../../../client/ioc/container';
import { CondaService } from '../../../client/pythonEnvironments/discovery/locators/services/condaService';
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
import * as ExperimentHelpers from '../../../client/common/experiments/helpers';
import * as WindowsStoreInterpreter from '../../../client/pythonEnvironments/discovery/locators/services/windowsStoreInterpreter';
import { ExperimentService } from '../../../client/common/experiments/service';
import { DiscoveryVariants } from '../../../client/common/experiments/groups';
import { IInterpreterAutoSelectionService } from '../../../client/interpreter/autoSelection/types';
const pythonInterpreter: PythonEnvironment = {
path: '/foo/bar/python.exe',
version: new SemVer('3.6.6-final'),
sysVersion: '1.0.0.0',
sysPrefix: 'Python',
envType: EnvironmentType.Unknown,
architecture: Architecture.x64,
};
function title(resource?: Uri, interpreter?: PythonEnvironment) {
return `${resource ? 'With a resource' : 'Without a resource'}${interpreter ? ' and an interpreter' : ''}`;
}
async function verifyCreateActivated(
factory: PythonExecutionFactory,
activationHelper: IEnvironmentActivationService,
resource?: Uri,
interpreter?: PythonEnvironment,
): Promise<IPythonExecutionService> {
when(activationHelper.getActivatedEnvironmentVariables(resource, anything(), anything())).thenResolve();
const service = await factory.createActivatedEnvironment({ resource, interpreter });
verify(activationHelper.getActivatedEnvironmentVariables(resource, anything(), anything())).once();
return service;
}
suite('Process - PythonExecutionFactory', () => {
[
{ resource: undefined, interpreter: undefined },
{ resource: undefined, interpreter: pythonInterpreter },
{ resource: Uri.parse('x'), interpreter: undefined },
{ resource: Uri.parse('x'), interpreter: pythonInterpreter },
].forEach((item) => {
const { resource } = item;
const { interpreter } = item;
suite(title(resource, interpreter), () => {
let factory: PythonExecutionFactory;
let activationHelper: IEnvironmentActivationService;
let bufferDecoder: IBufferDecoder;
let processFactory: IProcessServiceFactory;
let configService: IConfigurationService;
let condaService: ICondaService;
let condaLocatorService: ICondaLocatorService;
let processLogger: IProcessLogger;
let processService: typemoq.IMock<IProcessService>;
let interpreterService: IInterpreterService;
let pyenvs: IComponentAdapter;
let experimentService: IExperimentService;
let executionService: typemoq.IMock<IPythonExecutionService>;
let isWindowsStoreInterpreterStub: sinon.SinonStub;
let inDiscoveryExperimentStub: sinon.SinonStub;
let autoSelection: IInterpreterAutoSelectionService;
let interpreterPathExpHelper: IInterpreterPathProxyService;
setup(() => {
bufferDecoder = mock(BufferDecoder);
activationHelper = mock(EnvironmentActivationService);
processFactory = mock(ProcessServiceFactory);
configService = mock(ConfigurationService);
condaService = mock(CondaService);
condaLocatorService = mock<ICondaLocatorService>();
processLogger = mock(ProcessLogger);
autoSelection = mock<IInterpreterAutoSelectionService>();
interpreterPathExpHelper = mock<IInterpreterPathProxyService>();
when(interpreterPathExpHelper.get(anything())).thenReturn('selected interpreter path');
experimentService = mock(ExperimentService);
when(experimentService.inExperiment(DiscoveryVariants.discoverWithFileWatching)).thenResolve(false);
when(experimentService.inExperiment(DiscoveryVariants.discoveryWithoutFileWatching)).thenResolve(false);
pyenvs = mock<IComponentAdapter>();
when(pyenvs.isWindowsStoreInterpreter(anyString())).thenResolve(true);
executionService = typemoq.Mock.ofType<IPythonExecutionService>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
executionService.setup((p: any) => p.then).returns(() => undefined);
when(processLogger.logProcess('', [], {})).thenReturn();
processService = typemoq.Mock.ofType<IProcessService>();
processService
.setup((p) =>
p.on('exec', () => {
/** No body */
}),
)
.returns(() => processService.object);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
processService.setup((p: any) => p.then).returns(() => undefined);
interpreterService = mock(InterpreterService);
when(interpreterService.getInterpreterDetails(anything())).thenResolve({
version: { major: 3 },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
const serviceContainer = mock(ServiceContainer);
when(serviceContainer.get<IDisposableRegistry>(IDisposableRegistry)).thenReturn([]);
when(serviceContainer.get<IProcessLogger>(IProcessLogger)).thenReturn(processLogger);
when(serviceContainer.get<IInterpreterService>(IInterpreterService)).thenReturn(
instance(interpreterService),
);
when(serviceContainer.get<ICondaLocatorService>(ICondaLocatorService)).thenReturn(
instance(condaLocatorService),
);
when(serviceContainer.tryGet<IInterpreterService>(IInterpreterService)).thenReturn(
instance(interpreterService),
);
factory = new PythonExecutionFactory(
instance(serviceContainer),
instance(activationHelper),
instance(processFactory),
instance(configService),
instance(condaService),
instance(bufferDecoder),
instance(pyenvs),
instance(experimentService),
instance(autoSelection),
instance(interpreterPathExpHelper),
);
isWindowsStoreInterpreterStub = sinon.stub(WindowsStoreInterpreter, 'isWindowsStoreInterpreter');
isWindowsStoreInterpreterStub.resolves(true);
inDiscoveryExperimentStub = sinon.stub(ExperimentHelpers, 'inDiscoveryExperiment');
});
teardown(() => sinon.restore());
test('Ensure PythonExecutionService is created', async () => {
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(activationHelper.getActivatedEnvironmentVariables(resource)).thenResolve({ x: '1' });
when(pythonSettings.pythonPath).thenReturn('HELLO');
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
const service = await factory.create({ resource });
expect(service).to.not.equal(undefined);
verify(processFactory.create(resource)).once();
verify(pythonSettings.pythonPath).once();
});
test('If no interpreter is explicitly set, ensure we autoselect before PythonExecutionService is created', async () => {
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(activationHelper.getActivatedEnvironmentVariables(resource)).thenResolve({ x: '1' });
when(pythonSettings.pythonPath).thenReturn('HELLO');
reset(interpreterPathExpHelper);
when(interpreterPathExpHelper.get(anything())).thenReturn('python');
when(autoSelection.autoSelectInterpreter(anything())).thenResolve();
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
const service = await factory.create({ resource });
expect(service).to.not.equal(undefined);
verify(autoSelection.autoSelectInterpreter(anything())).once();
verify(processFactory.create(resource)).once();
verify(pythonSettings.pythonPath).once();
});
test('Ensure we use an existing `create` method if there are no environment variables for the activated env', async () => {
const pythonPath = 'path/to/python';
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
let createInvoked = false;
const mockExecService = 'something';
factory.create = async () => {
createInvoked = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Promise.resolve((mockExecService as any) as IPythonExecutionService);
};
const service = await verifyCreateActivated(factory, activationHelper, resource, interpreter);
assert.deepEqual(service, mockExecService);
assert.equal(createInvoked, true);
});
test('Ensure we use an existing `create` method if there are no environment variables (0 length) for the activated env', async () => {
const pythonPath = 'path/to/python';
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
let createInvoked = false;
const mockExecService = 'something';
factory.create = async () => {
createInvoked = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Promise.resolve((mockExecService as any) as IPythonExecutionService);
};
const service = await verifyCreateActivated(factory, activationHelper, resource, interpreter);
assert.deepEqual(service, mockExecService);
assert.equal(createInvoked, true);
});
test('PythonExecutionService is created', async () => {
let createInvoked = false;
const mockExecService = 'something';
factory.create = async () => {
createInvoked = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Promise.resolve((mockExecService as any) as IPythonExecutionService);
};
const pythonSettings = mock(PythonSettings);
when(activationHelper.getActivatedEnvironmentVariables(resource, anything(), anything())).thenResolve({
x: '1',
});
when(pythonSettings.pythonPath).thenReturn('HELLO');
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
const service = await factory.createActivatedEnvironment({ resource, interpreter });
expect(service).to.not.equal(undefined);
verify(activationHelper.getActivatedEnvironmentVariables(resource, anything(), anything())).once();
if (!interpreter) {
verify(pythonSettings.pythonPath).once();
}
assert.equal(createInvoked, false);
});
test("Ensure `create` returns a WindowsStorePythonProcess instance if it's a windows store intepreter path and we're in the discovery experiment", async () => {
const pythonPath = 'path/to/python';
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
inDiscoveryExperimentStub.resolves(true);
const service = await factory.create({ resource });
expect(service).to.not.equal(undefined);
verify(processFactory.create(resource)).once();
verify(pythonSettings.pythonPath).once();
verify(pyenvs.isWindowsStoreInterpreter(pythonPath)).once();
sinon.assert.calledOnce(inDiscoveryExperimentStub);
sinon.assert.notCalled(isWindowsStoreInterpreterStub);
});
test("Ensure `create` returns a WindowsStorePythonProcess instance if it's a windows store intepreter path and we're not in the discovery experiment", async () => {
const pythonPath = 'path/to/python';
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
inDiscoveryExperimentStub.resolves(false);
const service = await factory.create({ resource });
expect(service).to.not.equal(undefined);
verify(processFactory.create(resource)).once();
verify(pythonSettings.pythonPath).once();
verify(pyenvs.isWindowsStoreInterpreter(pythonPath)).never();
sinon.assert.calledOnce(inDiscoveryExperimentStub);
sinon.assert.calledOnce(isWindowsStoreInterpreterStub);
sinon.assert.calledWith(isWindowsStoreInterpreterStub, pythonPath);
});
test('Ensure `create` returns a CondaExecutionService instance if createCondaExecutionService() returns a valid object', async function () {
return this.skip();
const pythonPath = 'path/to/python';
const pythonSettings = mock(PythonSettings);
when(interpreterService.hasInterpreters()).thenResolve(true);
when(processFactory.create(resource)).thenResolve(processService.object);
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
when(condaService.getCondaVersion()).thenResolve(new SemVer(CONDA_RUN_VERSION));
when(condaLocatorService.getCondaEnvironment(pythonPath)).thenResolve({
name: 'foo',
path: 'path/to/foo/env',
});
when(condaService.getCondaFile()).thenResolve('conda');
const service = await factory.create({ resource });
expect(service).to.not.equal(undefined);
verify(processFactory.create(resource)).once();
verify(pythonSettings.pythonPath).once();
verify(condaService.getCondaVersion()).once();
verify(condaLocatorService.getCondaEnvironment(pythonPath)).once();
verify(condaService.getCondaFile()).once();
});
test('Ensure `create` returns a PythonExecutionService instance if createCondaExecutionService() returns undefined', async function () {
return this.skip();
const pythonPath = 'path/to/python';
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
when(condaService.getCondaVersion()).thenResolve(new SemVer('1.0.0'));
when(interpreterService.hasInterpreters()).thenResolve(true);
const service = await factory.create({ resource });
expect(service).to.not.equal(undefined);
verify(processFactory.create(resource)).once();
verify(pythonSettings.pythonPath).once();
verify(condaService.getCondaVersion()).once();
verify(condaLocatorService.getCondaEnvironment(pythonPath)).once();
verify(condaService.getCondaFile()).once();
});
test('Ensure `createActivatedEnvironment` returns a CondaExecutionService instance if createCondaExecutionService() returns a valid object', async function () {
return this.skip();
const pythonPath = 'path/to/python';
const pythonSettings = mock(PythonSettings);
when(processFactory.create(resource)).thenResolve(processService.object);
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(activationHelper.getActivatedEnvironmentVariables(resource, anything(), anything())).thenResolve({
x: '1',
});
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
when(condaService.getCondaVersion()).thenResolve(new SemVer(CONDA_RUN_VERSION));
when(condaLocatorService.getCondaEnvironment(anyString())).thenResolve({
name: 'foo',
path: 'path/to/foo/env',
});
when(condaService.getCondaFile()).thenResolve('conda');
const service = await factory.createActivatedEnvironment({ resource, interpreter });
expect(service).to.not.equal(undefined);
verify(condaService.getCondaFile()).once();
if (!interpreter) {
verify(pythonSettings.pythonPath).once();
verify(condaLocatorService.getCondaEnvironment(pythonPath)).once();
} else {
verify(condaLocatorService.getCondaEnvironment(interpreter!.path)).once();
}
});
test('Ensure `createActivatedEnvironment` returns a PythonExecutionService instance if createCondaExecutionService() returns undefined', async function () {
return this.skip();
let createInvoked = false;
const pythonPath = 'path/to/python';
const mockExecService = 'mockService';
factory.create = async () => {
createInvoked = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Promise.resolve((mockExecService as any) as IPythonExecutionService);
};
const pythonSettings = mock(PythonSettings);
when(activationHelper.getActivatedEnvironmentVariables(resource, anything(), anything())).thenResolve({
x: '1',
});
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(configService.getSettings(resource)).thenReturn(instance(pythonSettings));
when(condaService.getCondaVersion()).thenResolve(new SemVer('1.0.0'));
const service = await factory.createActivatedEnvironment({ resource, interpreter });
expect(service).to.not.equal(undefined);
verify(condaService.getCondaFile()).once();
verify(activationHelper.getActivatedEnvironmentVariables(resource, anything(), anything())).once();
verify(condaService.getCondaVersion()).once();
if (!interpreter) {
verify(pythonSettings.pythonPath).once();
}
assert.equal(createInvoked, false);
});
test('Ensure `createCondaExecutionService` creates a CondaExecutionService instance if there is a conda environment', async () => {
const pythonPath = 'path/to/python';
when(condaLocatorService.getCondaEnvironment(pythonPath)).thenResolve({
name: 'foo',
path: 'path/to/foo/env',
});
when(condaService.getCondaVersion()).thenResolve(new SemVer(CONDA_RUN_VERSION));
when(condaService.getCondaFile()).thenResolve('conda');
const result = await factory.createCondaExecutionService(pythonPath, processService.object, resource);
expect(result).to.not.equal(undefined);
verify(condaService.getCondaVersion()).once();
verify(condaLocatorService.getCondaEnvironment(pythonPath)).once();
verify(condaService.getCondaFile()).once();
});
test('Ensure `createCondaExecutionService` instantiates a ProcessService instance if the process argument is undefined', async () => {
const pythonPath = 'path/to/python';
when(processFactory.create(resource)).thenResolve(processService.object);
when(condaLocatorService.getCondaEnvironment(pythonPath)).thenResolve({
name: 'foo',
path: 'path/to/foo/env',
});
when(condaService.getCondaVersion()).thenResolve(new SemVer(CONDA_RUN_VERSION));
when(condaService.getCondaFile()).thenResolve('conda');
const result = await factory.createCondaExecutionService(pythonPath, undefined, resource);
expect(result).to.not.equal(undefined);
verify(processFactory.create(resource)).once();
verify(condaService.getCondaVersion()).once();
verify(condaLocatorService.getCondaEnvironment(pythonPath)).once();
verify(condaService.getCondaFile()).once();
});
test('Ensure `createCondaExecutionService` returns undefined if there is no conda environment', async () => {
const pythonPath = 'path/to/python';
when(condaLocatorService.getCondaEnvironment(pythonPath)).thenResolve(undefined);
when(condaService.getCondaVersion()).thenResolve(new SemVer(CONDA_RUN_VERSION));
const result = await factory.createCondaExecutionService(pythonPath, processService.object);
expect(result).to.be.equal(
undefined,
'createCondaExecutionService should return undefined if not in a conda environment',
);
verify(condaService.getCondaVersion()).once();
verify(condaLocatorService.getCondaEnvironment(pythonPath)).once();
verify(condaService.getCondaFile()).once();
});
test('Ensure `createCondaExecutionService` returns undefined if the conda version does not support conda run', async () => {
const pythonPath = 'path/to/python';
when(condaService.getCondaVersion()).thenResolve(new SemVer('1.0.0'));
const result = await factory.createCondaExecutionService(pythonPath, processService.object);
expect(result).to.be.equal(
undefined,
'createCondaExecutionService should return undefined if not in a conda environment',
);
verify(condaService.getCondaVersion()).once();
verify(condaLocatorService.getCondaEnvironment(pythonPath)).once();
verify(condaService.getCondaFile()).once();
});
});
});
});