Prevent panic on flagSet access from custom BashComplete

Fixes #944
This commit is contained in:
Roberto Hidalgo
2019-11-25 14:25:11 -05:00
parent 4805bd168c
commit 90a62d7b0c
2 changed files with 51 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package cli
import (
"bytes"
"errors"
"flag"
"fmt"
@@ -329,3 +330,49 @@ func TestCommandSkipFlagParsing(t *testing.T) {
expect(t, args, c.expectedArgs)
}
}
func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) {
cases := []struct {
testArgs args
expectedOut string
}{
{testArgs: args{"--undefined"}, expectedOut: "found 0 args"},
{testArgs: args{"--number"}, expectedOut: "found 0 args"},
{testArgs: args{"--number", "fourty-two"}, expectedOut: "found 0 args"},
{testArgs: args{"--number", "42"}, expectedOut: "found 0 args"},
{testArgs: args{"--number", "42", "newArg"}, expectedOut: "found 1 args"},
}
for _, c := range cases {
var outputBuffer bytes.Buffer
app := &App{
Writer: &outputBuffer,
EnableBashCompletion: true,
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Flags: []Flag{
&IntFlag{
Name: "number",
Usage: "A number to parse",
},
},
BashComplete: func(c *Context) {
fmt.Fprintf(c.App.Writer, "found %d args", c.NArg())
},
},
},
}
osArgs := args{"foo", "bar"}
osArgs = append(osArgs, c.testArgs...)
osArgs = append(osArgs, "--generate-bash-completion")
err := app.Run(osArgs)
stdout := outputBuffer.String()
expect(t, err, nil)
expect(t, stdout, c.expectedOut)
}
}