-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First cut at displayHelpWithError * Rename method * Rename * Add typings * Simplify unknownCommand message to match others * Add chain test * Add JSDoc typing for TypeScript --checkJS * Inherit behaviour to subcommands * Add tests * Add README * Tweak wording * Add test for invalid argument showing help
- Loading branch information
1 parent
5ddc41b
commit 082717f
Showing
9 changed files
with
180 additions
and
25 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
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
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,122 @@ | ||
const commander = require('../'); | ||
|
||
describe('showHelpAfterError with message', () => { | ||
const customHelpMessage = 'See --help'; | ||
|
||
function makeProgram() { | ||
const writeMock = jest.fn(); | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.showHelpAfterError(customHelpMessage) | ||
.configureOutput({ writeErr: writeMock }); | ||
|
||
return { program, writeMock }; | ||
} | ||
|
||
test('when missing command-argument then shows help', () => { | ||
const { program, writeMock } = makeProgram(); | ||
program.argument('<file>'); | ||
let caughtErr; | ||
try { | ||
program.parse([], { from: 'user' }); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.missingArgument'); | ||
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`); | ||
}); | ||
|
||
test('when missing option-argument then shows help', () => { | ||
const { program, writeMock } = makeProgram(); | ||
program.option('--output <file>'); | ||
let caughtErr; | ||
try { | ||
program.parse(['--output'], { from: 'user' }); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.optionMissingArgument'); | ||
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`); | ||
}); | ||
|
||
test('when missing mandatory option then shows help', () => { | ||
const { program, writeMock } = makeProgram(); | ||
program.requiredOption('--password <cipher>'); | ||
let caughtErr; | ||
try { | ||
program.parse([], { from: 'user' }); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.missingMandatoryOptionValue'); | ||
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`); | ||
}); | ||
|
||
test('when unknown option then shows help', () => { | ||
const { program, writeMock } = makeProgram(); | ||
let caughtErr; | ||
try { | ||
program.parse(['--unknown-option'], { from: 'user' }); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.unknownOption'); | ||
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`); | ||
}); | ||
|
||
test('when too many command-arguments then shows help', () => { | ||
const { program, writeMock } = makeProgram(); | ||
program | ||
.allowExcessArguments(false); | ||
let caughtErr; | ||
try { | ||
program.parse(['surprise'], { from: 'user' }); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.excessArguments'); | ||
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`); | ||
}); | ||
|
||
test('when unknown command then shows help', () => { | ||
const { program, writeMock } = makeProgram(); | ||
program.command('sub1'); | ||
let caughtErr; | ||
try { | ||
program.parse(['sub2'], { from: 'user' }); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.unknownCommand'); | ||
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`); | ||
}); | ||
|
||
test('when invalid option choice then shows help', () => { | ||
const { program, writeMock } = makeProgram(); | ||
program.addOption(new commander.Option('--color').choices(['red', 'blue'])); | ||
let caughtErr; | ||
try { | ||
program.parse(['--color', 'pink'], { from: 'user' }); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.invalidArgument'); | ||
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`); | ||
}); | ||
}); | ||
|
||
test('when showHelpAfterError() and error and then shows full help', () => { | ||
const writeMock = jest.fn(); | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.showHelpAfterError() | ||
.configureOutput({ writeErr: writeMock }); | ||
|
||
try { | ||
program.parse(['--unknown-option'], { from: 'user' }); | ||
} catch (err) { | ||
} | ||
expect(writeMock).toHaveBeenLastCalledWith(program.helpInformation()); | ||
}); |
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
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