mirror of
https://github.com/spf13/cobra
synced 2025-05-07 13:57:21 +00:00
handle error return values
This commit is contained in:
parent
7b62ee10c2
commit
d6568bad2e
5 changed files with 35 additions and 35 deletions
|
@ -52,7 +52,9 @@ func runShellCheck(s string) error {
|
|||
return err
|
||||
}
|
||||
go func() {
|
||||
stdin.Write([]byte(s))
|
||||
_, err := stdin.Write([]byte(s))
|
||||
er(err)
|
||||
|
||||
stdin.Close()
|
||||
}()
|
||||
|
||||
|
@ -74,26 +76,26 @@ func TestBashCompletions(t *testing.T) {
|
|||
Run: emptyRun,
|
||||
}
|
||||
rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")
|
||||
rootCmd.MarkFlagRequired("introot")
|
||||
er(rootCmd.MarkFlagRequired("introot"))
|
||||
|
||||
// Filename.
|
||||
rootCmd.Flags().String("filename", "", "Enter a filename")
|
||||
rootCmd.MarkFlagFilename("filename", "json", "yaml", "yml")
|
||||
er(rootCmd.MarkFlagFilename("filename", "json", "yaml", "yml"))
|
||||
|
||||
// Persistent filename.
|
||||
rootCmd.PersistentFlags().String("persistent-filename", "", "Enter a filename")
|
||||
rootCmd.MarkPersistentFlagFilename("persistent-filename")
|
||||
rootCmd.MarkPersistentFlagRequired("persistent-filename")
|
||||
er(rootCmd.MarkPersistentFlagFilename("persistent-filename"))
|
||||
er(rootCmd.MarkPersistentFlagRequired("persistent-filename"))
|
||||
|
||||
// Filename extensions.
|
||||
rootCmd.Flags().String("filename-ext", "", "Enter a filename (extension limited)")
|
||||
rootCmd.MarkFlagFilename("filename-ext")
|
||||
er(rootCmd.MarkFlagFilename("filename-ext"))
|
||||
rootCmd.Flags().String("custom", "", "Enter a filename (extension limited)")
|
||||
rootCmd.MarkFlagCustom("custom", "__complete_custom")
|
||||
er(rootCmd.MarkFlagCustom("custom", "__complete_custom"))
|
||||
|
||||
// Subdirectories in a given directory.
|
||||
rootCmd.Flags().String("theme", "", "theme to use (located in /themes/THEMENAME/)")
|
||||
rootCmd.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"})
|
||||
er(rootCmd.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"}))
|
||||
|
||||
// For two word flags check
|
||||
rootCmd.Flags().StringP("two", "t", "", "this is two word flags")
|
||||
|
@ -109,9 +111,9 @@ func TestBashCompletions(t *testing.T) {
|
|||
}
|
||||
|
||||
echoCmd.Flags().String("filename", "", "Enter a filename")
|
||||
echoCmd.MarkFlagFilename("filename", "json", "yaml", "yml")
|
||||
er(echoCmd.MarkFlagFilename("filename", "json", "yaml", "yml"))
|
||||
echoCmd.Flags().String("config", "", "config to use (located in /config/PROFILE/)")
|
||||
echoCmd.Flags().SetAnnotation("config", BashCompSubdirsInDir, []string{"config"})
|
||||
er(echoCmd.Flags().SetAnnotation("config", BashCompSubdirsInDir, []string{"config"}))
|
||||
|
||||
printCmd := &Command{
|
||||
Use: "print [string to print]",
|
||||
|
@ -149,7 +151,7 @@ func TestBashCompletions(t *testing.T) {
|
|||
rootCmd.AddCommand(echoCmd, printCmd, deprecatedCmd, colonCmd)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
rootCmd.GenBashCompletion(buf)
|
||||
er(rootCmd.GenBashCompletion(buf))
|
||||
output := buf.String()
|
||||
|
||||
check(t, output, "_root")
|
||||
|
@ -209,10 +211,10 @@ func TestBashCompletionHiddenFlag(t *testing.T) {
|
|||
|
||||
const flagName = "hiddenFlag"
|
||||
c.Flags().Bool(flagName, false, "")
|
||||
c.Flags().MarkHidden(flagName)
|
||||
er(c.Flags().MarkHidden(flagName))
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
c.GenBashCompletion(buf)
|
||||
er(c.GenBashCompletion(buf))
|
||||
output := buf.String()
|
||||
|
||||
if strings.Contains(output, flagName) {
|
||||
|
@ -225,10 +227,10 @@ func TestBashCompletionDeprecatedFlag(t *testing.T) {
|
|||
|
||||
const flagName = "deprecated-flag"
|
||||
c.Flags().Bool(flagName, false, "")
|
||||
c.Flags().MarkDeprecated(flagName, "use --not-deprecated instead")
|
||||
er(c.Flags().MarkDeprecated(flagName, "use --not-deprecated instead"))
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
c.GenBashCompletion(buf)
|
||||
er(c.GenBashCompletion(buf))
|
||||
output := buf.String()
|
||||
|
||||
if strings.Contains(output, flagName) {
|
||||
|
|
|
@ -401,7 +401,7 @@ func (c *Command) UsageString() string {
|
|||
c.outWriter = bb
|
||||
c.errWriter = bb
|
||||
|
||||
c.Usage()
|
||||
er(c.Usage())
|
||||
|
||||
// Setting things back to normal
|
||||
c.outWriter = tmpOutput
|
||||
|
@ -1032,10 +1032,10 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
|||
cmd, _, e := c.Root().Find(args)
|
||||
if cmd == nil || e != nil {
|
||||
c.Printf("Unknown help topic %#q\n", args)
|
||||
c.Root().Usage()
|
||||
er(c.Root().Usage())
|
||||
} else {
|
||||
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
|
||||
cmd.Help()
|
||||
er(cmd.Help())
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1632,7 +1632,7 @@ func (tc *calledAsTestcase) test(t *testing.T) {
|
|||
output := new(bytes.Buffer)
|
||||
parent.SetOutput(output)
|
||||
|
||||
parent.Execute()
|
||||
_ = parent.Execute()
|
||||
|
||||
if called == nil {
|
||||
if tc.call != "" {
|
||||
|
|
|
@ -109,7 +109,7 @@ func TestPowerShellCompletion(t *testing.T) {
|
|||
for _, tc := range tcs {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
tc.root.GenPowerShellCompletion(buf)
|
||||
er(tc.root.GenPowerShellCompletion(buf))
|
||||
output := buf.String()
|
||||
|
||||
for _, expectedExpression := range tc.expectedExpressions {
|
||||
|
|
|
@ -90,9 +90,9 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
Run: emptyRun,
|
||||
}
|
||||
r.Flags().StringVarP(&file, "config", "c", file, "config file")
|
||||
r.MarkFlagFilename("config")
|
||||
er(r.MarkFlagFilename("config"))
|
||||
r.Flags().String("output", "", "output file")
|
||||
r.MarkFlagFilename("output", "*.log", "*.txt")
|
||||
er(r.MarkFlagFilename("output", "*.log", "*.txt"))
|
||||
return r
|
||||
}(),
|
||||
expectedExpressions: []string{
|
||||
|
@ -161,8 +161,8 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
name: "argument completion for file with and without patterns",
|
||||
root: func() *Command {
|
||||
r := genTestCommand("root", true)
|
||||
r.MarkZshCompPositionalArgumentFile(1, "*.log")
|
||||
r.MarkZshCompPositionalArgumentFile(2)
|
||||
er(r.MarkZshCompPositionalArgumentFile(1, "*.log"))
|
||||
er(r.MarkZshCompPositionalArgumentFile(2))
|
||||
return r
|
||||
}(),
|
||||
expectedExpressions: []string{
|
||||
|
@ -173,7 +173,7 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
name: "argument zsh completion for words",
|
||||
root: func() *Command {
|
||||
r := genTestCommand("root", true)
|
||||
r.MarkZshCompPositionalArgumentWords(1, "word1", "word2")
|
||||
er(r.MarkZshCompPositionalArgumentWords(1, "word1", "word2"))
|
||||
return r
|
||||
}(),
|
||||
expectedExpressions: []string{
|
||||
|
@ -184,7 +184,7 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
name: "argument completion for words with spaces",
|
||||
root: func() *Command {
|
||||
r := genTestCommand("root", true)
|
||||
r.MarkZshCompPositionalArgumentWords(1, "single", "multiple words")
|
||||
er(r.MarkZshCompPositionalArgumentWords(1, "single", "multiple words"))
|
||||
return r
|
||||
}(),
|
||||
expectedExpressions: []string{
|
||||
|
@ -207,7 +207,7 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
root: func() *Command {
|
||||
r := genTestCommand("root", true)
|
||||
r.ValidArgs = []string{"word1", "word2"}
|
||||
r.MarkZshCompPositionalArgumentFile(2)
|
||||
er(r.MarkZshCompPositionalArgumentFile(2))
|
||||
return r
|
||||
}(),
|
||||
expectedExpressions: []string{
|
||||
|
@ -220,8 +220,8 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
r := genTestCommand("root", true)
|
||||
r.Flags().String("test", "", "test")
|
||||
r.PersistentFlags().String("ptest", "", "ptest")
|
||||
r.MarkFlagDirname("test")
|
||||
r.MarkPersistentFlagDirname("ptest")
|
||||
er(r.MarkFlagDirname("test"))
|
||||
er(r.MarkPersistentFlagDirname("ptest"))
|
||||
return r
|
||||
}(),
|
||||
expectedExpressions: []string{
|
||||
|
@ -237,7 +237,7 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
t.Skip(tc.skip)
|
||||
}
|
||||
tc.root.Root().SetArgs(tc.invocationArgs)
|
||||
tc.root.Execute()
|
||||
_ = tc.root.Execute()
|
||||
buf := new(bytes.Buffer)
|
||||
if err := tc.root.GenZshCompletion(buf); err != nil {
|
||||
t.Error(err)
|
||||
|
@ -311,11 +311,9 @@ func TestGenZshCompletionHidden(t *testing.T) {
|
|||
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
tc.root.Execute()
|
||||
_ = tc.root.Execute()
|
||||
buf := new(bytes.Buffer)
|
||||
if err := tc.root.GenZshCompletion(buf); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
er(tc.root.GenZshCompletion(buf))
|
||||
output := buf.String()
|
||||
|
||||
for _, expr := range tc.expectedExpressions {
|
||||
|
@ -457,7 +455,7 @@ func constructLargeCommandHierarchy() *Command {
|
|||
s1.AddCommand(s1_1, s1_2, s1_3)
|
||||
s2.AddCommand(s2_1, s2_2, s2_3, s2_4, s2_5)
|
||||
r.AddCommand(s1, s2, s3)
|
||||
r.Execute()
|
||||
er(r.Execute())
|
||||
return r
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue