import 'rxjs/add/observable/of'; import { EventEmitter } from 'events'; import { Observable } from 'rxjs/Observable'; import { ExecutionResult, IProcessService, ObservableExecutionResult, Output, ShellOptions, SpawnOptions } from '../../client/common/process/types'; import { noop } from '../core'; type ExecObservableCallback = (result: Observable> | Output) => void; type ExecCallback = (result: ExecutionResult) => 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 ) { this.on('execObservable', handler); } public execObservable(file: string, args: string[], options: SpawnOptions = {}): ObservableExecutionResult { let value: Observable> | Output | undefined; let valueReturned = false; this.emit('execObservable', file, args, options, (result: Observable> | Output) => { value = result; valueReturned = true; }); if (valueReturned) { const output = value as Output; if (['stderr', 'stdout'].some(source => source === output.source)) { return { // tslint:disable-next-line:no-any proc: {} as any, out: Observable.of(output), dispose: () => { noop(); } }; } else { return { // tslint:disable-next-line:no-any proc: {} as any, out: value as Observable>, dispose: () => { noop(); } }; } } else { return this.procService.execObservable(file, args, options); } } public onExec(handler: (file: string, args: string[], options: SpawnOptions, callback: ExecCallback) => void) { this.on('exec', handler); } public async exec(file: string, args: string[], options: SpawnOptions = {}): Promise> { let value: ExecutionResult | undefined; let valueReturned = false; this.emit('exec', file, args, options, (result: ExecutionResult) => { value = result; valueReturned = true; }); return valueReturned ? value! : this.procService.exec(file, args, options); } public async shellExec(command: string, options?: ShellOptions): Promise> { let value: ExecutionResult | undefined; let valueReturned = false; this.emit('shellExec', command, options, (result: ExecutionResult) => { value = result; valueReturned = true; }); return valueReturned ? value! : this.procService.shellExec(command, options); } public dispose() { return; } }