Skip to content

Commit

Permalink
Added help print when operation is omitted
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaanemaa committed Apr 29, 2019
1 parent 4879884 commit 2275ca8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/modules/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ program
process.exit(1);
});

// Parse args
program.parse(process.argv);

// Show help if no operation is provided
if (!commandArgs.operation) {
program.outputHelp();
process.exit(1);
}

module.exports = { ...commandArgs, ...program.opts() };
23 changes: 23 additions & 0 deletions test/modules/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ describe('options', () => {
});

it('should show error and exit on unknown operation', () => {
/*
This test runs over both the unknown an omitted operation scenarios because
both use process.exit(1) but since we mock it, it will run over it and not exit.
Thus triggering both paths, which in real usage is not possible
*/
global.process.argv = ['node', 'dummy.js', 'dance', 'mango'];
global.console.error = jest.fn();
global.process.exit = jest.fn();
const { Command } = require('commander');
Command.prototype.outputHelp = jest.fn();

require('../../src/modules/options');

Expand All @@ -32,7 +39,23 @@ describe('options', () => {
'Unknown operation: dance',
'\nUse --help to see all options',
);
expect(global.process.exit).toHaveBeenCalledTimes(2);
expect(global.process.exit).toHaveBeenCalledWith(1);
expect(Command.prototype.outputHelp).toHaveBeenCalledTimes(1);
});

it('should show help and exit if operation is omitted', () => {
global.process.argv = ['node', 'dummy.js'];
global.console.error = jest.fn();
global.process.exit = jest.fn();
const { Command } = require('commander');
Command.prototype.outputHelp = jest.fn();

require('../../src/modules/options');

expect(global.console.error).toHaveBeenCalledTimes(0);
expect(global.process.exit).toHaveBeenCalledTimes(1);
expect(global.process.exit).toHaveBeenCalledWith(1);
expect(Command.prototype.outputHelp).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 2275ca8

Please sign in to comment.