forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathterminalEnvVarCollectionService.unit.test.ts
529 lines (459 loc) · 23.9 KB
/
terminalEnvVarCollectionService.unit.test.ts
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as sinon from 'sinon';
import { assert, expect } from 'chai';
import { mock, instance, when, anything, verify, reset } from 'ts-mockito';
import {
EnvironmentVariableCollection,
EnvironmentVariableMutatorOptions,
GlobalEnvironmentVariableCollection,
ProgressLocation,
Uri,
WorkspaceConfiguration,
WorkspaceFolder,
} from 'vscode';
import {
IApplicationShell,
IApplicationEnvironment,
IWorkspaceService,
} from '../../../client/common/application/types';
import { TerminalEnvVarActivation } from '../../../client/common/experiments/groups';
import { IPlatformService } from '../../../client/common/platform/types';
import {
IExtensionContext,
IExperimentService,
Resource,
IConfigurationService,
IPythonSettings,
} from '../../../client/common/types';
import { Interpreters } from '../../../client/common/utils/localize';
import { OSType, getOSType } from '../../../client/common/utils/platform';
import { defaultShells } from '../../../client/interpreter/activation/service';
import { TerminalEnvVarCollectionService } from '../../../client/interpreter/activation/terminalEnvVarCollectionService';
import { IEnvironmentActivationService } from '../../../client/interpreter/activation/types';
import { IInterpreterService } from '../../../client/interpreter/contracts';
import { PathUtils } from '../../../client/common/platform/pathUtils';
import { PythonEnvType } from '../../../client/pythonEnvironments/base/info';
import { PythonEnvironment } from '../../../client/pythonEnvironments/info';
suite('Terminal Environment Variable Collection Service', () => {
let platform: IPlatformService;
let interpreterService: IInterpreterService;
let context: IExtensionContext;
let shell: IApplicationShell;
let experimentService: IExperimentService;
let collection: EnvironmentVariableCollection;
let globalCollection: GlobalEnvironmentVariableCollection;
let applicationEnvironment: IApplicationEnvironment;
let environmentActivationService: IEnvironmentActivationService;
let workspaceService: IWorkspaceService;
let workspaceConfig: WorkspaceConfiguration;
let terminalEnvVarCollectionService: TerminalEnvVarCollectionService;
const progressOptions = {
location: ProgressLocation.Window,
title: Interpreters.activatingTerminals,
};
let configService: IConfigurationService;
const displayPath = 'display/path';
const customShell = 'powershell';
const defaultShell = defaultShells[getOSType()];
setup(() => {
workspaceService = mock<IWorkspaceService>();
workspaceConfig = mock<WorkspaceConfiguration>();
when(workspaceService.getWorkspaceFolder(anything())).thenReturn(undefined);
when(workspaceService.workspaceFolders).thenReturn(undefined);
when(workspaceService.getConfiguration('terminal')).thenReturn(instance(workspaceConfig));
when(workspaceConfig.get<boolean>('integrated.shellIntegration.enabled')).thenReturn(true);
platform = mock<IPlatformService>();
when(platform.osType).thenReturn(getOSType());
interpreterService = mock<IInterpreterService>();
context = mock<IExtensionContext>();
shell = mock<IApplicationShell>();
globalCollection = mock<GlobalEnvironmentVariableCollection>();
collection = mock<EnvironmentVariableCollection>();
when(context.environmentVariableCollection).thenReturn(instance(globalCollection));
when(globalCollection.getScoped(anything())).thenReturn(instance(collection));
experimentService = mock<IExperimentService>();
when(experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)).thenReturn(true);
applicationEnvironment = mock<IApplicationEnvironment>();
when(applicationEnvironment.shell).thenReturn(customShell);
when(shell.withProgress(anything(), anything()))
.thenCall((options, _) => {
expect(options).to.deep.equal(progressOptions);
})
.thenResolve();
environmentActivationService = mock<IEnvironmentActivationService>();
when(environmentActivationService.getProcessEnvironmentVariables(anything(), anything())).thenResolve(
process.env,
);
configService = mock<IConfigurationService>();
when(configService.getSettings(anything())).thenReturn(({
terminal: { activateEnvironment: true },
pythonPath: displayPath,
} as unknown) as IPythonSettings);
when(collection.clear()).thenResolve();
terminalEnvVarCollectionService = new TerminalEnvVarCollectionService(
instance(platform),
instance(interpreterService),
instance(context),
instance(shell),
instance(experimentService),
instance(applicationEnvironment),
[],
instance(environmentActivationService),
instance(workspaceService),
instance(configService),
new PathUtils(getOSType() === OSType.Windows),
);
});
teardown(() => {
sinon.restore();
});
test('Apply activated variables to the collection on activation', async () => {
const applyCollectionStub = sinon.stub(terminalEnvVarCollectionService, '_applyCollection');
applyCollectionStub.resolves();
when(interpreterService.onDidChangeInterpreter(anything(), anything(), anything())).thenReturn();
when(applicationEnvironment.onDidChangeShell(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService.activate(undefined);
assert(applyCollectionStub.calledOnce, 'Collection not applied on activation');
});
test('When not in experiment, do not apply activated variables to the collection and clear it instead', async () => {
reset(experimentService);
when(context.environmentVariableCollection).thenReturn(instance(collection));
when(experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)).thenReturn(false);
const applyCollectionStub = sinon.stub(terminalEnvVarCollectionService, '_applyCollection');
applyCollectionStub.resolves();
when(interpreterService.onDidChangeInterpreter(anything(), anything(), anything())).thenReturn();
when(applicationEnvironment.onDidChangeShell(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService.activate(undefined);
verify(interpreterService.onDidChangeInterpreter(anything(), anything(), anything())).once();
verify(applicationEnvironment.onDidChangeShell(anything(), anything(), anything())).never();
assert(applyCollectionStub.notCalled, 'Collection should not be applied on activation');
verify(collection.clear()).atLeast(1);
});
test('When interpreter changes, apply new activated variables to the collection', async () => {
const applyCollectionStub = sinon.stub(terminalEnvVarCollectionService, '_applyCollection');
applyCollectionStub.resolves();
const resource = Uri.file('x');
let callback: (resource: Resource) => Promise<void>;
when(interpreterService.onDidChangeInterpreter(anything(), anything(), anything())).thenCall((cb) => {
callback = cb;
});
when(applicationEnvironment.onDidChangeShell(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService.activate(undefined);
await callback!(resource);
assert(applyCollectionStub.calledWithExactly(resource));
});
test('When selected shell changes, apply new activated variables to the collection', async () => {
const applyCollectionStub = sinon.stub(terminalEnvVarCollectionService, '_applyCollection');
applyCollectionStub.resolves();
let callback: (shell: string) => Promise<void>;
when(applicationEnvironment.onDidChangeShell(anything(), anything(), anything())).thenCall((cb) => {
callback = cb;
});
when(interpreterService.onDidChangeInterpreter(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService.activate(undefined);
await callback!(customShell);
assert(applyCollectionStub.calledWithExactly(undefined, customShell));
});
test('If activated variables are returned for custom shell, apply it correctly to the collection', async () => {
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
verify(collection.clear()).once();
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once();
});
test('If activated variables contain PS1, prefix it using shell integration', async () => {
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env, PS1: '(prompt)' };
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
let opts: EnvironmentVariableMutatorOptions | undefined;
when(collection.prepend('PS1', '(prompt)', anything())).thenCall((_, _v, o) => {
opts = o;
});
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
verify(collection.clear()).once();
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once();
assert.deepEqual(opts, { applyAtProcessCreation: false, applyAtShellIntegration: true });
});
test('Verify envs are not applied if env activation is disabled', async () => {
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
reset(configService);
when(configService.getSettings(anything())).thenReturn(({
terminal: { activateEnvironment: false },
pythonPath: displayPath,
} as unknown) as IPythonSettings);
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
verify(collection.clear()).once();
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).never();
});
test('Verify correct options are used when applying envs and setting description', async () => {
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, customShell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenCall(
(_e, _v, options: EnvironmentVariableMutatorOptions) => {
assert.deepEqual(options, { applyAtShellIntegration: true, applyAtProcessCreation: true });
return Promise.resolve();
},
);
await terminalEnvVarCollectionService._applyCollection(resource, customShell);
verify(collection.clear()).once();
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once();
});
test('Correct track that prompt was set for non-Windows bash where PS1 is set', async () => {
when(platform.osType).thenReturn(OSType.Linux);
const envVars: NodeJS.ProcessEnv = { VIRTUAL_ENV: 'prefix/to/venv', PS1: '(.venv)', ...process.env };
const ps1Shell = 'bash';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, ps1Shell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, ps1Shell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(true);
});
test('Correct track that prompt was set for PS1 if shell integration is disabled', async () => {
reset(workspaceConfig);
when(workspaceConfig.get<boolean>('integrated.shellIntegration.enabled')).thenReturn(false);
when(platform.osType).thenReturn(OSType.Linux);
const envVars: NodeJS.ProcessEnv = { VIRTUAL_ENV: 'prefix/to/venv', PS1: '(.venv)', ...process.env };
const ps1Shell = 'bash';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, ps1Shell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, ps1Shell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(false);
});
test('Correct track that prompt was set for non-Windows where PS1 is not set but should be set', async () => {
when(platform.osType).thenReturn(OSType.Linux);
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
const ps1Shell = 'zsh';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: PythonEnvType.Conda,
envName: 'envName',
envPath: 'prefix/to/conda',
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, ps1Shell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, ps1Shell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(true);
});
test('Correct track that prompt was not set for non-Windows where PS1 is not set but env name is base', async () => {
when(platform.osType).thenReturn(OSType.Linux);
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
const ps1Shell = 'zsh';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: PythonEnvType.Conda,
envName: 'base',
envPath: 'prefix/to/conda',
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, ps1Shell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, ps1Shell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(false);
});
test('Correct track that prompt was not set for non-Windows fish where PS1 is not set', async () => {
when(platform.osType).thenReturn(OSType.Linux);
const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
const ps1Shell = 'fish';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: PythonEnvType.Conda,
envName: 'envName',
envPath: 'prefix/to/conda',
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, ps1Shell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, ps1Shell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(false);
});
test('Correct track that prompt was set correctly for global interpreters', async () => {
when(platform.osType).thenReturn(OSType.Linux);
const ps1Shell = 'zsh';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: undefined,
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, ps1Shell),
).thenResolve(undefined);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, ps1Shell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(true);
});
test('Correct track that prompt was set for Windows when not using powershell', async () => {
when(platform.osType).thenReturn(OSType.Windows);
const envVars: NodeJS.ProcessEnv = { VIRTUAL_ENV: 'prefix/to/venv', ...process.env };
const windowsShell = 'cmd';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, windowsShell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, windowsShell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(true);
});
test('Correct track that prompt was not set for Windows when using powershell', async () => {
when(platform.osType).thenReturn(OSType.Linux);
const envVars: NodeJS.ProcessEnv = { VIRTUAL_ENV: 'prefix/to/venv', ...process.env };
const windowsShell = 'powershell';
const resource = Uri.file('a');
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file('workspacePath'),
name: 'workspace1',
index: 0,
};
when(interpreterService.getActiveInterpreter(resource)).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(workspaceService.getWorkspaceFolder(resource)).thenReturn(workspaceFolder);
when(
environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, windowsShell),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenReturn();
await terminalEnvVarCollectionService._applyCollection(resource, windowsShell);
const result = terminalEnvVarCollectionService.isTerminalPromptSetCorrectly(resource);
expect(result).to.equal(false);
});
test('If no activated variables are returned for custom shell, fallback to using default shell', async () => {
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(undefined);
const envVars = { CONDA_PREFIX: 'prefix/to/conda', ...process.env };
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
defaultShell?.shell,
),
).thenResolve(envVars);
when(collection.replace(anything(), anything(), anything())).thenResolve();
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once();
verify(collection.clear()).twice();
});
test('If no activated variables are returned for default shell, clear collection', async () => {
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
defaultShell?.shell,
),
).thenResolve(undefined);
when(collection.replace(anything(), anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
await terminalEnvVarCollectionService._applyCollection(undefined, defaultShell?.shell);
verify(collection.clear()).once();
});
});