Skip to content

Commit

Permalink
Handle unusual types in fishAddFileFlag [urfave#1156]
Browse files Browse the repository at this point in the history
  • Loading branch information
ErinCall committed Oct 22, 2020
1 parent c3263d4 commit 279c842
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string
func fishAddFileFlag(flag Flag, completion *strings.Builder) {
val := reflect.ValueOf(flag)
// if flag is a non-nil pointer to a struct...
if val.Kind() != reflect.Invalid && val.Elem().Kind() == reflect.Struct {
if val.Kind() == reflect.Ptr && val.Elem().Kind() == reflect.Struct {
field := val.Elem().FieldByName("TakesFile")
// if flag's underlying type has a bool field called TakesFile, whose value is true...
if field.Kind() == reflect.Bool && field.Bool() {
Expand Down
79 changes: 79 additions & 0 deletions fish_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"flag"
"strings"
"testing"
)

Expand All @@ -19,3 +21,80 @@ func TestFishCompletion(t *testing.T) {
expect(t, err, nil)
expectFileContent(t, "testdata/expected-fish-full.fish", res)
}

func TestFishAddFileFlag(t *testing.T) {
flags := []Flag{
&GenericFlag{
Name: "jen-and-eric",
TakesFile: false,
},
&PathFlag{
Name: "logfile",
TakesFile: false,
},
&BoolFlag{
Name: "dry-run",
},
&IntFlag{
Name: "depth",
},
}

for _, flag := range flags {
completion := &strings.Builder{}
fishAddFileFlag(flag, completion)
expect(t, completion.String(), " -f")
}

flags = []Flag{
&GenericFlag{
Name: "jen-and-eric",
TakesFile: true,
},
&PathFlag{
Name: "logfile",
TakesFile: true,
},
}

for _, flag := range flags {
completion := &strings.Builder{}
fishAddFileFlag(flag, completion)
t.Log(flag.Names())
expect(t, completion.String(), "")
}
}

type bareIntFlag int

func (bareIntFlag) Apply(*flag.FlagSet) error { return nil }
func (bareIntFlag) Names() []string { return []string{} }
func (bareIntFlag) IsSet() bool { return false }
func (bareIntFlag) String() string { return "bareIntFlag" }

type bareBoolFlag bool

func (*bareBoolFlag) Apply(*flag.FlagSet) error { return nil }
func (*bareBoolFlag) Names() []string { return []string{} }
func (*bareBoolFlag) IsSet() bool { return false }
func (*bareBoolFlag) String() string { return "bareBoolFlag" }

func TestFishAddFileFlagUnexpectedInput(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Errorf("test panicked: %v", err)
}
}()
bareInt := bareIntFlag(5)
bareBool := bareBoolFlag(false)
flags := []Flag{
bareInt,
&bareBool,
(*bareBoolFlag)(nil),
}
for _, flag := range flags {
completion := &strings.Builder{}
fishAddFileFlag(flag, completion)
expect(t, completion.String(), " -f")
}
}

0 comments on commit 279c842

Please sign in to comment.