forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonProcess.unit.test.ts
More file actions
119 lines (98 loc) · 5.35 KB
/
pythonProcess.unit.test.ts
File metadata and controls
119 lines (98 loc) · 5.35 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
// 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 TypeMoq from 'typemoq';
import { IFileSystem } from '../../../client/common/platform/types';
import { createPythonEnv } from '../../../client/common/process/pythonEnvironment';
import { createPythonProcessService } from '../../../client/common/process/pythonProcess';
import { IProcessService, StdErrError } from '../../../client/common/process/types';
import { noop } from '../../core';
use(chaiAsPromised);
suite('PythonProcessService', () => {
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('execObservable should call processService.execObservable', () => {
const args = ['-a', 'b', '-c'];
const options = {};
const observable = {
proc: undefined,
out: {} as any,
dispose: () => {
noop();
},
};
processService.setup((p) => p.execObservable(pythonPath, args, options)).returns(() => observable);
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const procs = createPythonProcessService(processService.object, env);
const result = procs.execObservable(args, options);
processService.verify((p) => p.execObservable(pythonPath, args, options), TypeMoq.Times.once());
expect(result).to.be.equal(observable, 'execObservable should return an observable');
});
test('execModuleObservable should call processService.execObservable with the -m argument', () => {
const args = ['-a', 'b', '-c'];
const moduleName = 'foo';
const expectedArgs = ['-m', moduleName, ...args];
const options = {};
const observable = {
proc: undefined,
out: {} as any,
dispose: () => {
noop();
},
};
processService.setup((p) => p.execObservable(pythonPath, expectedArgs, options)).returns(() => observable);
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const procs = createPythonProcessService(processService.object, env);
const result = procs.execModuleObservable(moduleName, args, options);
processService.verify((p) => p.execObservable(pythonPath, expectedArgs, options), TypeMoq.Times.once());
expect(result).to.be.equal(observable, 'execModuleObservable should return an observable');
});
test('exec should call processService.exec', async () => {
const args = ['-a', 'b', '-c'];
const options = {};
const stdout = 'foo';
processService.setup((p) => p.exec(pythonPath, args, options)).returns(() => Promise.resolve({ stdout }));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const procs = createPythonProcessService(processService.object, env);
const result = await procs.exec(args, options);
processService.verify((p) => p.exec(pythonPath, args, options), TypeMoq.Times.once());
expect(result.stdout).to.be.equal(stdout, 'exec should return the content of stdout');
});
test('execModule should call processService.exec with the -m argument', async () => {
const args = ['-a', 'b', '-c'];
const moduleName = 'foo';
const expectedArgs = ['-m', moduleName, ...args];
const options = {};
const stdout = 'bar';
processService
.setup((p) => p.exec(pythonPath, expectedArgs, options))
.returns(() => Promise.resolve({ stdout }));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const procs = createPythonProcessService(processService.object, env);
const result = await procs.execModule(moduleName, args, options);
processService.verify((p) => p.exec(pythonPath, expectedArgs, options), TypeMoq.Times.once());
expect(result.stdout).to.be.equal(stdout, 'exec should return the content of stdout');
});
test('execModule should throw an error if the module is not installed', async () => {
const args = ['-a', 'b', '-c'];
const moduleName = 'foo';
const expectedArgs = ['-m', moduleName, ...args];
const options = {};
processService
.setup((p) => p.exec(pythonPath, expectedArgs, options))
.returns(() => Promise.resolve({ stdout: 'bar', stderr: `Error: No module named ${moduleName}` }));
processService
.setup((p) => p.exec(pythonPath, ['-c', `import ${moduleName}`], { throwOnStdErr: true }))
.returns(() => Promise.reject(new StdErrError('not installed')));
const env = createPythonEnv(pythonPath, processService.object, fileSystem.object);
const procs = createPythonProcessService(processService.object, env);
const result = procs.execModule(moduleName, args, options);
expect(result).to.eventually.be.rejectedWith(`Module '${moduleName}' not installed`);
});
});