<commit_before>'use strict';

const Shipment = require('./../lib/Shipment');
const Action   = require('./../lib/Action');

require('./support/index');

const testShipment = require('./fixtures/testShipment');

const sinon    = require('sinon');
const Bluebird = require('bluebird');
const execa    = require('execa');
const path     = require('path');
const request  = require('supertest');

describe('Shipment', () => {

    /**
     * Return a fluent assertion builder for the complete execa result of a call to testcli with the given args
     * @param {String[]} args
     * @returns {Function}
     */
    const resultOf = (args) => {
        return execa('node', [path.join(__dirname, 'fixtures/testCli.js')].concat(args));
    };

    /**
     * Return a fluent assertion builder for the stdout buffer of a call to testcli with the given args
     * @param {String[]} args
     * @returns {Function}
     */
    const stdoutOf = (args) => {
        let builder    = resultOf(args).should.eventually.have.property('stdout');
        builder.should = builder;
        return builder;
    };

    let actionSpy, shipment;

    beforeEach(() => {

        actionSpy = sinon.spy();

        shipment = testShipment(actionSpy);
    });

    describe('cli', () => {

        let CustomShipment, customShipment, mockPkg, updateNotifierSpy;

        beforeEach(() => {

            updateNotifierSpy             = sinon.spy();
            mockPkg                       = {version: '1.4'};
            CustomShipment                = class extends Shipment {
            };
            CustomShipment.updateNotifier = options => {
                return {
                    notify: () => updateNotifierSpy(options.pkg)
                };
            };
            customShipment                = new CustomShipment([], {pkg: mockPkg});
        });

        it('should execute the requested action', () => {

            actionSpy.should.not.have.been.called;

            shipment.cli(['do-something']);

            // Because there is no way to intercept the control flow when issuing subcommands via yargs
            // Could also use an eventemitter architecture
            return Bluebird.delay(20).then(() => {

                actionSpy.should.have.been.calledOnce;
                let action = actionSpy.firstCall.args[0];
                action.should.be.an.instanceOf(Action);
                action.getName().should.equal('do-something');
            });
        });

        it('should invoke updateNotifier', () => {

            customShipment.cli(['']);
            updateNotifierSpy.should.have.been.calledWith(mockPkg);
        });

        it('...unless noUpdateNotifier is given', () => {

            let customShipment = new CustomShipment([], {pkg: mockPkg, noUpdateNotifier: true});
            customShipment.cli(['']);
            updateNotifierSpy.should.not.have.been.called;
        });

        describe('subcommand', () => {

            it('should output the return value of "run"', () => {

                return stdoutOf(['do-something']).should.have.string('did something');
            });
        });

        describe('subcommand with input arguments', () => {

            it('should take input arguments', () => {

                return stdoutOf(['to-upper', '--message', 'very very cool message']).should.have.string('VERY VERY COOL MESSAGE');
            });
        });

        describe('subcommand that throws', () => {

            it('should have a non-zero exitcode', () => {

                return resultOf(['fail']).catch(error => {
                    error.should.have.property('code').that.is.not.equal(0);
                    return 'ok';
                }).should.eventually.equal('ok');
            });
        });

        describe('--help', () => {

            it('should print the subcommands', () => {

                return stdoutOf(['--help']).should
                    .have.string('do-something').and
                    .have.string('do-something-else').and
                    .have.string('fail');
            });
        });

        describe('--version', () => {

            it('should print a version number', () => {
<commit_msg>Add test for error reporter<commit_after>'use strict';

const Shipment = require('./../lib/Shipment');
const Action   = require('./../lib/Action');

require('./support/index');

const testShipment = require('./fixtures/testShipment');

const sinon    = require('sinon');
const Bluebird = require('bluebird');
const execa    = require('execa');
const path     = require('path');
const request  = require('supertest');

describe('Shipment', () => {

    /**
     * Return a fluent assertion builder for the complete execa result of a call to testcli with the given args
     * @param {String[]} args
     * @returns {Function}
     */
    const resultOf = (args) => {
        return execa('node', [path.join(__dirname, 'fixtures/testCli.js')].concat(args));
    };

    /**
     * Return a fluent assertion builder for the stdout buffer of a call to testcli with the given args
     * @param {String[]} args
     * @returns {Function}
     */
    const stdoutOf = (args) => {
        let builder    = resultOf(args).should.eventually.have.property('stdout');
        builder.should = builder;
        return builder;
    };

    let actionSpy, shipment;

    beforeEach(() => {

        actionSpy = sinon.spy();

        shipment = testShipment(actionSpy);
    });

    describe('cli', () => {

        let CustomShipment, customShipment, mockPkg, updateNotifierSpy;

        beforeEach(() => {

            updateNotifierSpy             = sinon.spy();
            mockPkg                       = {version: '1.4'};
            CustomShipment                = class extends Shipment {
            };
            CustomShipment.updateNotifier = options => {
                return {
                    notify: () => updateNotifierSpy(options.pkg)
                };
            };
            customShipment                = new CustomShipment([], {pkg: mockPkg});
        });

        it('should execute the requested action', () => {

            actionSpy.should.not.have.been.called;

            shipment.cli(['do-something']);

            // Because there is no way to intercept the control flow when issuing subcommands via yargs
            // Could also use an eventemitter architecture
            return Bluebird.delay(20).then(() => {

                actionSpy.should.have.been.calledOnce;
                let action = actionSpy.firstCall.args[0];
                action.should.be.an.instanceOf(Action);
                action.getName().should.equal('do-something');
            });
        });

        it('should invoke updateNotifier', () => {

            customShipment.cli(['']);
            updateNotifierSpy.should.have.been.calledWith(mockPkg);
        });

        it('...unless noUpdateNotifier is given', () => {

            let customShipment = new CustomShipment([], {pkg: mockPkg, noUpdateNotifier: true});
            customShipment.cli(['']);
            updateNotifierSpy.should.not.have.been.called;
        });

        describe('subcommand', () => {

            it('should output the return value of "run"', () => {

                return stdoutOf(['do-something']).should.have.string('did something');
            });
        });

        describe('subcommand with input arguments', () => {

            it('should take input arguments', () => {

                return stdoutOf(['to-upper', '--message', 'very very cool message']).should.have.string('VERY VERY COOL MESSAGE');
            });
        });

        describe('subcommand that throws', () => {

            it('should print the error message', () => {

                return stdoutOf(['fail']).should.have.string('something went awfully wrong');
            });

            it('should have a non-zero exitcode', () => {

                return resultOf(['fail']).catch(error => {
                    error.should.have.property('code').that.is.not.equal(0);
                    return 'ok';
                }).should.eventually.equal('ok');
            });
        });

        describe('--help', () => {

            it('should print the subcommands', () => {

                return stdoutOf(['--help']).should
                    .have.string('do-something').and
                    .have.string('do-something-else').and
                    .have.string('fail');
            });
        });

        describe('--version', () => {

            it('should print a version number', () => {

{'fraction_non_alphanumeric': 0.1488545179672055, 'fraction_numerical': 0.0013955111059425515, 'mean_word_length': 1.8401585204755615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3551736', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2234, 'n_words': 494}