[silence-errors]: -m adds a way for errors to silenced down the stack
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Austin Riendeau · committed by Austin Riendeau · parent 8b2293c · Browse files · 2 files·+46 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+46 added−22 removed
- other:+46 added−22 removed
Provenance
- Commit
f6e1a2d- Tree
ae99fe8- Author
Austin Riendeau <austin@apriendeau.com>- Committer
Austin Riendeau <austin@apriendeau.com>- Parent
8b2293c
2 files changed+46 added−22 removed
modified:cobra_test.go+32 added-14 removed
215 unmodified lines216 217 return fullTester(c, input)218 }219 220+func noRRSetupTestSilenced(input string) resulter {221+ c := initialize()222+ c.SilenceErrors = true223+ return fullTester(c, input)224+}225+226 func noRRSetupTest(input string) resulter {227 c := initialize()228 229 return fullTester(c, input)263 unmodified lines493 if r.Error == nil {494 t.Errorf("invalid flag should generate error")495 }496 491- if !strings.Contains(r.Output, "unknown shorthand") {492- t.Errorf("Wrong error message displayed, \n %s", r.Output)497+ if !strings.Contains(r.Error.Error(), "unknown shorthand") {498+ t.Errorf("Wrong error message displayed, \n %s", r.Error)499 }500 501 if flagi2 != 99 {502 t.Errorf("flag value should be 99, %d given", flagi2)8 unmodified lines511 512 if r.Error == nil {513 t.Errorf("invalid flag should generate error")514 }509-510- if !strings.Contains(r.Output, "unknown shorthand flag") {511- t.Errorf("Wrong error message displayed, \n %s", r.Output)515+ if !strings.Contains(r.Error.Error(), "unknown shorthand flag") {516+ t.Errorf("Wrong error message displayed, \n %s", r.Error)517 }518 519 // Testing with persistent flag overwritten by child520 noRRSetupTest("echo times --strtwo=child one two")11 unmodified lines532 533 if r.Error == nil {534 t.Errorf("invalid input should generate error")535 }531-532- if !strings.Contains(r.Output, "invalid argument \"10E\" for i10E") {533- t.Errorf("Wrong error message displayed, \n %s", r.Output)536+ if !strings.Contains(r.Error.Error(), "invalid argument \"10E\" for i10E") {537+ t.Errorf("Wrong error message displayed, \n %s", r.Error)538 }539 }540 541 func TestTrailingCommandFlags(t *testing.T) {8 unmodified lines550 cmd := initializeWithRootCmd()551 cmd.AddCommand(cmdTimes)552 553 result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")550-551- checkResultContains(t, result, "unknown flag: --badflag")552-553- if strings.Contains(result.Output, "unknown flag: --inttwo") {554+ // given that we are not checking here result.Error we check for555+ // stock usage message556+ checkResultContains(t, result, "cobra-test times [# times]")557+ if strings.Contains(result.Error.Error(), "unknown flag: --inttwo") {558 t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag")559 }560 561 }250 unmodified lines812 t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)813 }814 }815 816+func TestRootUnknownCommandSilenced(t *testing.T) {817+ r := noRRSetupTestSilenced("bogus")818+ s := "Run 'cobra-test --help' for usage.\n"819+820+ if r.Output != "" {821+ t.Errorf("Unexpected response.\nExpecting to be: \n\"\"\n Got:\n %q\n", s, r.Output)822+ }823+824+ r = noRRSetupTestSilenced("--strtwo=a bogus")825+ if r.Output != "" {826+ t.Errorf("Unexpected response.\nExpecting to be:\n\"\"\nGot:\n %q\n", s, r.Output)827+ }828+}829+830 func TestRootSuggestions(t *testing.T) {831 outputWithSuggestions := "Error: unknown command \"%s\" for \"cobra-test\"\n\nDid you mean this?\n\t%s\n\nRun 'cobra-test --help' for usage.\n"832 outputWithoutSuggestions := "Error: unknown command \"%s\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n"833 55 unmodified lines889 }890 891 // With parsing error properly reported892 x = fullSetupTest("-i10E echo")875- if !strings.Contains(x.Output, "invalid argument \"10E\" for i10E") {876- t.Errorf("Wrong error message displayed, \n %s", x.Output)893+ if !strings.Contains(x.Error.Error(), "invalid argument \"10E\" for i10E") {894+ t.Errorf("Wrong error message displayed, \n %s", x.Error)895 }896 897 //With quotes898 x = fullSetupTest("-s=\"walking\" echo")178 unmodified linesmodified:command.go+14 added-8 removed
59 unmodified lines60 // Set of flags childrens of this command will inherit61 pflags *flag.FlagSet62 // Flags that are declared specifically by this command (not inherited).63 lflags *flag.FlagSet64+ // SilenceErrors is an option to quiet errors down stream65+ SilenceErrors bool66 // The *Run functions are executed in the following order:67 // * PersistentPreRun()68 // * PreRun()69 // * Run()557 unmodified lines627 // If found parse to a subcommand and then failed, talk about the subcommand628 if cmd != nil {629 c = cmd630 }631+ if !c.SilenceErrors {629- c.Println("Error:", err.Error())630- c.Printf("Run '%v --help' for usage.\n", c.CommandPath())632+ c.Println("Error:", err.Error())633+ c.Printf("Run '%v --help' for usage.\n", c.CommandPath())634+ }635 return err636 }637 638 err = cmd.execute(flags)639 if err != nil {640+ if !cmd.SilenceErrors {636- if err == flag.ErrHelp {637- cmd.HelpFunc()(cmd, args)638- return nil641+ if err == flag.ErrHelp {642+ cmd.HelpFunc()(cmd, args)643+ return nil644+ }645+ c.Println(cmd.UsageString())646+ c.Println("Error:", err.Error())647 }640- c.Println(cmd.UsageString())641- c.Println("Error:", err.Error())648+ return err649 }643-650 return651 }652 653 func (c *Command) initHelpFlag() {516 unmodified lines