forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
terminalEnvVarCollectionService.unit.test.ts
247 lines (215 loc) · 10.7 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
// 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 { cloneDeep } from 'lodash';
import { mock, instance, when, anything, verify, reset } from 'ts-mockito';
import { EnvironmentVariableCollection, ProgressLocation, Uri } from 'vscode';
import { IApplicationShell, IApplicationEnvironment } 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 { getOSType } from '../../../client/common/utils/platform';
import { defaultShells } from '../../../client/interpreter/activation/service';
import {
TerminalEnvVarCollectionService,
_normCaseKeys,
} from '../../../client/interpreter/activation/terminalEnvVarCollectionService';
import { IEnvironmentActivationService } from '../../../client/interpreter/activation/types';
import { IInterpreterService } from '../../../client/interpreter/contracts';
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 applicationEnvironment: IApplicationEnvironment;
let environmentActivationService: IEnvironmentActivationService;
let terminalEnvVarCollectionService: TerminalEnvVarCollectionService;
const progressOptions = {
location: ProgressLocation.Window,
title: Interpreters.activatingTerminals,
};
const customShell = 'powershell';
const defaultShell = defaultShells[getOSType()];
setup(() => {
platform = mock<IPlatformService>();
when(platform.osType).thenReturn(getOSType());
interpreterService = mock<IInterpreterService>();
context = mock<IExtensionContext>();
shell = mock<IApplicationShell>();
collection = mock<EnvironmentVariableCollection>();
when(context.environmentVariableCollection).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>();
const configService = mock<IConfigurationService>();
when(configService.getSettings(anything())).thenReturn(({
terminal: { activateEnvironment: true },
} as unknown) as IPythonSettings);
terminalEnvVarCollectionService = new TerminalEnvVarCollectionService(
instance(platform),
instance(interpreterService),
instance(context),
instance(shell),
instance(experimentService),
instance(applicationEnvironment),
[],
instance(environmentActivationService),
instance(configService),
);
});
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();
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(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();
verify(interpreterService.onDidChangeInterpreter(anything(), anything(), anything())).never();
verify(applicationEnvironment.onDidChangeShell(anything(), anything(), anything())).never();
assert(applyCollectionStub.notCalled, 'Collection should not be applied on activation');
verify(collection.clear()).once();
});
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();
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();
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', ..._normCaseKeys(process.env) };
delete envVars.PATH;
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(envVars);
when(collection.replace(anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda')).once();
verify(collection.delete('PATH')).once();
});
test('Only relative changes to previously applied variables are applied to the collection', async () => {
const envVars: NodeJS.ProcessEnv = {
RANDOM_VAR: 'random',
CONDA_PREFIX: 'prefix/to/conda',
..._normCaseKeys(process.env),
};
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(envVars);
when(collection.replace(anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
const newEnvVars = cloneDeep(envVars);
delete newEnvVars.CONDA_PREFIX;
newEnvVars.RANDOM_VAR = undefined; // Deleting the variable from the collection is the same as setting it to undefined.
reset(environmentActivationService);
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
customShell,
),
).thenResolve(newEnvVars);
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
verify(collection.delete('CONDA_PREFIX')).once();
verify(collection.delete('RANDOM_VAR')).once();
});
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', ..._normCaseKeys(process.env) };
when(
environmentActivationService.getActivatedEnvironmentVariables(
anything(),
undefined,
undefined,
defaultShell?.shell,
),
).thenResolve(envVars);
when(collection.replace(anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
await terminalEnvVarCollectionService._applyCollection(undefined, customShell);
verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda')).once();
verify(collection.delete(anything())).never();
});
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())).thenResolve();
when(collection.delete(anything())).thenResolve();
await terminalEnvVarCollectionService._applyCollection(undefined, defaultShell?.shell);
verify(collection.clear()).once();
});
});