-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Run default command if it's not just * #1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shadowspawn
merged 12 commits into
tj:release/5.x
from
just-be-dev:run-action-if-no-arg
Dec 11, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
94df44e
Run default command if it's not just *
just-be-dev f5ecadd
Fix lint error
just-be-dev 6a87525
Add prgm action, only emit command:* when it's registered
just-be-dev b44d1a0
Test asterisk handler not called without args
just-be-dev 1e78ec0
Restore check to not call program when signature wrong, and expand pr…
shadowspawn b9e2570
Document and rework and extend asterisk tests
shadowspawn 3d57638
Merge pull request #1 from shadowspawn/feature/add-asterisk-tests
just-be-dev a776461
Merge pull request #2 from shadowspawn/feature/really-add-asterisk-tests
just-be-dev be32fe9
Merge branch 'run-action-if-no-arg' of github.com:zephraph/commander.…
just-be-dev ec66ad5
Update * and program-action logic based on PR feedback
just-be-dev 676f23d
Rename program-action to program-command
just-be-dev 8b71d8d
Remove unnecessary command listener
just-be-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,102 @@ | ||
| const 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 | ||
| .command('*') | ||
| .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 | ||
| .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('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 .on explicitly rather than assuming covered by .command | ||
| describe(".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 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(); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.