mirror of
https://github.com/spf13/cobra
synced 2025-04-27 09:07:19 +00:00
[update] More tests that were requested
Root error prefix called from subcommand, and different error prefixes in root and subcommand. https://github.com/spf13/cobra/pull/2023#discussion_r1319266611
This commit is contained in:
parent
fabc43c5eb
commit
ed4583eb59
1 changed files with 26 additions and 5 deletions
|
@ -1099,16 +1099,37 @@ func TestShorthandVersionTemplate(t *testing.T) {
|
|||
checkStringContains(t, output, "customized version: 1.0.0")
|
||||
}
|
||||
|
||||
func TestErrPrefix(t *testing.T) {
|
||||
rootCmd := &Command{Use: "root", SilenceUsage: true, Run: emptyRun}
|
||||
rootCmd.SetErrPrefix("customized error prefix:")
|
||||
func TestRootErrPrefixExecutedOnSubcommand(t *testing.T) {
|
||||
rootCmd := &Command{Use: "root", Run: emptyRun}
|
||||
rootCmd.SetErrPrefix("root error prefix:")
|
||||
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})
|
||||
|
||||
output, err := executeCommand(rootCmd, "--unknown", "flag")
|
||||
output, err := executeCommand(rootCmd, "sub", "--unknown-flag")
|
||||
if err == nil {
|
||||
t.Errorf("Expected error")
|
||||
}
|
||||
|
||||
checkStringContains(t, output, "customized error prefix: unknown flag: --unknown")
|
||||
checkStringContains(t, output, "root error prefix: unknown flag: --unknown-flag")
|
||||
}
|
||||
|
||||
func TestRootAndSubErrPrefix(t *testing.T) {
|
||||
rootCmd := &Command{Use: "root", Run: emptyRun}
|
||||
subCmd := &Command{Use: "sub", Run: emptyRun}
|
||||
rootCmd.AddCommand(subCmd)
|
||||
rootCmd.SetErrPrefix("root error prefix:")
|
||||
subCmd.SetErrPrefix("sub error prefix:")
|
||||
|
||||
if output, err := executeCommand(rootCmd, "--unknown-root-flag"); err == nil {
|
||||
t.Errorf("Expected error")
|
||||
} else {
|
||||
checkStringContains(t, output, "root error prefix: unknown flag: --unknown-root-flag")
|
||||
}
|
||||
|
||||
if output, err := executeCommand(rootCmd, "sub", "--unknown-sub-flag"); err == nil {
|
||||
t.Errorf("Expected error")
|
||||
} else {
|
||||
checkStringContains(t, output, "sub error prefix: unknown flag: --unknown-sub-flag")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionFlagExecutedOnSubcommand(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue