-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add .allowExcessArguments() and error message #1407
Merged
shadowspawn
merged 7 commits into
tj:release/7.x
from
shadowspawn:feature/error-for-unexpected-arrguments
Dec 4, 2020
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
08688d1
Fix test file name
shadowspawn 5ea7774
Add initial support for allowExcessArguments
shadowspawn 636767f
Add TypeScript
shadowspawn 7ae352d
Add tests
shadowspawn 7cf8aa8
Refine error for program
shadowspawn 6f0c80b
Remove exception for legacy asterisk command, can disable if needed
shadowspawn a42d5c9
Use defaulr parameters and simplify code
shadowspawn 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 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 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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
const commander = require('../'); | ||
|
||
// Not testing output, just testing whether an error is detected. | ||
|
||
describe('allowUnknownOption', () => { | ||
// Optional. Use internal knowledge to suppress output to keep test output clean. | ||
let writeErrorSpy; | ||
|
||
beforeAll(() => { | ||
writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); | ||
}); | ||
|
||
afterEach(() => { | ||
writeErrorSpy.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
writeErrorSpy.mockRestore(); | ||
}); | ||
|
||
test('when specify excess program argument then ignored by default', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['excess'], { from: 'user' }); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when specify excess program argument and allowExcessArguments(false) then error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.allowExcessArguments(false) | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['excess'], { from: 'user' }); | ||
}).toThrow(); | ||
}); | ||
|
||
test('when specify excess program argument and allowExcessArguments() then no error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.allowExcessArguments() | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['excess'], { from: 'user' }); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when specify excess program argument and allowExcessArguments(true) then no error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.allowExcessArguments(true) | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['excess'], { from: 'user' }); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when specify excess command argument then no error (by default)', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.command('sub') | ||
.action(() => { }); | ||
|
||
expect(() => { | ||
program.parse(['sub', 'excess'], { from: 'user' }); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when specify excess command argument and allowExcessArguments(false) then error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.command('sub') | ||
.allowUnknownOption() | ||
.allowExcessArguments(false) | ||
.action(() => { }); | ||
|
||
expect(() => { | ||
program.parse(['sub', 'excess'], { from: 'user' }); | ||
}).toThrow(); | ||
}); | ||
|
||
test('when specify expected arg and allowExcessArguments(false) then no error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.arguments('<file>') | ||
.exitOverride() | ||
.allowExcessArguments(false) | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['file'], { from: 'user' }); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when specify excess after <arg> and allowExcessArguments(false) then error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.arguments('<file>') | ||
.exitOverride() | ||
.allowExcessArguments(false) | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['file', 'excess'], { from: 'user' }); | ||
}).toThrow(); | ||
}); | ||
|
||
test('when specify excess after [arg] and allowExcessArguments(false) then error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.arguments('[file]') | ||
.exitOverride() | ||
.allowExcessArguments(false) | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['file', 'excess'], { from: 'user' }); | ||
}).toThrow(); | ||
}); | ||
|
||
test('when specify args for [args...] and allowExcessArguments(false) then no error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.arguments('[files...]') | ||
.exitOverride() | ||
.allowExcessArguments(false) | ||
.action(() => {}); | ||
|
||
expect(() => { | ||
program.parse(['file1', 'file2', 'file3'], { from: 'user' }); | ||
}).not.toThrow(); | ||
}); | ||
}); |
This file contains 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 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
File renamed without changes.
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the default parameters available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, I'll look into that. We have a few methods with this sort of pattern that could use it. Likely can because we have moved forward our minimum version of node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults from node 6, which is our minimum supported version: https://node.green/#ES2015-syntax-default-function-parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added default parameter instead of manual test to multiple routines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for fixing it.