Skip to content
Myr

Test behavior for subcommand with same name as root command

Before adoption: this commit came in with the history the project adopted, so no landing records it.

If, for some reason, you have an application with some name "foo", and your app has a subcommand "foo", cobra should behave properly when you call "foo foo", and it should also behave if you call "foo f". These changes verify both of these cases and ensure cobra responds properly.

authored by tummychow · committed by tummychow · parent 4c29b19 · Browse files · 2 files+51 added−1 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+51 added−1 removed

  • other:+51 added−1 removed

2 files+51 added−1 removed

modified:cobra_test.go+50 added
45 unmodified lines
46 Short: "The root can run it's own function",47 Long: "The root description for help",48}4950var cmdRootSameName = &Command{51 Use: "print",52 Short: "Root with the same name as a subcommand",53 Long: "The root description for help",54}5556var cmdRootWithRun = &Command{57 Use: "cobra-test",58 Short: "The root can run it's own function",59 Long: "The root description for help",
10 unmodified lines
70 cmdEcho.ResetFlags()71 cmdPrint.ResetFlags()72 cmdTimes.ResetFlags()73 cmdRootNoRun.ResetFlags()74 cmdRootSameName.ResetFlags()75 cmdRootWithRun.ResetFlags()76 cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")77 cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")78 cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
9 unmodified lines
88 cmdEcho.ResetCommands()89 cmdPrint.ResetCommands()90 cmdTimes.ResetCommands()91 cmdRootNoRun.ResetCommands()92 cmdRootSameName.ResetCommands()93 cmdRootWithRun.ResetCommands()94}9596func initialize() *Command {
3 unmodified lines
100 commandInit()101 return c102}103104func initializeWithSameName() *Command {105 tt, tp, te = nil, nil, nil106 var c = cmdRootSameName107 flagInit()108 commandInit()109 return c110}111112func initializeWithRootCmd() *Command {113 cmdRootWithRun.ResetCommands()114 tt, tp, te, rootcalled = nil, nil, nil, false115 flagInit()
55 unmodified lines
171 t.Error("Command didn't parse correctly")172 }173}174175func TestChildSameName(t *testing.T) {176 c := initializeWithSameName()177 c.AddCommand(cmdPrint, cmdEcho)178 c.SetArgs(strings.Split("print one two", " "))179 c.Execute()180181 if te != nil || tt != nil {182 t.Error("Wrong command called")183 }184 if tp == nil {185 t.Error("Wrong command called")186 }187 if strings.Join(tp, " ") != "one two" {188 t.Error("Command didn't parse correctly")189 }190}191192func TestChildSameNamePrefix(t *testing.T) {193 c := initializeWithSameName()194 c.AddCommand(cmdPrint, cmdEcho)195 c.SetArgs(strings.Split("pr one two", " "))196 c.Execute()197198 if te != nil || tt != nil {199 t.Error("Wrong command called")200 }201 if tp == nil {202 t.Error("Wrong command called")203 }204 if strings.Join(tp, " ") != "one two" {205 t.Error("Command didn't parse correctly")206 }207}208209func TestFlagLong(t *testing.T) {210 c := initialize()211 c.AddCommand(cmdPrint, cmdEcho, cmdTimes)212 c.SetArgs(strings.Split("echo --intone=13 something here", " "))
219 unmodified lines
modified:command.go+1 added-1 removed
258 unmodified lines
259260 commandFound, a := innerfind(c, arrs)261262 // if commander returned and not appropriately matched return nil & error263 if commandFound.Name() == c.Name() && commandFound.Name() != arrs[0] {263 if commandFound.Name() == c.Name() && !strings.HasPrefix(commandFound.Name(), arrs[0]) {264 return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0])265 }266267 return commandFound, a, nil
385 unmodified lines