forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutionServiceMock.ts
More file actions
70 lines (64 loc) · 3.03 KB
/
executionServiceMock.ts
File metadata and controls
70 lines (64 loc) · 3.03 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { SemVer } from 'semver';
import { ErrorUtils } from '../../client/common/errors/errorUtils';
import { ModuleNotInstalledError } from '../../client/common/errors/moduleNotInstalledError';
import { BufferDecoder } from '../../client/common/process/decoder';
import { ProcessService } from '../../client/common/process/proc';
import {
ExecutionResult,
InterpreterInfomation,
IPythonExecutionService,
ObservableExecutionResult,
SpawnOptions
} from '../../client/common/process/types';
import { Architecture } from '../../client/common/utils/platform';
export class MockPythonExecutionService implements IPythonExecutionService {
private procService : ProcessService;
private pythonPath : string = 'python';
constructor() {
this.procService = new ProcessService(new BufferDecoder());
}
public getInterpreterInformation(): Promise<InterpreterInfomation> {
return Promise.resolve(
{
path: '',
version: new SemVer('3.6.0-beta'),
sysVersion: '1.0',
sysPrefix: '1.0',
architecture: Architecture.x64
});
}
public getExecutablePath(): Promise<string> {
return Promise.resolve(this.pythonPath);
}
public isModuleInstalled(moduleName: string): Promise<boolean> {
return this.procService.exec(this.pythonPath, ['-c', `import ${moduleName}`], { throwOnStdErr: true })
.then(() => true).catch(() => false);
}
public execObservable(args: string[], options: SpawnOptions): ObservableExecutionResult<string> {
const opts: SpawnOptions = { ...options };
return this.procService.execObservable(this.pythonPath, args, opts);
}
public execModuleObservable(moduleName: string, args: string[], options: SpawnOptions): ObservableExecutionResult<string> {
const opts: SpawnOptions = { ...options };
return this.procService.execObservable(this.pythonPath, ['-m', moduleName, ...args], opts);
}
public exec(args: string[], options: SpawnOptions): Promise<ExecutionResult<string>> {
const opts: SpawnOptions = { ...options };
return this.procService.exec(this.pythonPath, args, opts);
}
public async execModule(moduleName: string, args: string[], options: SpawnOptions): Promise<ExecutionResult<string>> {
const opts: SpawnOptions = { ...options };
const result = await this.procService.exec(this.pythonPath, ['-m', moduleName, ...args], opts);
// If a module is not installed we'll have something in stderr.
if (moduleName && ErrorUtils.outputHasModuleNotInstalledError(moduleName!, result.stderr)) {
const isInstalled = await this.isModuleInstalled(moduleName!);
if (!isInstalled) {
throw new ModuleNotInstalledError(moduleName!);
}
}
return result;
}
}