Skip to content

Commit

Permalink
Add subsub command test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Jun 5, 2023
1 parent b68cbf6 commit 0b3304b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 45 deletions.
40 changes: 0 additions & 40 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,46 +1391,6 @@ func TestApp_BeforeAfterFuncShellCompletion(t *testing.T) {
}
}

func TestApp_SubcommandShellCompletion(t *testing.T) {

var w bytes.Buffer

app := &App{
Name: "command",
EnableBashCompletion: true,
Commands: []*Command{
{
Name: "subcmd1",
Subcommands: []*Command{
{
Name: "subcmd2",
Subcommands: []*Command{
{
Name: "subcmd3",
},
},
},
},
},
},
Flags: []Flag{
&StringFlag{Name: "opt"},
},
Writer: &w,
}

// run with the Before() func succeeding
err := app.Run([]string{"command", "subcmd1", "subcmd2", "--generate-bash-completion"})

if err != nil {
t.Error(err)
}

if !strings.Contains(w.String(), "subcmd3\n") {
t.Errorf("Expected subcmd3 got %s", w.String())
}
}

func TestApp_AfterFunc(t *testing.T) {
counts := &opCounts{}
afterError := fmt.Errorf("fail")
Expand Down
2 changes: 0 additions & 2 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ func DefaultCompleteWithFlags(cmd *Command) func(cCtx *Context) {
if cmd != nil {
if cCtx.NArg() > 1 {
lastArg = cCtx.Args().Get(cCtx.NArg() - 1)
} else {
lastArg = ""
}
}

Expand Down
31 changes: 28 additions & 3 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
Commands: []*Command{
{
Name: "putz",
HideHelp: true,
Name: "putz",
Subcommands: []*Command{
{Name: "futz"},
},
Expand All @@ -1234,7 +1233,33 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--happiness", "putz", "--generate-bash-completion"},
expected: "futz\n",
expected: "futz\nhelp\nh\n",
},
{
name: "typical-subcommand-subcommand-suggestion",
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
Commands: []*Command{
{
Name: "putz",
Subcommands: []*Command{
{
Name: "futz",
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
},
},
},
},
argv: []string{"cmd", "--happiness", "putz", "futz", "-e", "--generate-bash-completion"},
expected: "--excitement\n",
},
{
name: "autocomplete-with-spaces",
Expand Down

0 comments on commit 0b3304b

Please sign in to comment.