Run default command if it's not just * (#1062)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Run default command if it's not just * * Fix lint error * Add prgm action, only emit command:* when it's registered * Test asterisk handler not called without args * Restore check to not call program when signature wrong, and expand program tests * Document and rework and extend asterisk tests * Update * and program-action logic based on PR feedback * Rename program-action to program-command * Remove unnecessary command listener
authored by Justin Bennett · committed by John Gee · parent 9de0968 · Browse files · 3 files·+133 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+133 added−36 removed
- tests:+123 added−30 removed
- other:+10 added−6 removed
Provenance
- Commit
4fb9ed4- Tree
6d999a2- Author
Justin Bennett <zephraph@gmail.com>- Committer
John Gee <john@ruru.gen.nz>- Parent
9de0968
3 files changed+133 added−36 removed
modified:tests/command.action.test.js+27 added-2 removed
33 unmodified lines343536373838394041424344454647484950515253545556575859606122 unmodified lines848586877888899091924 unmodified lines97989910010110210310410510610710810911011111211311411533 unmodified lines program.parse(['node', 'test', 'info', 'my-file', 'a']); expect(actionMock).toHaveBeenCalledWith('my-file', cmd, ['a']);});
test('when .action on program with argument then action called', () => {test('when .action on program with required argument and argument supplied then action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('<file>') .action(actionMock); program.parse(['node', 'test', 'my-file']); expect(actionMock).toHaveBeenCalledWith('my-file', program);});
test('when .action on program with required argument and argument not supplied then action not called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('<file>') .action(actionMock); program.parse(['node', 'test']); expect(actionMock).not.toHaveBeenCalled();});
// Changes made in #729 to call program action handlertest('when .action on program and no arguments then action called', () => { const actionMock = jest.fn(); const program = new commander.Command();22 unmodified lines program.parse(['node', 'test']); expect(actionMock).toHaveBeenCalledWith(undefined, program);});
test('when .action on program with subcommand and program argument then program action called', () => {test('when .action on program with optional argument and subcommand and program argument then program action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('[file]')4 unmodified lines program.parse(['node', 'test', 'a']);
expect(actionMock).toHaveBeenCalledWith('a', program);});
// Changes made in #1062 to allow this casetest('when .action on program with optional argument and subcommand and no program argument then program action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('[file]') .action(actionMock); program .command('subcommand');
program.parse(['node', 'test']);
expect(actionMock).toHaveBeenCalledWith(undefined, program);});modified:tests/command.asterisk.test.js+96 added-28 removed
1234567891011345678910111213141516171819202122232425262728293031323313141516171819202122343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990912526272829303132339293949596979899100101102const commander = require('../');
test('when no arguments then asterisk action not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install') .action(mockAction); program.parse(['node', 'test']); expect(mockAction).not.toHaveBeenCalled();});// .command('*') is the old main/default command handler. It adds a listener// for 'command:*'. It has been somewhat replaced by the program action handler,// so most uses are probably old code. Current plan is keep the code backwards compatible// and put work in elsewhere for new code (e.g. evolving behaviour for program action handler).//// The event 'command:*' is also listened for directly for testing for unknown commands// due to an example in the README, although this is not robust (e.g. sent for git-style commands).//// Historical: the event 'command:*' used to also be shared by the action handler on the program.
describe(".command('*')", () => { test('when no arguments then asterisk action not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('*') .action(mockAction); program.parse(['node', 'test']); expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised argument then asterisk action called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('*') .action(mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });
test('when recognised command then asterisk action not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install') .action(() => { }); program .action(mockAction); program.parse(['node', 'test', 'install']); expect(mockAction).not.toHaveBeenCalled(); test('when recognised command then asterisk action not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install') .action(() => { }); program .command('*') .action(mockAction); program.parse(['node', 'test', 'install']); expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised command/argument then asterisk action called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install'); program .command('*') .action(mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });});
// Test .on explicitly rather than assuming covered by .commanddescribe(".on('command:*')", () => { test('when no arguments then listener not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .on('command:*', mockAction); program.parse(['node', 'test']); expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised argument then listener called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .on('command:*', mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });
test('when recognised command then listener not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install') .action(() => { }); program .on('command:*', mockAction); program.parse(['node', 'test', 'install']); expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised command/argument then asterisk action called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install'); program .action(mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); test('when unrecognised command/argument then listener called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install'); program .on('command:*', mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });});modified:index.js+10 added-6 removed
367 unmodified lines368 369 fn.apply(self, actionArgs);370 };371 var parent = this.parent || this;372- var name = parent === this ? '*' : this._name;373- parent.on('command:' + name, listener);372+ if (parent === this) {373+ parent.on('program-command', listener);374+ } else {375+ parent.on('command:' + this._name, listener);376+ }377+378 if (this._alias) parent.on('command:' + this._alias, listener);379 return this;380 };381 491 unmodified lines873 name = args[0];874 if (this.listeners('command:' + name).length) {875 this.emit('command:' + args.shift(), args, unknown);876 } else {877+ this.emit('program-command', args, unknown);878 this.emit('command:*', args, unknown);879 }880 } else {881 outputHelpIfNecessary(this, unknown);877-882 // If there were no args and we have unknown options,883 // then they are extraneous and we need to error.884 if (unknown.length > 0 && !this.defaultExecutable) {885 this.unknownOption(unknown[0]);886 }883- if (this.commands.length === 0 &&884- this._args.filter(function(a) { return a.required; }).length === 0) {885- this.emit('command:*');887+ // Call the program action handler, unless it has a (missing) required parameter and signature does not match.888+ if (this._args.filter(function(a) { return a.required; }).length === 0) {889+ this.emit('program-command');890 }891 }892 893 return this;739 unmodified lines