# Code coverage report for mock-spawn/index.js

## Statements: 94.21% (114 / 121) Branches: 86.11% (31 / 36) Functions: 100% (29 / 29) Lines: 93.97% (109 / 116) Ignored: 2 statements, 1 function, 3 branches

All files » mock-spawn/ » index.js

    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
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199

    1

    1
    12

    36

    22
    11
    11

    12
    12
    12
    12

    12
    12
    12

    12
    1
    1

    12
    12
    12
    2

    1

    1
    12

    12
    12
    12
    12
    12
    12

    12
    1

    11
    11
    10
    10
    10
    10
    10
    10

    11

    1
    2
    2

    2
    1

    1
    1
    1
    1
    1
    1

    1
    11

    5

    5
    1

    5
    1

    5
    5

    11
    11
    11

    11

    1
    5

    7
    7
    6

    6
    1

    7

    5
    6

    5

    1
    8
    8
    8
    8
    8

    1
    1

    1
    2

    1
    12

    12
    9

    12
    12
    12
    12

    1
    6
    5

    6

    1
    8

    12

    8
    21
    8
    8
    8
    3

    8

    8

     'use strict';
    /*
    Copyright 2014 Yahoo! Inc. All rights reserved.
    Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
    */

    /*jslint nomen: true */
    var util = require('util'),
        EventEmitter = require('events').EventEmitter,
        through = require('through'),
        stream = require('stream'),
        lastPid = 1;

    function MockProcess(runner, signals) {
        var that = this,
            streamer = function () {
                /* istanbul ignore next - fallback */
                return stream.PassThrough ? new stream.PassThrough() : through();
            },
            emitted = false,
            emitter = function () {
                if (!emitted) {
                    emitted = true;
                    that.emit('close', that.exitCode, that.signal);
                }
            };
        EventEmitter.call(this);
        this.stdin = streamer();
        this.stdout = streamer();
        this.stderr = streamer();

        this.stderr.on('end', emitter);
        this.stderr.on('finish', emitter);
        this.stderr.on('close', emitter);

        /* istanbul ignore next - polyfill */
        Iif (!this.stdout.setEncoding) {
            this.stdout.setEncoding = this.stderr.setEncoding = function (encoding) {
                this.encoding = encoding;
            };
        }
        this._runner = runner;
        this.signal = null;
        if(signals){
            this.signals = signals;
        }
    }

    util.inherits(MockProcess, EventEmitter);

    MockProcess.prototype._start = function (command, args, opts) {
        var that = this,
            runner = this._runner;

        this.command = command;
        this.args = args;
        this.opts = opts;
        this.pid = lastPid;
        this.ended = false;
        lastPid += 1;

        if(runner.throws instanceof Error){
            throw runner.throws;
        }
        process.nextTick(function () {
            runner.call(that, function (exitCode) {
                Eif(!that.ended){
                    that.ended = true;
                    that.exitCode = exitCode;
                    that.emit('exit', exitCode);
                    that.stdout.end();
                    that.stderr.end();
                }
            });
        });
        return this;
    };

    MockProcess.prototype.kill = function (signal) {
        signal = signal || 'SIGTERM';
        var that = this;
        // if the signals object contains this signal and is *truthy*, and it is still running, exit
        if(this.signals[signal]){
            this.signal = signal;
            /* istanbul ignore else - no-op if ended */
            Eif(!this.ended){
                this.ended = true;
                process.nextTick(function(){
                    that.emit('exit',null,signal);
                    that.stdout.end();
                    that.stderr.end();
                });
            }
        }
    };

    var ezRunner = function (exitCode, stdout, stderr) {
        var verbose,
            fn = function (cb) {
                Iif (verbose) {
                    console.log('Run:' + this.command + ', with args');
                    console.log(this.args);
                    console.log('With stdout = ' + stdout);
                    console.log('With stderr = ' + stderr);
                    console.log('Exit code = ' + exitCode);
                }
                if (stdout) {
                    this.stdout.write(stdout);
                }
                if (stderr) {
                    this.stderr.write(stderr);
                }
                process.nextTick(function () {
                    return cb(exitCode);
                });
            };
        fn.setVerbose = function (v) {
            verbose = !!v;
            return fn;
        };
        return fn;
    };

    function makeSeq(verbose) {
        var runners = [],
            fn = function (/* command, args, opts */) {
                var runner;
                if (runners.length) {
                    Iif (verbose) {
                        console.log('Running first of ' + runners.length +
                            ' remaining functions');
                    }
                    runner = runners.shift();
                } else Iif (verbose) {
                    console.log('No runners left, using default');
                }
                return runner;
            };

        fn.add = function (runner) {
            runners.push(runner);
        };
        return fn;
    }

    function ProcessList(verbose) {
        this.verbose = verbose;
        this.fns = [];
        this.defaultFn = ezRunner(0).setVerbose(verbose);
        this.strategy = null;
        this.calls = [];
    }

    ProcessList.prototype.setStrategy = function (strategy) {
        this.strategy = strategy;
    };

    ProcessList.prototype.setSignals = function (sigs) {
        this.signals = sigs;
    };

    ProcessList.prototype.next = function (command, args, opts) {
        var runner,
            mp;

        if (this.strategy) {
            runner = this.strategy(command, args, opts);
        }
        runner = runner || this.defaultFn;
        mp = new MockProcess(runner,this.signals);
        this.calls.push(mp);
        return mp._start(command, args, opts);
    };

    ProcessList.prototype.__defineGetter__('sequence', function () {
        if (!this.strategy) {
            this.strategy = makeSeq(this.verbose);
        }
        return this.strategy;
    });

    module.exports = function (verbose) {
        var pm = new ProcessList(verbose),
            main = function (command, args, opts) {
                return pm.next(command, args, opts);
            };

        main.__defineGetter__('sequence', function () { return pm.sequence; });
        main.__defineGetter__('calls', function () { return pm.calls.slice(); });
        main.setDefault = function (fn) { pm.defaultFn = fn; };
        main.setStrategy = function (s) { pm.setStrategy(s); };
        main.simple = function (exitCode, stdout, stderr) {
            return ezRunner(exitCode, stdout, stderr).setVerbose(verbose);
        };
        main.setSignals = function (sigs) { pm.setSignals(sigs); };

        return main;
    };