Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw for likely option name problems (#1275)
* Make a start on warning for option name clashes with Command properties * Use correct property name for lookup * Shift clash detection into routine, still WIP * Prevent false positive clashes when negated option * Add tests for option name clashes * Refine advice
- Loading branch information
1 parent
0576033
commit 42f61ca
Showing
2 changed files
with
97 additions
and
0 deletions.
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,52 @@ | ||
const commander = require('../'); | ||
|
||
// Check detection of likely name clashes | ||
|
||
test('when option clashes with property then throw', () => { | ||
const program = new commander.Command(); | ||
expect(() => { | ||
program.option('-n, --name <name>'); | ||
}).toThrow(); | ||
}); | ||
|
||
test('when option clashes with property and storeOptionsAsProperties(true) then ok', () => { | ||
const program = new commander.Command(); | ||
program.storeOptionsAsProperties(true); | ||
expect(() => { | ||
program.option('-n, --name <name>'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when option would clash with property but storeOptionsAsProperties(false) then ok', () => { | ||
const program = new commander.Command(); | ||
program.storeOptionsAsProperties(false); | ||
expect(() => { | ||
program.option('-n, --name <name>'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when negated option clashes with property then throw', () => { | ||
const program = new commander.Command(); | ||
expect(() => { | ||
program.option('-E, --no-emit'); | ||
}).toThrow(); | ||
}); | ||
|
||
test('when positive and negative option then ok', () => { | ||
const program = new commander.Command(); | ||
expect(() => { | ||
program | ||
.option('-c, --colour', 'red') | ||
.option('-C, --no-colour'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('when negative and positive option then ok', () => { | ||
// Not a likely pattern, but possible and not an error. | ||
const program = new commander.Command(); | ||
expect(() => { | ||
program | ||
.option('-C, --no-colour') | ||
.option('-c, --colour'); | ||
}).not.toThrow(); | ||
}); |