forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.ts
More file actions
93 lines (79 loc) · 3.21 KB
/
proc.ts
File metadata and controls
93 lines (79 loc) · 3.21 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
import 'rxjs/add/observable/of';
import { EventEmitter } from 'events';
import { Observable } from 'rxjs/Observable';
import { ChildProcess } from 'child_process';
import {
ExecutionResult,
IProcessService,
ObservableExecutionResult,
Output,
ShellOptions,
SpawnOptions,
} from '../../client/common/process/types';
import { noop } from '../core';
type ExecObservableCallback = (result: Observable<Output<string>> | Output<string>) => void;
type ExecCallback = (result: ExecutionResult<string>) => void;
export const IOriginalProcessService = Symbol('IProcessService');
export class MockProcessService extends EventEmitter implements IProcessService {
constructor(private procService: IProcessService) {
super();
}
public onExecObservable(
handler: (file: string, args: string[], options: SpawnOptions, callback: ExecObservableCallback) => void,
): void {
this.on('execObservable', handler);
}
public execObservable(file: string, args: string[], options: SpawnOptions = {}): ObservableExecutionResult<string> {
let value: Observable<Output<string>> | Output<string> | undefined;
let valueReturned = false;
this.emit('execObservable', file, args, options, (result: Observable<Output<string>> | Output<string>) => {
value = result;
valueReturned = true;
});
if (valueReturned) {
const output = value as Output<string>;
if (['stderr', 'stdout'].some((source) => source === output.source)) {
return {
proc: {} as ChildProcess,
out: Observable.of(output),
dispose: () => {
noop();
},
};
}
return {
proc: {} as ChildProcess,
out: value as Observable<Output<string>>,
dispose: () => {
noop();
},
};
}
return this.procService.execObservable(file, args, options);
}
public onExec(
handler: (file: string, args: string[], options: SpawnOptions, callback: ExecCallback) => void,
): void {
this.on('exec', handler);
}
public async exec(file: string, args: string[], options: SpawnOptions = {}): Promise<ExecutionResult<string>> {
let value: ExecutionResult<string> | undefined;
let valueReturned = false;
this.emit('exec', file, args, options, (result: ExecutionResult<string>) => {
value = result;
valueReturned = true;
});
return valueReturned ? value! : this.procService.exec(file, args, options);
}
public async shellExec(command: string, options?: ShellOptions): Promise<ExecutionResult<string>> {
let value: ExecutionResult<string> | undefined;
let valueReturned = false;
this.emit('shellExec', command, options, (result: ExecutionResult<string>) => {
value = result;
valueReturned = true;
});
return valueReturned ? value! : this.procService.shellExec(command, options);
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
public dispose(): void {}
}