Skip to content

Commit

Permalink
output help only when there are no arguments
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yeonhoyoon committed Jan 13, 2020
1 parent 2e3c7fe commit 606f944
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bin/cli.js
Expand Up @@ -341,7 +341,7 @@ function invoke(env) {
.catch(exit);
});

if (!commander._args.length) {
if (!process.argv.slice(2).length) {
commander.outputHelp();
}

Expand Down
33 changes: 33 additions & 0 deletions 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');
});
});
});
1 change: 1 addition & 0 deletions test/index.js
Expand Up @@ -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');
});

0 comments on commit 606f944

Please sign in to comment.