[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:
5ouma 2023-09-08 12:10:16 +09:00
parent fabc43c5eb
commit ed4583eb59
No known key found for this signature in database

View file

@ -1099,16 +1099,37 @@ func TestShorthandVersionTemplate(t *testing.T) {
checkStringContains(t, output, "customized version: 1.0.0") checkStringContains(t, output, "customized version: 1.0.0")
} }
func TestErrPrefix(t *testing.T) { func TestRootErrPrefixExecutedOnSubcommand(t *testing.T) {
rootCmd := &Command{Use: "root", SilenceUsage: true, Run: emptyRun} rootCmd := &Command{Use: "root", Run: emptyRun}
rootCmd.SetErrPrefix("customized error prefix:") 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 { if err == nil {
t.Errorf("Expected error") 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) { func TestVersionFlagExecutedOnSubcommand(t *testing.T) {