forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonEnvironment.unit.test.ts
More file actions
362 lines (294 loc) · 15.3 KB
/
pythonEnvironment.unit.test.ts
File metadata and controls
362 lines (294 loc) · 15.3 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import { SemVer } from 'semver';
import * as TypeMoq from 'typemoq';
import { IFileSystem } from '../../../client/common/platform/types';
import {
createCondaEnv,
createPythonEnv,
createMicrosoftStoreEnv,
} from '../../../client/common/process/pythonEnvironment';
import { IProcessService, StdErrError } from '../../../client/common/process/types';
import { Architecture } from '../../../client/common/utils/platform';
import { Conda } from '../../../client/pythonEnvironments/common/environmentManagers/conda';
import { OUTPUT_MARKER_SCRIPT } from '../../../client/common/process/internal/scripts';
use(chaiAsPromised.default);
suite('PythonEnvironment', () => {
let processService: TypeMoq.IMock<IProcessService>;
let fileSystem: TypeMoq.IMock<IFileSystem>;
const pythonPath = 'path/to/python';
setup(() => {
processService = TypeMoq.Mock.ofType<IProcessService>(undefined, TypeMoq.MockBehavior.Strict);
fileSystem = TypeMoq.Mock.ofType<IFileSystem>(undefined, TypeMoq.MockBehavior.Strict);
});
test('getInterpreterInformation should return an object if the python path is valid', async () => {
const json = {
versionInfo: [3, 7, 5, 'candidate', 1],
sysPrefix: '/path/of/sysprefix/versions/3.7.5rc1',
version: '3.7.5rc1 (default, Oct 18 2019, 14:48:48) \n[Clang 11.0.0 (clang-1100.0.33.8)]',
is64Bit: true,
};
processService
.setup((p) => p.shellExec(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() =>
Promise.resolve({
stdout: JSON.stringify(json),
}),
);
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getInterpreterInformation();
const expectedResult = {
architecture: Architecture.x64,
path: pythonPath,
version: new SemVer('3.7.5-candidate1'),
sysPrefix: json.sysPrefix,
sysVersion: undefined,
};
expect(result).to.deep.equal(expectedResult, 'Incorrect value returned by getInterpreterInformation().');
});
test('getInterpreterInformation should return an object if the version info contains less than 5 items', async () => {
const json = {
versionInfo: [3, 7, 5, 'alpha'],
sysPrefix: '/path/of/sysprefix/versions/3.7.5a1',
version: '3.7.5a1 (default, Oct 18 2019, 14:48:48) \n[Clang 11.0.0 (clang-1100.0.33.8)]',
is64Bit: true,
};
processService
.setup((p) => p.shellExec(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() =>
Promise.resolve({
stdout: JSON.stringify(json),
}),
);
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getInterpreterInformation();
const expectedResult = {
architecture: Architecture.x64,
path: pythonPath,
version: new SemVer('3.7.5-alpha'),
sysPrefix: json.sysPrefix,
sysVersion: undefined,
};
expect(result).to.deep.equal(
expectedResult,
'Incorrect value returned by getInterpreterInformation() with truncated versionInfo.',
);
});
test('getInterpreterInformation should return an object if the version info contains less than 4 items', async () => {
const json = {
versionInfo: [3, 7, 5],
sysPrefix: '/path/of/sysprefix/versions/3.7.5rc1',
version: '3.7.5rc1 (default, Oct 18 2019, 14:48:48) \n[Clang 11.0.0 (clang-1100.0.33.8)]',
is64Bit: true,
};
processService
.setup((p) => p.shellExec(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() =>
Promise.resolve({
stdout: JSON.stringify(json),
}),
);
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getInterpreterInformation();
const expectedResult = {
architecture: Architecture.x64,
path: pythonPath,
version: new SemVer('3.7.5'),
sysPrefix: json.sysPrefix,
sysVersion: undefined,
};
expect(result).to.deep.equal(
expectedResult,
'Incorrect value returned by getInterpreterInformation() with truncated versionInfo.',
);
});
test('getInterpreterInformation should return an object with the architecture value set to x86 if json.is64bit is not 64bit', async () => {
const json = {
versionInfo: [3, 7, 5, 'candidate'],
sysPrefix: '/path/of/sysprefix/versions/3.7.5rc1',
version: '3.7.5rc1 (default, Oct 18 2019, 14:48:48) \n[Clang 11.0.0 (clang-1100.0.33.8)]',
is64Bit: false,
};
processService
.setup((p) => p.shellExec(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() =>
Promise.resolve({
stdout: JSON.stringify(json),
}),
);
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getInterpreterInformation();
const expectedResult = {
architecture: Architecture.x86,
path: pythonPath,
version: new SemVer('3.7.5-candidate'),
sysPrefix: json.sysPrefix,
sysVersion: undefined,
};
expect(result).to.deep.equal(
expectedResult,
'Incorrect value returned by getInterpreterInformation() for x86b architecture.',
);
});
test('getInterpreterInformation should error out if interpreterInfo.py times out', async () => {
processService
.setup((p) => p.shellExec(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => Promise.reject(new Error('timed out')));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getInterpreterInformation();
expect(result).to.equal(
undefined,
'getInterpreterInfo() should return undefined because interpreterInfo timed out.',
);
});
test('getInterpreterInformation should return undefined if the json value returned by interpreterInfo.py is not valid', async () => {
processService
.setup((p) => p.shellExec(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => Promise.resolve({ stdout: 'bad json' }));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getInterpreterInformation();
expect(result).to.equal(undefined, 'getInterpreterInfo() should return undefined because of bad json.');
});
test('getExecutablePath should return pythonPath if pythonPath is a file', async () => {
fileSystem.setup((f) => f.pathExists(pythonPath)).returns(() => Promise.resolve(true));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getExecutablePath();
expect(result).to.equal(pythonPath, "getExecutablePath() sbould return pythonPath if it's a file");
});
test('getExecutablePath should not return pythonPath if pythonPath is not a file', async () => {
const executablePath = 'path/to/dummy/executable';
fileSystem.setup((f) => f.pathExists(pythonPath)).returns(() => Promise.resolve(false));
processService
.setup((p) => p.shellExec(`${pythonPath} -c "import sys;print(sys.executable)"`, TypeMoq.It.isAny()))
.returns(() => Promise.resolve({ stdout: executablePath }));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getExecutablePath();
expect(result).to.equal(executablePath, "getExecutablePath() sbould not return pythonPath if it's not a file");
});
test('getExecutablePath should return `undefined` if the result of exec() writes to stderr', async () => {
const stderr = 'bar';
fileSystem.setup((f) => f.pathExists(pythonPath)).returns(() => Promise.resolve(false));
processService
.setup((p) => p.shellExec(`${pythonPath} -c "import sys;print(sys.executable)"`, TypeMoq.It.isAny()))
.returns(() => Promise.reject(new StdErrError(stderr)));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.getExecutablePath();
expect(result).to.be.equal(undefined);
});
test('isModuleInstalled should call processService.exec()', async () => {
const moduleName = 'foo';
const argv = ['-c', `import ${moduleName}`];
processService
.setup((p) => p.exec(pythonPath, argv, { throwOnStdErr: true }))
.returns(() => Promise.resolve({ stdout: '' }))
.verifiable(TypeMoq.Times.once());
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
await env.isModuleInstalled(moduleName);
processService.verifyAll();
});
test('isModuleInstalled should return true when processService.exec() succeeds', async () => {
const moduleName = 'foo';
const argv = ['-c', `import ${moduleName}`];
processService
.setup((p) => p.exec(pythonPath, argv, { throwOnStdErr: true }))
.returns(() => Promise.resolve({ stdout: '' }));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.isModuleInstalled(moduleName);
expect(result).to.equal(true, 'isModuleInstalled() should return true if the module exists');
});
test('isModuleInstalled should return false when processService.exec() throws', async () => {
const moduleName = 'foo';
const argv = ['-c', `import ${moduleName}`];
processService
.setup((p) => p.exec(pythonPath, argv, { throwOnStdErr: true }))
.returns(() => Promise.reject(new StdErrError('bar')));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = await env.isModuleInstalled(moduleName);
expect(result).to.equal(false, 'isModuleInstalled() should return false if the module does not exist');
});
test('getExecutionInfo should return pythonPath and the execution arguments as is', () => {
const args = ['-a', 'b', '-c'];
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const result = env.getExecutionInfo(args);
expect(result).to.deep.equal(
{ command: pythonPath, args, python: [pythonPath], pythonExecutable: pythonPath },
'getExecutionInfo should return pythonPath and the command and execution arguments as is',
);
});
});
suite('CondaEnvironment', () => {
let processService: TypeMoq.IMock<IProcessService>;
let fileSystem: TypeMoq.IMock<IFileSystem>;
const args = ['-a', 'b', '-c'];
const pythonPath = 'path/to/python';
const condaFile = 'path/to/conda';
setup(() => {
sinon.stub(Conda, 'getConda').resolves(new Conda(condaFile));
sinon.stub(Conda.prototype, 'getInterpreterPathForEnvironment').resolves(pythonPath);
processService = TypeMoq.Mock.ofType<IProcessService>(undefined, TypeMoq.MockBehavior.Strict);
fileSystem = TypeMoq.Mock.ofType<IFileSystem>(undefined, TypeMoq.MockBehavior.Strict);
});
teardown(() => sinon.restore());
test('getExecutionInfo with a named environment should return execution info using the environment path', async () => {
const condaInfo = { name: 'foo', path: 'bar' };
const env = await createCondaEnv(condaInfo, processService.object, fileSystem.object);
const result = env?.getExecutionInfo(args, pythonPath);
expect(result).to.deep.equal({
command: condaFile,
args: ['run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
python: [condaFile, 'run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
pythonExecutable: pythonPath,
});
});
test('getExecutionInfo with a non-named environment should return execution info using the environment path', async () => {
const condaInfo = { name: '', path: 'bar' };
const env = await createCondaEnv(condaInfo, processService.object, fileSystem.object);
const result = env?.getExecutionInfo(args, pythonPath);
expect(result).to.deep.equal({
command: condaFile,
args: ['run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
python: [condaFile, 'run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
pythonExecutable: pythonPath,
});
});
test('getExecutionObservableInfo with a named environment should return execution info using conda full path with the path', async () => {
const condaInfo = { name: 'foo', path: 'bar' };
const expected = {
command: condaFile,
args: ['run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
python: [condaFile, 'run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
pythonExecutable: pythonPath,
};
const env = await createCondaEnv(condaInfo, processService.object, fileSystem.object);
const result = env?.getExecutionObservableInfo(args, pythonPath);
expect(result).to.deep.equal(expected);
});
test('getExecutionObservableInfo with a non-named environment should return execution info using conda full path', async () => {
const condaInfo = { name: '', path: 'bar' };
const expected = {
command: condaFile,
args: ['run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
python: [condaFile, 'run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
pythonExecutable: pythonPath,
};
const env = await createCondaEnv(condaInfo, processService.object, fileSystem.object);
const result = env?.getExecutionObservableInfo(args, pythonPath);
expect(result).to.deep.equal(expected);
});
});
suite('MicrosoftStoreEnvironment', () => {
let processService: TypeMoq.IMock<IProcessService>;
const pythonPath = 'foo';
setup(() => {
processService = TypeMoq.Mock.ofType<IProcessService>(undefined, TypeMoq.MockBehavior.Strict);
});
test('Should return pythonPath if it is the path to the microsoft store interpreter', async () => {
const env = createMicrosoftStoreEnv(pythonPath, processService.object);
const executablePath = await env.getExecutablePath();
expect(executablePath).to.equal(pythonPath);
processService.verifyAll();
});
});