Skip to content

Commit

Permalink
[update] More tests that were requested
Browse files Browse the repository at this point in the history
Root error prefix called from subcommand, and different error prefixes in root and subcommand.
#2023 (comment)
  • Loading branch information
5ouma committed Sep 8, 2023
1 parent fabc43c commit ed4583e
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ed4583e

Please sign in to comment.