From 27e388fc315a360ab47e8a2fdc50ca9fdedf040c Mon Sep 17 00:00:00 2001 From: Brian Mann Date: Mon, 4 Sep 2017 02:15:43 -0400 Subject: [PATCH] cli: more comprehensive tests around cli + util, exits, rejected promises, etc --- cli/lib/cli.js | 8 ++--- cli/lib/util.js | 4 --- cli/test/lib/cli_spec.js | 67 +++++++++++++++++++++++++++++++++------ cli/test/lib/util_spec.js | 28 ++++++++++++++++ 4 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 cli/test/lib/util_spec.js diff --git a/cli/lib/cli.js b/cli/lib/cli.js index 2af3744a0d..374973d5d0 100644 --- a/cli/lib/cli.js +++ b/cli/lib/cli.js @@ -58,7 +58,7 @@ module.exports = { require('./exec/run') .start(parseOpts(opts)) .then(util.exit) - .catch(util.exit1) + .catch(util.logErrorExit1) }) program @@ -73,7 +73,7 @@ module.exports = { .action((opts) => { require('./exec/open') .start(parseOpts(opts)) - .catch(util.exit1) + .catch(util.logErrorExit1) }) program @@ -82,7 +82,7 @@ module.exports = { .action(() => { require('./tasks/install') .start({ force: true }) - .catch(util.exit1) + .catch(util.logErrorExit1) }) program @@ -91,7 +91,7 @@ module.exports = { .action(() => { require('./tasks/verify') .start({ force: true }) - .catch(util.exit1) + .catch(util.logErrorExit1) }) debug('cli starts with arguments %j', process.argv) diff --git a/cli/lib/util.js b/cli/lib/util.js index f34141d2e3..67acf3414a 100644 --- a/cli/lib/util.js +++ b/cli/lib/util.js @@ -18,10 +18,6 @@ module.exports = { process.exit(code) }, - exit1 () { - process.exit(1) - }, - logErrorExit1 (err) { logger.error(err.message) diff --git a/cli/test/lib/cli_spec.js b/cli/test/lib/cli_spec.js index 281d938599..7e204fb822 100644 --- a/cli/test/lib/cli_spec.js +++ b/cli/test/lib/cli_spec.js @@ -1,6 +1,7 @@ require('../spec_helper') const cli = require(`${lib}/cli`) +const util = require(`${lib}/util`) const run = require(`${lib}/exec/run`) const open = require(`${lib}/exec/open`) const verify = require(`${lib}/tasks/verify`) @@ -9,20 +10,32 @@ const install = require(`${lib}/tasks/install`) describe('cli', function () { beforeEach(function () { this.sandbox.stub(process, 'exit') + this.sandbox.stub(util, 'exit') + this.sandbox.stub(util, 'logErrorExit1') this.exec = (args) => cli.init().parse(`node test ${args}`.split(' ')) }) - it('exits when done', function (done) { - this.sandbox.stub(run, 'start').resolves() - this.exec('run --port 7878') - - process.exit.callsFake(done) - }) - - it('run calls run.start with options', function () { - this.sandbox.stub(run, 'start').resolves() + it('run calls run.start with options + exits with code', function (done) { + this.sandbox.stub(run, 'start').resolves(10) this.exec('run --port 7878') expect(run.start).to.be.calledWith({ port: '7878' }) + + util.exit.callsFake((code) => { + expect(code).to.eq(10) + done() + }) + }) + + it('run calls run.start with options + catches errors', function (done) { + const err = new Error('foo') + this.sandbox.stub(run, 'start').rejects(err) + this.exec('run --port 7878') + expect(run.start).to.be.calledWith({ port: '7878' }) + + util.logErrorExit1.callsFake((e) => { + expect(e).to.eq(err) + done() + }) }) it('open calls open.start with options', function () { @@ -31,15 +44,51 @@ describe('cli', function () { expect(open.start).to.be.calledWith({ port: '7878' }) }) + it('open calls open.start + catches errors', function (done) { + const err = new Error('foo') + + this.sandbox.stub(open, 'start').rejects(err) + this.exec('open --port 7878') + + util.logErrorExit1.callsFake((e) => { + expect(e).to.eq(err) + done() + }) + }) + it('install calls install.start with force: true', function () { this.sandbox.stub(install, 'start').resolves() this.exec('install') expect(install.start).to.be.calledWith({ force: true }) }) + it('install calls install.start + catches errors', function (done) { + const err = new Error('foo') + + this.sandbox.stub(install, 'start').rejects(err) + this.exec('install') + + util.logErrorExit1.callsFake((e) => { + expect(e).to.eq(err) + done() + }) + }) + it('verify calls verify.start with force: true', function () { this.sandbox.stub(verify, 'start').resolves() this.exec('verify') expect(verify.start).to.be.calledWith({ force: true }) }) + + it('verify calls verify.start + catches errors', function (done) { + const err = new Error('foo') + + this.sandbox.stub(verify, 'start').rejects(err) + this.exec('verify') + + util.logErrorExit1.callsFake((e) => { + expect(e).to.eq(err) + done() + }) + }) }) diff --git a/cli/test/lib/util_spec.js b/cli/test/lib/util_spec.js new file mode 100644 index 0000000000..f5dccb12bf --- /dev/null +++ b/cli/test/lib/util_spec.js @@ -0,0 +1,28 @@ +require('../spec_helper') + +const util = require(`${lib}/util`) +const logger = require(`${lib}/logger`) + +describe('util', function () { + beforeEach(function () { + this.sandbox.stub(process, 'exit') + this.sandbox.stub(logger, 'error') + }) + + it('.exit', function () { + util.exit(2) + expect(process.exit).to.be.calledWith(2) + + util.exit(0) + expect(process.exit).to.be.calledWith(0) + }) + + it('.logErrorExit1', function () { + const err = new Error('foo') + + util.logErrorExit1(err) + + expect(process.exit).to.be.calledWith(1) + expect(logger.error).to.be.calledWith('foo') + }) +})