always return error on Execute and do no print it by default downstream.

Signed-off-by: António Meireles <antonio.meireles@reformi.st>
This commit is contained in:
António Meireles 2015-09-10 10:02:21 +01:00
parent 4b86c66ef2
commit 2a7500618c
2 changed files with 14 additions and 14 deletions

View file

@ -481,8 +481,8 @@ func TestChildCommandFlags(t *testing.T) {
t.Errorf("invalid flag should generate error") t.Errorf("invalid flag should generate error")
} }
if !strings.Contains(r.Output, "unknown shorthand") { if !strings.Contains(r.Error.Error(), "unknown shorthand") {
t.Errorf("Wrong error message displayed, \n %s", r.Output) t.Errorf("Wrong error message displayed, \n %s", r.Error)
} }
if flagi2 != 99 { if flagi2 != 99 {
@ -500,8 +500,8 @@ func TestChildCommandFlags(t *testing.T) {
t.Errorf("invalid flag should generate error") t.Errorf("invalid flag should generate error")
} }
if !strings.Contains(r.Output, "unknown shorthand flag") { if !strings.Contains(r.Error.Error(), "unknown shorthand flag") {
t.Errorf("Wrong error message displayed, \n %s", r.Output) t.Errorf("Wrong error message displayed, \n %s", r.Error)
} }
// Testing with persistent flag overwritten by child // Testing with persistent flag overwritten by child
@ -522,8 +522,8 @@ func TestChildCommandFlags(t *testing.T) {
t.Errorf("invalid input should generate error") t.Errorf("invalid input should generate error")
} }
if !strings.Contains(r.Output, "invalid argument \"10E\" for i10E") { if !strings.Contains(r.Error.Error(), "invalid argument \"10E\" for i10E") {
t.Errorf("Wrong error message displayed, \n %s", r.Output) t.Errorf("Wrong error message displayed, \n %s", r.Error)
} }
} }
@ -540,10 +540,11 @@ func TestInvalidSubcommandFlags(t *testing.T) {
cmd.AddCommand(cmdTimes) cmd.AddCommand(cmdTimes)
result := simpleTester(cmd, "times --inttwo=2 --badflag=bar") result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")
// given that we are not checking here result.Error we check for
// stock usage message
checkResultContains(t, result, "cobra-test times [# times]")
checkResultContains(t, result, "unknown flag: --badflag") if strings.Contains(result.Error.Error(), "unknown flag: --inttwo") {
if strings.Contains(result.Output, "unknown flag: --inttwo") {
t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag") t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag")
} }
@ -787,7 +788,7 @@ func TestRootNoCommandHelp(t *testing.T) {
func TestRootUnknownCommand(t *testing.T) { func TestRootUnknownCommand(t *testing.T) {
r := noRRSetupTest("bogus") r := noRRSetupTest("bogus")
s := "Error: unknown command \"bogus\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n" s := "Run 'cobra-test --help' for usage.\n"
if r.Output != s { if r.Output != s {
t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output) t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
@ -823,8 +824,8 @@ func TestFlagsBeforeCommand(t *testing.T) {
// With parsing error properly reported // With parsing error properly reported
x = fullSetupTest("-i10E echo") x = fullSetupTest("-i10E echo")
if !strings.Contains(x.Output, "invalid argument \"10E\" for i10E") { if !strings.Contains(x.Error.Error(), "invalid argument \"10E\" for i10E") {
t.Errorf("Wrong error message displayed, \n %s", x.Output) t.Errorf("Wrong error message displayed, \n %s", x.Error)
} }
//With quotes //With quotes

View file

@ -576,7 +576,6 @@ func (c *Command) Execute() (err error) {
if cmd != nil { if cmd != nil {
c = cmd c = cmd
} }
c.Println("Error:", err.Error())
c.Printf("Run '%v --help' for usage.\n", c.CommandPath()) c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
return err return err
} }
@ -588,7 +587,7 @@ func (c *Command) Execute() (err error) {
return nil return nil
} }
c.Println(cmd.UsageString()) c.Println(cmd.UsageString())
c.Println("Error:", err.Error()) return err
} }
return return