All files fake-spawn.ts

97.67% Statements 42/43
80% Branches 8/10
93.33% Functions 14/15
97.56% Lines 40/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  1x         1x         84x                     1x   15x             1x                 84x   84x 84x 84x       84x 84x 84x   84x 84x       7x 7x 7x 7x               1x         84x 84x       5x       65x 65x       3x 3x         15x 15x       6x 6x       19x 19x       77x 43x     77x 77x 77x 77x     77x      
import {SpawnOptions} from 'child_process';
import {EventEmitter} from 'events';
 
/**
 * @internal
 */
export function createFakeSpawn(callback: ((cp: FakeChildProcessController) => void)): (
    command: string,
    args: ReadonlyArray<string>,
    options: SpawnOptions
) => FakeChildProcess {
    return (command: string, args: ReadonlyArray<string>, options: SpawnOptions) => new FakeChildProcess(
        command,
        args,
        options,
        callback
    );
}
 
/**
 * @internal
 */
export class FakeChildProcessStdIn extends EventEmitter {
    write (data: string): void {
        this.emit('data', Buffer.from(data));
    }
}
 
/**
 * @internal
 */
export class FakeChildProcess extends EventEmitter {
    cmd: string;
    args: ReadonlyArray<string>;
    timeout: NodeJS.Timeout | undefined;
    stdout: EventEmitter;
    stderr: EventEmitter;
    stdin: FakeChildProcessStdIn;
 
    constructor(command: string, args: ReadonlyArray<string>, options: SpawnOptions, callback: (cp: FakeChildProcessController) => void) {
        super();
 
        this.cmd = command;
        this.args = args;
        this.timeout = setTimeout(() => {
            console.error(new Error(`FakeSpawn: Timeout for ${this.cmd} ${this.args.join(' ')}!`));
        }, 5000);
 
        this.stdout = new EventEmitter();
        this.stderr = new EventEmitter();
        this.stdin = new FakeChildProcessStdIn();
 
        const controller = new FakeChildProcessController(this);
        setTimeout(() => callback(controller), 0);
    }
 
    kill(): void {
        this.emit('close', 0);
        Eif(this.timeout !== undefined) {
            clearTimeout(this.timeout);
            this.timeout = undefined;
        }
    }
}
 
/**
 * @internal
 */
export class FakeChildProcessController {
    _cp: FakeChildProcess;
    _code: number | null;
 
    constructor(cp: FakeChildProcess) {
        this._cp = cp;
        this._code = null;
    }
 
    cmd(): string {
        return this._cp.cmd;
    }
 
    stdout(content: string | Record<string, unknown>): this {
        this._cp.stdout.emit('data', typeof content === 'string' ? content : JSON.stringify(content));
        return this;
    }
 
    stderr(content: string): this {
        this._cp.stderr.emit('data', content);
        return this;
    }
 
    // eslint-disable-next-line
    onStdIn(listener: (...args: any[]) => void): this {
        this._cp.stdin.on('data', listener);
        return this;
    }
 
    error(error: Error): this {
        this._cp.emit('error', error);
        return this;
    }
 
    code(exitCode: number): this {
        this._code = exitCode;
        return this;
    }
 
    end(content?: string | Record<string, unknown>): this {
        if (content !== undefined) {
            this.stdout(content);
        }
 
        this._cp.emit('close', this._code || 0);
        Eif(this._cp.timeout !== undefined) {
            clearTimeout(this._cp.timeout);
            this._cp.timeout = undefined;
        }
 
        return this;
    }
}