Merge pull request #192 from apriendeau/executeC
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Creates the ExecuteC function while maintaining Execute signature
authored by Eric Paris · committed by Eric Paris · parents 9324f36, 3f60599 · Browse files · 2 files·+58 added−7 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+58 added−7 removed
- other:+58 added−7 removed
Provenance
- Commit
cb63a06- Tree
824e553- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
9324f36- Parent 2
3f60599
2 files changed+58 added−7 removed
modified:cobra_test.go+47 added
246 unmodified lines247 248 return resulter{err, output, c}249 }250 251+func simpleTesterC(c *Command, input string) resulter {252+ buf := new(bytes.Buffer)253+ // Testing flag with invalid input254+ c.SetOutput(buf)255+ c.SetArgs(strings.Split(input, " "))256+257+ cmd, err := c.ExecuteC()258+ output := buf.String()259+260+ return resulter{err, output, cmd}261+}262+263 func fullTester(c *Command, input string) resulter {264 buf := new(bytes.Buffer)265 // Testing flag with invalid input266 c.SetOutput(buf)305 unmodified lines572 }573 574 }575 576+func TestSubcommandExecuteC(t *testing.T) {577+ cmd := initializeWithRootCmd()578+ double := &Command{579+ Use: "double message",580+ Run: func(c *Command, args []string) {581+ msg := strings.Join(args, " ")582+ c.Println(msg, msg)583+ },584+ }585+586+ echo := &Command{587+ Use: "echo message",588+ Run: func(c *Command, args []string) {589+ msg := strings.Join(args, " ")590+ c.Println(msg, msg)591+ },592+ }593+594+ cmd.AddCommand(double, echo)595+596+ result := simpleTesterC(cmd, "double hello world")597+ checkResultContains(t, result, "hello world hello world")598+599+ if result.Command.Name() != "double" {600+ t.Errorf("invalid cmd returned from ExecuteC: should be 'double' but got %s", result.Command.Name())601+ }602+603+ result = simpleTesterC(cmd, "echo msg to be echoed")604+ checkResultContains(t, result, "msg to be echoed")605+606+ if result.Command.Name() != "echo" {607+ t.Errorf("invalid cmd returned from ExecuteC: should be 'echo' but got %s", result.Command.Name())608+ }609+}610+611 func TestSubcommandArgEvaluation(t *testing.T) {612 cmd := initializeWithRootCmd()613 614 first := &Command{549 unmodified linesmodified:command.go+11 added-7 removed
612 unmodified lines613 614 // Call execute to use the args (os.Args[1:] by default)615 // and run through the command tree finding appropriate matches616 // for commands and then corresponding flags.617-func (c *Command) Execute() (err error) {617+func (c *Command) Execute() error {618+ _, err := c.ExecuteC()619+ return err620+}621 622+func (c *Command) ExecuteC() (cmd *Command, err error) {623+624 // Regardless of what command execute is called on, run on Root only625 if c.HasParent() {621- return c.Root().Execute()626+ return c.Root().ExecuteC()627 }628 629 if EnableWindowsMouseTrap && runtime.GOOS == "windows" {630 if mousetrap.StartedByExplorer() {25 unmodified lines656 if !c.SilenceErrors {657 c.Println("Error:", err.Error())658 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())659 }655- return err660+ return c, err661 }657-662 err = cmd.execute(flags)663 if err != nil {664 // If root command has SilentErrors flagged,665 // all subcommands should respect it666 if !cmd.SilenceErrors && !c.SilenceErrors {667 if err == flag.ErrHelp {668 cmd.HelpFunc()(cmd, args)665- return nil669+ return cmd, nil670 }671 c.Println("Error:", err.Error())672 }673 1 unmodified line675 // all subcommands should respect it676 if !cmd.SilenceUsage && !c.SilenceUsage {677 c.Println(cmd.UsageString())678 }675- return err679+ return cmd, err680 }677- return681+ return cmd, nil682 }683 684 func (c *Command) initHelpFlag() {685 if c.Flags().Lookup("help") == nil {515 unmodified lines