fix #1239: slice flag value don't append to default values from ENV or file (#1240)

* fix #1239: slice flag value don't append to default values from ENV or file

* remove test code
This commit is contained in:
Ally Dale 2021-07-07 08:33:01 +08:00 committed by GitHub
parent 6373f5bf65
commit 58d113dd73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 2 deletions

View File

@ -144,6 +144,9 @@ func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error {
} }
} }
// Set this to false so that we reset the slice if we then set values from
// flags that have already been set by the environment.
f.Value.hasBeenSet = false
f.HasBeenSet = true f.HasBeenSet = true
} }
} }

View File

@ -144,6 +144,9 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error {
} }
} }
// Set this to false so that we reset the slice if we then set values from
// flags that have already been set by the environment.
f.Value.hasBeenSet = false
f.HasBeenSet = true f.HasBeenSet = true
} }

View File

@ -155,6 +155,9 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
} }
} }
// Set this to false so that we reset the slice if we then set values from
// flags that have already been set by the environment.
f.Value.hasBeenSet = false
f.HasBeenSet = true f.HasBeenSet = true
} }

View File

@ -52,15 +52,21 @@ func TestBoolFlagApply_SetsAllNames(t *testing.T) {
} }
func TestFlagsFromEnv(t *testing.T) { func TestFlagsFromEnv(t *testing.T) {
newSetFloat64Slice := func(defaults ...float64) Float64Slice {
s := NewFloat64Slice(defaults...)
s.hasBeenSet = false
return *s
}
newSetIntSlice := func(defaults ...int) IntSlice { newSetIntSlice := func(defaults ...int) IntSlice {
s := NewIntSlice(defaults...) s := NewIntSlice(defaults...)
s.hasBeenSet = true s.hasBeenSet = false
return *s return *s
} }
newSetInt64Slice := func(defaults ...int64) Int64Slice { newSetInt64Slice := func(defaults ...int64) Int64Slice {
s := NewInt64Slice(defaults...) s := NewInt64Slice(defaults...)
s.hasBeenSet = true s.hasBeenSet = false
return *s return *s
} }
@ -96,6 +102,9 @@ func TestFlagsFromEnv(t *testing.T) {
{"1.2", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2" as int value for flag seconds: .*`}, {"1.2", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2" as int value for flag seconds: .*`},
{"foobar", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int value for flag seconds: .*`}, {"foobar", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int value for flag seconds: .*`},
{"1.0,2", newSetFloat64Slice(1, 2), &Float64SliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""},
{"foobar", newSetFloat64Slice(), &Float64SliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "\[\]float64{}" as float64 slice value for flag seconds: .*`},
{"1,2", newSetIntSlice(1, 2), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""}, {"1,2", newSetIntSlice(1, 2), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""},
{"1.2,2", newSetIntSlice(), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2,2" as int slice value for flag seconds: .*`}, {"1.2,2", newSetIntSlice(), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2,2" as int slice value for flag seconds: .*`},
{"foobar", newSetIntSlice(), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int slice value for flag seconds: .*`}, {"foobar", newSetIntSlice(), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int slice value for flag seconds: .*`},