forked from mirror/cobra
Proper handling of flag error messages. Fixing test that wasn't passing.
This commit is contained in:
parent
62fb674a93
commit
68f3c66d07
|
@ -215,18 +215,21 @@ func TestChildCommandFlags(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing with flag only existing on child
|
// Testing with flag only existing on child
|
||||||
|
buf2 := new(bytes.Buffer)
|
||||||
c = initialize()
|
c = initialize()
|
||||||
|
c.SetOutput(buf2)
|
||||||
cmdEcho.AddCommand(cmdTimes)
|
cmdEcho.AddCommand(cmdTimes)
|
||||||
c.AddCommand(cmdPrint, cmdEcho)
|
c.AddCommand(cmdPrint, cmdEcho)
|
||||||
c.SetArgs(strings.Split("echo -j 99 -i77 one two", " "))
|
c.SetArgs(strings.Split("echo -j 99 -i77 one two", " "))
|
||||||
err := c.Execute()
|
err := c.Execute()
|
||||||
_ = err
|
|
||||||
//c.DebugFlags()
|
|
||||||
|
|
||||||
// TODO figure out why this isn't passing
|
if err == nil {
|
||||||
//if err == nil {
|
t.Errorf("invalid flag should generate error")
|
||||||
//t.Errorf("invalid flag should generate error")
|
}
|
||||||
//}
|
|
||||||
|
if !strings.Contains(buf2.String(), "intone=123") {
|
||||||
|
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -336,10 +336,11 @@ func (c *Command) ParseFlags(args []string) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if c.flagErrorBuf != nil {
|
// The upstream library adds spaces to the error
|
||||||
//fmt.Println(c.flagErrorBuf.String())
|
// response regardless of success.
|
||||||
return nil
|
// Handling it here until fixing upstream
|
||||||
//return fmt.Errorf("%s", c.flagErrorBuf.String())
|
if len(strings.TrimSpace(c.flagErrorBuf.String())) > 1 {
|
||||||
|
return fmt.Errorf("%s", c.flagErrorBuf.String())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue