From 606f944b9e57de69f4b117df00405931964ff4e7 Mon Sep 17 00:00:00 2001 From: yeonhoyoon Date: Wed, 8 Jan 2020 14:03:00 +0900 Subject: [PATCH 1/2] output help only when there are no arguments knex cli should output help only when there are no arguments. current implementation always outputs help, even with correct arguments, such as `knex migrate:status` the argument length should be checked via process.argv. reference: https://github.com/tj/commander.js/#outputhelpcb Add tests for help command --- bin/cli.js | 2 +- test/cli/help.spec.js | 33 +++++++++++++++++++++++++++++++++ test/index.js | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/cli/help.spec.js diff --git a/bin/cli.js b/bin/cli.js index e71422a784..e97bbd1df9 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -341,7 +341,7 @@ function invoke(env) { .catch(exit); }); - if (!commander._args.length) { + if (!process.argv.slice(2).length) { commander.outputHelp(); } diff --git a/test/cli/help.spec.js b/test/cli/help.spec.js new file mode 100644 index 0000000000..1903adfa00 --- /dev/null +++ b/test/cli/help.spec.js @@ -0,0 +1,33 @@ +'use strict'; + +const path = require('path'); +const { execCommand } = require('cli-testlab'); + +const cliPkg = require('../../package'); +const KNEX = path.normalize(__dirname + '/../../bin/cli.js'); + +describe('help', () => { + it('Prints help', () => { + return execCommand(`node ${KNEX} --help`, { + expectedOutput: 'Usage', + }); + }); + + it('Prints help using -h flag', () => { + return execCommand(`node ${KNEX} -h`, { + expectedOutput: 'Usage', + }); + }); + + it('Prints help when no arguments are given', () => { + return execCommand(`node ${KNEX}`, { + expectedOutput: 'Usage', + }); + }); + + it('Does not print help when argument is given', () => { + return execCommand(`node ${KNEX} -V`).then(({ stdout, _ }) => { + expect(stdout).to.not.include('Usage'); + }); + }); +}); diff --git a/test/index.js b/test/index.js index da39d21087..37331aa2bc 100644 --- a/test/index.js +++ b/test/index.js @@ -93,4 +93,5 @@ describe('CLI tests', function() { require('./cli/migrate-make.spec'); require('./cli/seed-make.spec'); require('./cli/version.spec'); + require('./cli/help.spec'); }); From 85a78ea440df40d797145ce44970f8f0a221403a Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 14 Jan 2020 11:46:26 +0900 Subject: [PATCH 2/2] Update cli-testlab version to 1.10.0 to use 'notExpectedOutput' functionality. --- package.json | 2 +- test/cli/help.spec.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 4e2d6298ab..19f843ac0c 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "JSONStream": "^1.3.5", "chai": "^4.2.0", "chai-subset-in-order": "^2.1.3", - "cli-testlab": "^1.8.0", + "cli-testlab": "^1.10.0", "coveralls": "^3.0.9", "cross-env": "^6.0.3", "dtslint": "2.0.2", diff --git a/test/cli/help.spec.js b/test/cli/help.spec.js index 1903adfa00..a503b7f655 100644 --- a/test/cli/help.spec.js +++ b/test/cli/help.spec.js @@ -3,7 +3,6 @@ const path = require('path'); const { execCommand } = require('cli-testlab'); -const cliPkg = require('../../package'); const KNEX = path.normalize(__dirname + '/../../bin/cli.js'); describe('help', () => { @@ -26,8 +25,8 @@ describe('help', () => { }); it('Does not print help when argument is given', () => { - return execCommand(`node ${KNEX} -V`).then(({ stdout, _ }) => { - expect(stdout).to.not.include('Usage'); + return execCommand(`node ${KNEX} -V`, { + notExpectedOutput: 'Usage', }); }); });