<commit_before>/* --- ENVIRONMENT SETUP --- */

// require external modules
var ExecPlan = require('exec-plan').ExecPlan;
var sinon    = require('sinon');
var utils    = require('../utils');

// short-hand variables
var emptyFn    = utils.emptyFn;
var isFunction = utils.isFunction;
var isDefined  = utils.isDefined;
var isNumber   = utils.isNumber;
var isString   = utils.isString;
var isObject   = utils.isObject;
var isArray    = utils.isArray;

/* ------------------------- */

/* --- TEST CASES --- */

// GROUP: verification of correct construction
exports.construction = {
    'Passing in no config object makes autoPrintOut/Err true':
            function (test) {
        var execPlan = new ExecPlan();
        test.ok(execPlan.willAutoPrintOut());
        test.ok(execPlan.willAutoPrintErr());
        test.done();
    },
    'Config {autoPrintOut:false} sets autoPrintOut/Err accordingly':
            function (test) {
        var execPlan = new ExecPlan({autoPrintOut: false});
        test.equal(execPlan.willAutoPrintOut(), false, 'execPlan will not auto print stdout');
        test.ok(execPlan.willAutoPrintErr(), 'execPlan will auto print stderr');
        test.done();
    },
    'Config {autoPrintErr:false} sets autoPrintOut/Err accordingly':
            function (test) {
        var execPlan = new ExecPlan({autoPrintErr: false});
        test.ok(execPlan.willAutoPrintOut(), 'execPlan will auto print stderr');
        test.equal(execPlan.willAutoPrintErr(), false, 'execPlan will not auto print stderr');
        test.done();
    },
    'Config {autoPrintOut:false, autoPrintErr:false} sets autoPrintOut/Err correctly':
            function (test) {
        var execPlan = new ExecPlan({autoPrintOut: false, autoPrintErr: false});
        test.equal(execPlan.willAutoPrintOut(), false, 'execPlan will not auto print stdout');
        test.equal(execPlan.willAutoPrintErr(), false, 'execPlan will not auto print stderr');
        test.done();
    }
};

// GROUP: verification that autoPrintOut/Err settings are honored by execution
exports.autoPrintOut_Err = {
    setUp: function (callback) {
        this.stdoutStub = sinon.stub(process.stdout, "write");
        this.stderrStub = sinon.stub(process.stderr, "write");
        callback();
    },
    tearDown: function (callback) {
        this.stdoutStub.restore();
        this.stderrStub.restore();
        callback();
    },
    'Prints to stdout by default': function (test) {  // this test case will serve as a template
                                                      // for the others (avoid repeating comments).
        // setup exec plan
        var testCase = this, execPlan = new ExecPlan();
        execPlan.add('ls');
        execPlan.add('ps -ef');
        execPlan.on('finish', function () {  // need to wait until plan finishes
            test.ok(testCase.stdoutStub.called, 'stdout is written to');
            test.done();
        });

        // set the asynchronous execution of the plan into motion
        execPlan.execute();
    },
    'Prints to stderr by default': function (test) {
        var testCase = this, execPlan = new ExecPlan();
        execPlan.add('./command_that_does_not_exist');
        execPlan.on('finish', function () {
            test.ok(testCase.stdoutStub.called, 'stderr is written to');
            test.done();
        });
        execPlan.execute();
    },
    'Does not print to stdout when "autoPrintOut" is turned off': function (test) {
        var testCase = this, execPlan = new ExecPlan({autoPrintOut: false});
        var errorSpy = sinon.spy();
        execPlan.add('ls');
        execPlan.on('finish', function () {
            test.ok(!testCase.stdoutStub.called, 'stdout is not wrriten to');
            test.done();
        });
        execPlan.execute();
    },
    'Does not print to stderr when "autoPrintErr" is turned off': function (test) {
        var testCase = this, execPlan = new ExecPlan({autoPrintErr: false});
        var errorSpy = sinon.spy();
        execPlan.add('./command_that_does_not_exist');
        execPlan.on('execerror', errorSpy);
        execPlan.on('finish', function () {
            test.ok(errorSpy.called, 'error event handler is called');
            test.ok(!testCase.stderrStub.called, 'stderr is not wrriten to');
            test.done();
        });
        execPlan.execute();
    }
};

// GROUP: verification of 'execute' method
exports.execute = {
    setUp: function (callback) {
        this.execPlan = new ExecPlan({autoPrintOut: false, autoPrintErr: false});
        callback();
    },
    tearDown: function (callback) {
        delete this.execPlan;
        callback();
    },
    'Execution happens in order given': function (test) {
        var execPlan = this.execPlan;
        var execQueue = [];
        var makeQueuer = function (order) { return function () { execQueue.push(order) }; };
        execPlan.add(makeQueuer(1), 'echo "hi"');
        execPlan.add(makeQueuer(2), 'echo "hi"');
        execPlan.add(makeQueuer(3), 'echo "hi"');
        execPlan.add(makeQueuer(4), 'echo "hi"');
        execPlan.on('finish', function () {
            for (var i=1; i<=execQueue.length; i++) {<commit_msg>Update test/ExecPlanTest.js<commit_after>/* --- ENVIRONMENT SETUP --- */

// require external modules
var ExecPlan = require('exec-plan').ExecPlan;
var sinon    = require('sinon');
var utils    = require('../utils');

// short-hand variables
var emptyFn    = utils.emptyFn;
var isFunction = utils.isFunction;
var isDefined  = utils.isDefined;
var isNumber   = utils.isNumber;
var isString   = utils.isString;
var isObject   = utils.isObject;
var isArray    = utils.isArray;

/* ------------------------- */

/* --- TEST CASES --- */

// GROUP: verification of correct construction
exports.construction = {
    'Passing in no config object makes autoPrintOut/Err true':
            function (test) {
        var execPlan = new ExecPlan();
        test.ok(execPlan.willAutoPrintOut());
        test.ok(execPlan.willAutoPrintErr());
        test.done();
    },
    'Config {autoPrintOut:false} sets autoPrintOut/Err accordingly':
            function (test) {
        var execPlan = new ExecPlan({autoPrintOut: false});
        test.equal(execPlan.willAutoPrintOut(), false, 'execPlan will not auto print stdout');
        test.ok(execPlan.willAutoPrintErr(), 'execPlan will auto print stderr');
        test.done();
    },
    'Config {autoPrintErr:false} sets autoPrintOut/Err accordingly':
            function (test) {
        var execPlan = new ExecPlan({autoPrintErr: false});
        test.ok(execPlan.willAutoPrintOut(), 'execPlan will auto print stderr');
        test.equal(execPlan.willAutoPrintErr(), false, 'execPlan will not auto print stderr');
        test.done();
    },
    'Config {autoPrintOut:false, autoPrintErr:false} sets autoPrintOut/Err correctly':
            function (test) {
        var execPlan = new ExecPlan({autoPrintOut: false, autoPrintErr: false});
        test.equal(execPlan.willAutoPrintOut(), false, 'execPlan will not auto print stdout');
        test.equal(execPlan.willAutoPrintErr(), false, 'execPlan will not auto print stderr');
        test.done();
    }
};

// GROUP: verification that autoPrintOut/Err settings are honored by execution
exports.autoPrintOut_Err = {
    setUp: function (callback) {
        this.stdoutStub = sinon.stub(process.stdout, "write");
        this.stderrStub = sinon.stub(process.stderr, "write");
        callback();
    },
    tearDown: function (callback) {
        this.stdoutStub.restore();
        this.stderrStub.restore();
        callback();
    },
    'Prints to stdout by default': function (test) {  // this test case will serve as a template
                                                      // for the others (avoid repeating comments).
        // setup exec plan
        var testCase = this, execPlan = new ExecPlan();
        execPlan.add('ls');
        execPlan.add('ps -ef');
        execPlan.on('finish', function () {  // need to wait until plan finishes
            test.ok(testCase.stdoutStub.called, 'stdout is written to');
            test.done();
        });

        // set the asynchronous execution of the plan into motion
        execPlan.execute();
    },
    'Prints to stderr by default': function (test) {
        var testCase = this, execPlan = new ExecPlan();
        execPlan.add('./command_that_does_not_exist');
        execPlan.on('finish', function () {
            test.ok(testCase.stdoutStub.called, 'stderr is written to');
            test.done();
        });
        execPlan.execute();
    },
    'Does not print to stdout when "autoPrintOut" is turned off': function (test) {
        var testCase = this, execPlan = new ExecPlan({autoPrintOut: false});
        var errorSpy = sinon.spy();
        execPlan.add('ls');
        execPlan.on('finish', function () {
            test.ok(!testCase.stdoutStub.called, 'stdout is not written to');
            test.done();
        });
        execPlan.execute();
    },
    'Does not print to stderr when "autoPrintErr" is turned off': function (test) {
        var testCase = this, execPlan = new ExecPlan({autoPrintErr: false});
        var errorSpy = sinon.spy();
        execPlan.add('./command_that_does_not_exist');
        execPlan.on('execerror', errorSpy);
        execPlan.on('finish', function () {
            test.ok(errorSpy.called, 'error event handler is called');
            test.ok(!testCase.stderrStub.called, 'stderr is not written to');
            test.done();
        });
        execPlan.execute();
    }
};

// GROUP: verification of 'execute' method
exports.execute = {
    setUp: function (callback) {
        this.execPlan = new ExecPlan({autoPrintOut: false, autoPrintErr: false});
        callback();
    },
    tearDown: function (callback) {
        delete this.execPlan;
        callback();
    },
    'Execution happens in order given': function (test) {
        var execPlan = this.execPlan;
        var execQueue = [];
        var makeQueuer = function (order) { return function () { execQueue.push(order) }; };
        execPlan.add(makeQueuer(1), 'echo "hi"');
        execPlan.add(makeQueuer(2), 'echo "hi"');
        execPlan.add(makeQueuer(3), 'echo "hi"');
        execPlan.add(makeQueuer(4), 'echo "hi"');
        execPlan.on('finish', function () {
            for (var i=1; i<=execQueue.length; i++) {
{'fraction_non_alphanumeric': 0.1372021521906226, 'fraction_numerical': 0.0009607993850883935, 'mean_word_length': 2.495298858294157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6397592', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2957, 'n_words': 779}