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:(issue_1197) Set destination field from altsrc for slice flags #1495

Merged
merged 2 commits into from Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions altsrc/flag.go
Expand Up @@ -110,6 +110,9 @@ func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSour
}
underlyingFlag.Value = &sliceValue
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
}
}
return nil
}
Expand Down Expand Up @@ -138,6 +141,9 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
}
underlyingFlag.Value = &sliceValue
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
}
}
return nil
}
Expand Down
64 changes: 58 additions & 6 deletions altsrc/flag_test.go
Expand Up @@ -100,39 +100,61 @@ func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
}

func TestStringSliceApplyInputSourceValue_Alias(t *testing.T) {
dest := cli.NewStringSlice()
tis := testApplyInputSource{
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Aliases: []string{"test_alias"}}),
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
FlagName: "test_alias",
MapValue: []interface{}{"hello", "world"},
}
c := runTest(t, tis)
expect(t, c.StringSlice("test_alias"), []string{"hello", "world"})
expect(t, dest.Value(), []string{"hello", "world"})

// reset dest
dest = cli.NewStringSlice()
tis = testApplyInputSource{
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
FlagName: "test_alias",
MapValue: []interface{}{"hello", "world"},
}
c = runRacyTest(t, tis)
refute(t, c.StringSlice("test_alias"), []string{"hello", "world"})
refute(t, dest.Value(), []string{"hello", "world"})
}

func TestStringSliceApplyInputSourceValue(t *testing.T) {
dest := cli.NewStringSlice()
tis := testApplyInputSource{
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{"hello", "world"},
}
c := runTest(t, tis)
expect(t, c.StringSlice("test"), []string{"hello", "world"})
expect(t, dest.Value(), []string{"hello", "world"})

// reset dest
dest = cli.NewStringSlice()
tis = testApplyInputSource{
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{"hello", "world"},
}
c = runRacyTest(t, tis)
refute(t, c.StringSlice("test"), []string{"hello", "world"})
refute(t, dest.Value(), []string{"hello", "world"})
}

func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
dest := cli.NewStringSlice()
c := runTest(t, testApplyInputSource{
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{"hello", "world"},
ContextValueString: "ohno",
})
expect(t, c.StringSlice("test"), []string{"ohno"})
expect(t, dest.Value(), []string{"ohno"})
}

func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
Expand All @@ -151,43 +173,73 @@ func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
}

func TestIntSliceApplyInputSourceValue_Alias(t *testing.T) {
dest := cli.NewIntSlice()
tis := testApplyInputSource{
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Aliases: []string{"test_alias"}}),
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
FlagName: "test_alias",
MapValue: []interface{}{1, 2},
}
c := runTest(t, tis)
expect(t, c.IntSlice("test_alias"), []int{1, 2})
expect(t, dest.Value(), []int{1, 2})

dest = cli.NewIntSlice()
tis = testApplyInputSource{
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Aliases: []string{"test_alias"}, Destination: dest}),
FlagName: "test_alias",
MapValue: []interface{}{1, 2},
}
c = runRacyTest(t, tis)
refute(t, c.IntSlice("test_alias"), []int{1, 2})
refute(t, dest.Value(), []int{1, 2})
}

func TestIntSliceApplyInputSourceValue(t *testing.T) {
dest := cli.NewIntSlice()
tis := testApplyInputSource{
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{1, 2},
}
c := runTest(t, tis)
expect(t, c.IntSlice("test"), []int{1, 2})
expect(t, dest.Value(), []int{1, 2})

// reset dest
dest = cli.NewIntSlice()
tis = testApplyInputSource{
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{1, 2},
}
c = runRacyTest(t, tis)
refute(t, c.IntSlice("test"), []int{1, 2})
refute(t, dest.Value(), []int{1, 2})
}

func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
dest := cli.NewIntSlice()
tis := testApplyInputSource{
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{1, 2},
ContextValueString: "3",
}
c := runTest(t, tis)
expect(t, c.IntSlice("test"), []int{3})
expect(t, dest.Value(), []int{3})

// reset dest
dest = cli.NewIntSlice()
tis = testApplyInputSource{
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{1, 2},
ContextValueString: "3",
}
c = runRacyTest(t, tis)
refute(t, c.IntSlice("test"), []int{3})
refute(t, dest.Value(), []int{3})
}

func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions altsrc/yaml_file_loader.go
Expand Up @@ -33,11 +33,9 @@ func NewYamlSourceFromFile(file string) (InputSourceContext, error) {
// NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a provided flag name and source context.
func NewYamlSourceFromFlagFunc(flagFileName string) func(cCtx *cli.Context) (InputSourceContext, error) {
return func(cCtx *cli.Context) (InputSourceContext, error) {
if cCtx.IsSet(flagFileName) {
filePath := cCtx.String(flagFileName)
if filePath := cCtx.String(flagFileName); filePath != "" {
return NewYamlSourceFromFile(filePath)
}

return defaultInputSource()
}
}
Expand Down