forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.unit.test.ts
424 lines (380 loc) · 25 KB
/
helper.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { expect } from 'chai';
import { SemVer } from 'semver';
import * as sinon from 'sinon';
import { anything, capture, instance, mock, verify, when } from 'ts-mockito';
import { Terminal, Uri } from 'vscode';
import { TerminalManager } from '../../../client/common/application/terminalManager';
import { ITerminalManager } from '../../../client/common/application/types';
import { PythonSettings } from '../../../client/common/configSettings';
import { ConfigurationService } from '../../../client/common/configuration/service';
import { PlatformService } from '../../../client/common/platform/platformService';
import { IPlatformService } from '../../../client/common/platform/types';
import { Bash } from '../../../client/common/terminal/environmentActivationProviders/bash';
import { CommandPromptAndPowerShell } from '../../../client/common/terminal/environmentActivationProviders/commandPrompt';
import { Nushell } from '../../../client/common/terminal/environmentActivationProviders/nushell';
import { CondaActivationCommandProvider } from '../../../client/common/terminal/environmentActivationProviders/condaActivationProvider';
import { PipEnvActivationCommandProvider } from '../../../client/common/terminal/environmentActivationProviders/pipEnvActivationProvider';
import { PyEnvActivationCommandProvider } from '../../../client/common/terminal/environmentActivationProviders/pyenvActivationProvider';
import { TerminalHelper } from '../../../client/common/terminal/helper';
import { ShellDetector } from '../../../client/common/terminal/shellDetector';
import { TerminalNameShellDetector } from '../../../client/common/terminal/shellDetectors/terminalNameShellDetector';
import {
IShellDetector,
ITerminalActivationCommandProvider,
TerminalShellType,
} from '../../../client/common/terminal/types';
import { IConfigurationService } from '../../../client/common/types';
import { getNamesAndValues } from '../../../client/common/utils/enum';
import { Architecture, OSType } from '../../../client/common/utils/platform';
import { IComponentAdapter } from '../../../client/interpreter/contracts';
import { InterpreterService } from '../../../client/interpreter/interpreterService';
import { IServiceContainer } from '../../../client/ioc/types';
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
suite('Terminal Service helpers', () => {
let helper: TerminalHelper;
let terminalManager: ITerminalManager;
let platformService: IPlatformService;
let condaService: IComponentAdapter;
let serviceContainer: IServiceContainer;
let configurationService: IConfigurationService;
let condaActivationProvider: ITerminalActivationCommandProvider;
let bashActivationProvider: ITerminalActivationCommandProvider;
let cmdActivationProvider: ITerminalActivationCommandProvider;
let nushellActivationProvider: ITerminalActivationCommandProvider;
let pyenvActivationProvider: ITerminalActivationCommandProvider;
let pipenvActivationProvider: ITerminalActivationCommandProvider;
let pythonSettings: PythonSettings;
let shellDetectorIdentifyTerminalShell: sinon.SinonStub<[(Terminal | undefined)?], TerminalShellType>;
let mockDetector: IShellDetector;
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 doSetup() {
mockDetector = mock(TerminalNameShellDetector);
terminalManager = mock(TerminalManager);
platformService = mock(PlatformService);
serviceContainer = mock<IServiceContainer>();
condaService = mock<IComponentAdapter>();
when(serviceContainer.get<IComponentAdapter>(IComponentAdapter)).thenReturn(instance(condaService));
configurationService = mock(ConfigurationService);
condaActivationProvider = mock(CondaActivationCommandProvider);
bashActivationProvider = mock(Bash);
cmdActivationProvider = mock(CommandPromptAndPowerShell);
nushellActivationProvider = mock(Nushell);
pyenvActivationProvider = mock(PyEnvActivationCommandProvider);
pipenvActivationProvider = mock(PipEnvActivationCommandProvider);
pythonSettings = mock(PythonSettings);
shellDetectorIdentifyTerminalShell = sinon.stub(ShellDetector.prototype, 'identifyTerminalShell');
helper = new TerminalHelper(
instance(platformService),
instance(terminalManager),
instance(serviceContainer),
instance(mock(InterpreterService)),
instance(configurationService),
instance(condaActivationProvider),
instance(bashActivationProvider),
instance(cmdActivationProvider),
instance(nushellActivationProvider),
instance(pyenvActivationProvider),
instance(pipenvActivationProvider),
[instance(mockDetector)],
);
}
teardown(() => shellDetectorIdentifyTerminalShell.restore());
suite('Misc', () => {
setup(doSetup);
test('Create terminal without a title', () => {
const terminal = 'Terminal Created';
when(terminalManager.createTerminal(anything())).thenReturn(terminal as any);
const term = helper.createTerminal();
verify(terminalManager.createTerminal(anything())).once();
const args = capture(terminalManager.createTerminal).first()[0];
expect(term).to.be.deep.equal(terminal);
expect(args.name).to.be.deep.equal(undefined, 'name should be undefined');
});
test('Create terminal with a title', () => {
const theTitle = 'Hello';
const terminal = 'Terminal Created';
when(terminalManager.createTerminal(anything())).thenReturn(terminal as any);
const term = helper.createTerminal(theTitle);
verify(terminalManager.createTerminal(anything())).once();
const args = capture(terminalManager.createTerminal).first()[0];
expect(term).to.be.deep.equal(terminal);
expect(args.name).to.be.deep.equal(theTitle);
});
test('Ensure spaces in command is quoted', async () => {
getNamesAndValues<TerminalShellType>(TerminalShellType).forEach((item) => {
const command = 'c:\\python 3.7.exe';
const args = ['1', '2'];
const commandPrefix =
item.value === TerminalShellType.powershell || item.value === TerminalShellType.powershellCore
? '& '
: '';
const expectedTerminalCommand = `${commandPrefix}${command.fileToCommandArgumentForPythonExt()} 1 2`;
const terminalCommand = helper.buildCommandForTerminal(item.value, command, args);
expect(terminalCommand).to.equal(expectedTerminalCommand, `Incorrect command for Shell ${item.name}`);
});
});
test('Ensure spaces in args are quoted', async () => {
getNamesAndValues<TerminalShellType>(TerminalShellType).forEach((item) => {
const command = 'python3.7.exe';
const args = ['a file.py', '1', '2'];
const commandPrefix =
item.value === TerminalShellType.powershell || item.value === TerminalShellType.powershellCore
? '& '
: '';
const expectedTerminalCommand = `${commandPrefix}${command} "a file.py" 1 2`;
const terminalCommand = helper.buildCommandForTerminal(item.value, command, args);
expect(terminalCommand).to.equal(expectedTerminalCommand, `Incorrect command for Shell ${item.name}`);
});
});
test('Ensure empty args are ignored', async () => {
getNamesAndValues<TerminalShellType>(TerminalShellType).forEach((item) => {
const command = 'python3.7.exe';
const args: string[] = [];
const commandPrefix =
item.value === TerminalShellType.powershell || item.value === TerminalShellType.powershellCore
? '& '
: '';
const expectedTerminalCommand = `${commandPrefix}${command}`;
const terminalCommand = helper.buildCommandForTerminal(item.value, command, args);
expect(terminalCommand).to.equal(expectedTerminalCommand, `Incorrect command for Shell '${item.name}'`);
});
});
test('Ensure empty args are ignored with s in command', async () => {
getNamesAndValues<TerminalShellType>(TerminalShellType).forEach((item) => {
const command = 'c:\\python 3.7.exe';
const args: string[] = [];
const commandPrefix =
item.value === TerminalShellType.powershell || item.value === TerminalShellType.powershellCore
? '& '
: '';
const expectedTerminalCommand = `${commandPrefix}${command.fileToCommandArgumentForPythonExt()}`;
const terminalCommand = helper.buildCommandForTerminal(item.value, command, args);
expect(terminalCommand).to.equal(expectedTerminalCommand, `Incorrect command for Shell ${item.name}`);
});
});
});
function title(resource?: Uri, interpreter?: PythonEnvironment) {
return `${resource ? 'With a resource' : 'Without a resource'}${interpreter ? ' and an interpreter' : ''}`;
}
suite('Activation', () => {
[undefined, Uri.parse('a')].forEach((resource) => {
suite(title(resource), () => {
setup(() => {
doSetup();
when(configurationService.getSettings(resource)).thenReturn(instance(pythonSettings));
});
function ensureCondaIsSupported(
isSupported: boolean,
pythonPath: string,
condaActivationCommands: string[],
) {
when(pythonSettings.pythonPath).thenReturn(pythonPath);
when(pythonSettings.terminal).thenReturn({ activateEnvironment: true } as any);
when(condaService.isCondaEnvironment(pythonPath)).thenResolve(isSupported);
when(condaActivationProvider.getActivationCommands(resource, anything())).thenResolve(
condaActivationCommands,
);
}
test('Activation command must return conda activation command if interpreter is conda', async () => {
const pythonPath = 'some python Path value';
const condaActivationCommands = ['Hello', '1'];
ensureCondaIsSupported(true, pythonPath, condaActivationCommands);
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
expect(cmd).to.equal(condaActivationCommands);
verify(pythonSettings.pythonPath).once();
verify(condaService.isCondaEnvironment(pythonPath)).once();
verify(condaActivationProvider.getActivationCommands(resource, anything())).once();
});
test('Activation command must return undefined if none of the proivders support the shell', async () => {
const pythonPath = 'some python Path value';
ensureCondaIsSupported(false, pythonPath, []);
when(bashActivationProvider.isShellSupported(anything())).thenReturn(false);
when(cmdActivationProvider.isShellSupported(anything())).thenReturn(false);
when(nushellActivationProvider.isShellSupported(anything())).thenReturn(false);
when(pyenvActivationProvider.isShellSupported(anything())).thenReturn(false);
when(pipenvActivationProvider.isShellSupported(anything())).thenReturn(false);
const cmd = await helper.getEnvironmentActivationCommands(
('someShell' as any) as TerminalShellType,
resource,
);
expect(cmd).to.equal(undefined, 'Command must be undefined');
verify(pythonSettings.pythonPath).once();
verify(condaService.isCondaEnvironment(pythonPath)).once();
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
verify(nushellActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pyenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pipenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(cmdActivationProvider.isShellSupported(anything())).atLeast(1);
});
test('Activation command must return command from bash if that is supported and others are not', async () => {
const pythonPath = 'some python Path value';
const expectCommand = ['one', 'two'];
ensureCondaIsSupported(false, pythonPath, []);
when(bashActivationProvider.getActivationCommands(resource, anything())).thenResolve(expectCommand);
when(bashActivationProvider.isShellSupported(anything())).thenReturn(true);
when(cmdActivationProvider.isShellSupported(anything())).thenReturn(false);
when(nushellActivationProvider.isShellSupported(anything())).thenReturn(false);
when(pyenvActivationProvider.isShellSupported(anything())).thenReturn(false);
when(pipenvActivationProvider.isShellSupported(anything())).thenReturn(false);
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
expect(cmd).to.deep.equal(expectCommand);
verify(pythonSettings.pythonPath).once();
verify(condaService.isCondaEnvironment(pythonPath)).once();
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
verify(bashActivationProvider.getActivationCommands(resource, anything())).once();
verify(nushellActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pyenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pipenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(cmdActivationProvider.isShellSupported(anything())).atLeast(1);
});
test('Activation command must return command from pipenv if that is supported and even if others are supported', async () => {
const pythonPath = 'some python Path value';
const expectCommand = ['one', 'two'];
ensureCondaIsSupported(false, pythonPath, []);
when(pipenvActivationProvider.getActivationCommands(resource, anything())).thenResolve(
expectCommand,
);
when(pipenvActivationProvider.isShellSupported(anything())).thenReturn(true);
[
bashActivationProvider,
cmdActivationProvider,
nushellActivationProvider,
pyenvActivationProvider,
].forEach((provider) => {
when(provider.getActivationCommands(resource, anything())).thenResolve(['Something']);
when(provider.isShellSupported(anything())).thenReturn(true);
});
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
expect(cmd).to.deep.equal(expectCommand);
verify(pythonSettings.pythonPath).once();
verify(condaService.isCondaEnvironment(pythonPath)).once();
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
verify(bashActivationProvider.getActivationCommands(resource, anything())).never();
verify(pyenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pipenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pipenvActivationProvider.getActivationCommands(resource, anything())).atLeast(1);
verify(cmdActivationProvider.isShellSupported(anything())).atLeast(1);
verify(nushellActivationProvider.isShellSupported(anything())).atLeast(1);
});
test('Activation command must return command from Command Prompt if that is supported and others are not', async () => {
const pythonPath = 'some python Path value';
const expectCommand = ['one', 'two'];
ensureCondaIsSupported(false, pythonPath, []);
when(cmdActivationProvider.getActivationCommands(resource, anything())).thenResolve(expectCommand);
when(bashActivationProvider.isShellSupported(anything())).thenReturn(false);
when(cmdActivationProvider.isShellSupported(anything())).thenReturn(true);
when(nushellActivationProvider.isShellSupported(anything())).thenReturn(false);
when(pyenvActivationProvider.isShellSupported(anything())).thenReturn(false);
when(pipenvActivationProvider.isShellSupported(anything())).thenReturn(false);
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
expect(cmd).to.deep.equal(expectCommand);
verify(pythonSettings.pythonPath).once();
verify(condaService.isCondaEnvironment(pythonPath)).once();
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
verify(nushellActivationProvider.isShellSupported(anything())).atLeast(1);
verify(cmdActivationProvider.getActivationCommands(resource, anything())).once();
verify(pyenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pipenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(cmdActivationProvider.isShellSupported(anything())).atLeast(1);
});
test('Activation command must return command from Command Prompt if that is supported, and so is bash and nushell but no commands are returned', async () => {
const pythonPath = 'some python Path value';
const expectCommand = ['one', 'two'];
ensureCondaIsSupported(false, pythonPath, []);
when(cmdActivationProvider.getActivationCommands(resource, anything())).thenResolve(expectCommand);
when(bashActivationProvider.getActivationCommands(resource, anything())).thenResolve([]);
when(nushellActivationProvider.getActivationCommands(resource, anything())).thenResolve([]);
when(bashActivationProvider.isShellSupported(anything())).thenReturn(true);
when(cmdActivationProvider.isShellSupported(anything())).thenReturn(true);
when(nushellActivationProvider.isShellSupported(anything())).thenReturn(true);
when(pyenvActivationProvider.isShellSupported(anything())).thenReturn(false);
when(pipenvActivationProvider.isShellSupported(anything())).thenReturn(false);
const cmd = await helper.getEnvironmentActivationCommands(anything(), resource);
expect(cmd).to.deep.equal(expectCommand);
verify(pythonSettings.pythonPath).once();
verify(condaService.isCondaEnvironment(pythonPath)).once();
verify(bashActivationProvider.isShellSupported(anything())).atLeast(1);
verify(bashActivationProvider.getActivationCommands(resource, anything())).once();
verify(cmdActivationProvider.getActivationCommands(resource, anything())).once();
verify(nushellActivationProvider.getActivationCommands(resource, anything())).once();
verify(pyenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(pipenvActivationProvider.isShellSupported(anything())).atLeast(1);
verify(cmdActivationProvider.isShellSupported(anything())).atLeast(1);
verify(nushellActivationProvider.isShellSupported(anything())).atLeast(1);
});
[undefined, pythonInterpreter].forEach((interpreter) => {
test('Activation command for Shell must be empty for unknown os', async () => {
when(platformService.osType).thenReturn(OSType.Unknown);
for (const item of getNamesAndValues<TerminalShellType>(TerminalShellType)) {
const cmd = await helper.getEnvironmentActivationShellCommands(
resource,
item.value,
interpreter,
);
expect(cmd).to.equal(undefined, 'Command must be undefined');
}
});
});
[undefined, pythonInterpreter].forEach((interpreter) => {
[OSType.Linux, OSType.OSX, OSType.Windows].forEach((osType) => {
test(`Activation command for Shell must never use pipenv nor pyenv (${osType})`, async () => {
const pythonPath = 'some python Path value';
const shellToExpect =
osType === OSType.Windows ? TerminalShellType.commandPrompt : TerminalShellType.bash;
ensureCondaIsSupported(false, pythonPath, []);
shellDetectorIdentifyTerminalShell.returns(shellToExpect);
when(platformService.osType).thenReturn(osType);
when(bashActivationProvider.isShellSupported(shellToExpect)).thenReturn(false);
when(cmdActivationProvider.isShellSupported(shellToExpect)).thenReturn(false);
when(nushellActivationProvider.isShellSupported(shellToExpect)).thenReturn(false);
const cmd = await helper.getEnvironmentActivationShellCommands(
resource,
shellToExpect,
interpreter,
);
expect(cmd).to.equal(undefined, 'Command must be undefined');
verify(pythonSettings.pythonPath).times(interpreter ? 0 : 1);
verify(condaService.isCondaEnvironment(pythonPath)).times(interpreter ? 0 : 1);
verify(bashActivationProvider.isShellSupported(shellToExpect)).atLeast(1);
verify(pyenvActivationProvider.isShellSupported(anything())).never();
verify(pipenvActivationProvider.isShellSupported(anything())).never();
verify(cmdActivationProvider.isShellSupported(shellToExpect)).atLeast(1);
verify(nushellActivationProvider.isShellSupported(shellToExpect)).atLeast(1);
});
});
});
});
});
});
suite('Identify Terminal Shell', () => {
setup(doSetup);
test('Use shell detector to identify terminal shells', () => {
const terminal = {} as any;
const expectedShell = TerminalShellType.ksh;
shellDetectorIdentifyTerminalShell.returns(expectedShell);
const shell = helper.identifyTerminalShell(terminal);
expect(shell).to.be.equal(expectedShell);
expect(shellDetectorIdentifyTerminalShell.callCount).to.equal(1);
expect(shellDetectorIdentifyTerminalShell.args[0]).deep.equal([terminal]);
});
test('Detector passed throught constructor is used by shell detector class', () => {
const terminal = {} as any;
const expectedShell = TerminalShellType.ksh;
shellDetectorIdentifyTerminalShell.callThrough();
when(mockDetector.identify(anything(), terminal)).thenReturn(expectedShell);
const shell = helper.identifyTerminalShell(terminal);
expect(shell).to.be.equal(expectedShell);
expect(shellDetectorIdentifyTerminalShell.callCount).to.equal(1);
verify(mockDetector.identify(anything(), terminal)).once();
});
});
});