Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #76 from mitchellh/b-default
Browse files Browse the repository at this point in the history
Ignore default command for autocomplete init
  • Loading branch information
mitchellh authored Apr 14, 2018
2 parents c54c85e + 897ee48 commit c48282d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ func (c *CLI) initAutocomplete() {
func (c *CLI) initAutocompleteSub(prefix string) complete.Command {
var cmd complete.Command
walkFn := func(k string, raw interface{}) bool {
// Ignore the empty key which can be present for default commands.
if k == "" {
return false
}

// Keep track of the full key so that we can nest further if necessary
fullKey := k

Expand Down
26 changes: 26 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,32 @@ func TestCLIRun_default(t *testing.T) {
}
}

// GH-74: When using NewCLI with a default command only, Run would
// stack overflow and crash.
func TestCLIRun_defaultFromNew(t *testing.T) {
commandBar := new(MockCommand)

cli := NewCLI("test", "0.1.0")
cli.Commands = map[string]CommandFactory{
"": func() (Command, error) {
return commandBar, nil
},
}

exitCode, err := cli.Run()
if err != nil {
t.Fatalf("err: %s", err)
}

if exitCode != commandBar.RunResult {
t.Fatalf("bad: %d", exitCode)
}

if !commandBar.RunCalled {
t.Fatalf("run should be called")
}
}

func TestCLIRun_helpNested(t *testing.T) {
helpCalled := false
buf := new(bytes.Buffer)
Expand Down

0 comments on commit c48282d

Please sign in to comment.