Test behavior for subcommand with same name as root command
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.
This commit is contained in:
parent
4c29b190e0
commit
667c348dbd
@ -47,6 +47,12 @@ var cmdRootNoRun = &Command{
|
||||
Long: "The root description for help",
|
||||
}
|
||||
|
||||
var cmdRootSameName = &Command{
|
||||
Use: "print",
|
||||
Short: "Root with the same name as a subcommand",
|
||||
Long: "The root description for help",
|
||||
}
|
||||
|
||||
var cmdRootWithRun = &Command{
|
||||
Use: "cobra-test",
|
||||
Short: "The root can run it's own function",
|
||||
@ -65,6 +71,7 @@ func flagInit() {
|
||||
cmdPrint.ResetFlags()
|
||||
cmdTimes.ResetFlags()
|
||||
cmdRootNoRun.ResetFlags()
|
||||
cmdRootSameName.ResetFlags()
|
||||
cmdRootWithRun.ResetFlags()
|
||||
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
|
||||
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
|
||||
@ -82,6 +89,7 @@ func commandInit() {
|
||||
cmdPrint.ResetCommands()
|
||||
cmdTimes.ResetCommands()
|
||||
cmdRootNoRun.ResetCommands()
|
||||
cmdRootSameName.ResetCommands()
|
||||
cmdRootWithRun.ResetCommands()
|
||||
}
|
||||
|
||||
@ -93,6 +101,14 @@ func initialize() *Command {
|
||||
return c
|
||||
}
|
||||
|
||||
func initializeWithSameName() *Command {
|
||||
tt, tp, te = nil, nil, nil
|
||||
var c = cmdRootSameName
|
||||
flagInit()
|
||||
commandInit()
|
||||
return c
|
||||
}
|
||||
|
||||
func initializeWithRootCmd() *Command {
|
||||
cmdRootWithRun.ResetCommands()
|
||||
tt, tp, te, rootcalled = nil, nil, nil, false
|
||||
@ -156,6 +172,40 @@ func TestChildCommandPrefix(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChildSameName(t *testing.T) {
|
||||
c := initializeWithSameName()
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
c.SetArgs(strings.Split("print one two", " "))
|
||||
c.Execute()
|
||||
|
||||
if te != nil || tt != nil {
|
||||
t.Error("Wrong command called")
|
||||
}
|
||||
if tp == nil {
|
||||
t.Error("Wrong command called")
|
||||
}
|
||||
if strings.Join(tp, " ") != "one two" {
|
||||
t.Error("Command didn't parse correctly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChildSameNamePrefix(t *testing.T) {
|
||||
c := initializeWithSameName()
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
c.SetArgs(strings.Split("pr one two", " "))
|
||||
c.Execute()
|
||||
|
||||
if te != nil || tt != nil {
|
||||
t.Error("Wrong command called")
|
||||
}
|
||||
if tp == nil {
|
||||
t.Error("Wrong command called")
|
||||
}
|
||||
if strings.Join(tp, " ") != "one two" {
|
||||
t.Error("Command didn't parse correctly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagLong(t *testing.T) {
|
||||
c := initialize()
|
||||
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
||||
|
@ -260,7 +260,7 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
|
||||
commandFound, a := innerfind(c, arrs)
|
||||
|
||||
// if commander returned and not appropriately matched return nil & error
|
||||
if commandFound.Name() == c.Name() && commandFound.Name() != arrs[0] {
|
||||
if commandFound.Name() == c.Name() && !strings.HasPrefix(commandFound.Name(), arrs[0]) {
|
||||
return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0])
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user