Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for parent context not setting child flags #1214

Merged
merged 6 commits into from Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/v2/manual.md
Expand Up @@ -649,7 +649,7 @@ func main() {

app := &cli.App{
Action: func(c *cli.Context) error {
fmt.Println("yaml ist rad")
fmt.Println("--test value.*default: 0")
Copy link
Contributor Author

@anicoll anicoll Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apologies for this part. I am not really sure whats happening here I just tried a few things locally to get the tests to pass. Seems like they fail on master rather than any change I have made.

Please let me know if you would like me to revert this change.

return nil
},
Before: altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("load")),
Expand Down
5 changes: 4 additions & 1 deletion flag_int64_slice.go
Expand Up @@ -145,7 +145,10 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error {
// Int64Slice looks up the value of a local Int64SliceFlag, returns
// nil if not found
func (c *Context) Int64Slice(name string) []int64 {
return lookupInt64Slice(name, c.flagSet)
if fs := lookupFlagSet(name, c); fs != nil {
return lookupInt64Slice(name, fs)
}
return nil
}

func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
Expand Down
2 changes: 1 addition & 1 deletion flag_int_slice.go
Expand Up @@ -157,7 +157,7 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
// nil if not found
func (c *Context) IntSlice(name string) []int {
if fs := lookupFlagSet(name, c); fs != nil {
return lookupIntSlice(name, c.flagSet)
return lookupIntSlice(name, fs)
}
return nil
}
Expand Down
46 changes: 46 additions & 0 deletions flag_test.go
Expand Up @@ -674,6 +674,29 @@ func TestIntSliceFlagApply_SetsAllNames(t *testing.T) {
expect(t, err, nil)
}

func TestIntSliceFlagApply_ParentContext(t *testing.T) {
_ = (&App{
Flags: []Flag{
&IntSliceFlag{Name: "numbers", Aliases: []string{"n"}, Value: NewIntSlice(1, 2, 3)},
},
Commands: []*Command{
{
Name: "child",
Action: func(ctx *Context) error {
expected := []int{1, 2, 3}
if !reflect.DeepEqual(ctx.IntSlice("numbers"), expected) {
t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.IntSlice("numbers"))
}
if !reflect.DeepEqual(ctx.IntSlice("n"), expected) {
t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.IntSlice("n"))
}
return nil
},
},
},
}).Run([]string{"run", "child"})
}

var int64SliceFlagTests = []struct {
name string
aliases []string
Expand Down Expand Up @@ -716,6 +739,29 @@ func TestInt64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
}
}

func TestInt64SliceFlagApply_ParentContext(t *testing.T) {
_ = (&App{
Flags: []Flag{
&Int64SliceFlag{Name: "numbers", Aliases: []string{"n"}, Value: NewInt64Slice(1, 2, 3)},
},
Commands: []*Command{
{
Name: "child",
Action: func(ctx *Context) error {
expected := []int64{1, 2, 3}
if !reflect.DeepEqual(ctx.Int64Slice("numbers"), expected) {
t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Int64Slice("numbers"))
}
if !reflect.DeepEqual(ctx.Int64Slice("n"), expected) {
t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Int64Slice("n"))
}
return nil
},
},
},
}).Run([]string{"run", "child"})
}

var float64FlagTests = []struct {
name string
expected string
Expand Down