Skip to content
Myr

[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

2 files+46 added−22 removed

modified:cobra_test.go+32 added-14 removed
215 unmodified lines
216217 return fullTester(c, input)218}219220func noRRSetupTestSilenced(input string) resulter {221 c := initialize()222 c.SilenceErrors = true223 return fullTester(c, input)224}225226func noRRSetupTest(input string) resulter {227 c := initialize()228229 return fullTester(c, input)
263 unmodified lines
493 if r.Error == nil {494 t.Errorf("invalid flag should generate error")495 }496491 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 }500501 if flagi2 != 99 {502 t.Errorf("flag value should be 99, %d given", flagi2)
8 unmodified lines
511512 if r.Error == nil {513 t.Errorf("invalid flag should generate error")514 }509510 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 }518519 // Testing with persistent flag overwritten by child520 noRRSetupTest("echo times --strtwo=child one two")
11 unmodified lines
532533 if r.Error == nil {534 t.Errorf("invalid input should generate error")535 }531532 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}540541func TestTrailingCommandFlags(t *testing.T) {
8 unmodified lines
550 cmd := initializeWithRootCmd()551 cmd.AddCommand(cmdTimes)552553 result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")550551 checkResultContains(t, result, "unknown flag: --badflag")552553 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 }560561}
250 unmodified lines
812 t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)813 }814}815816func TestRootUnknownCommandSilenced(t *testing.T) {817 r := noRRSetupTestSilenced("bogus")818 s := "Run 'cobra-test --help' for usage.\n"819820 if r.Output != "" {821 t.Errorf("Unexpected response.\nExpecting to be: \n\"\"\n Got:\n %q\n", s, r.Output)822 }823824 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}829830func 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 lines
889 }890891 // 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 }896897 //With quotes898 x = fullSetupTest("-s=\"walking\" echo")
178 unmodified lines
modified:command.go+14 added-8 removed
59 unmodified lines
60 // 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 lines
627 // 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 }637638 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 }643650 return651}652653func (c *Command) initHelpFlag() {
516 unmodified lines