Skip to content

Commit

Permalink
fix #1239: slice flag value don't append to default values from ENV o…
Browse files Browse the repository at this point in the history
…r file (#1240)

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

* remove test code
  • Loading branch information
vipally committed Jul 7, 2021
1 parent 6373f5b commit 58d113d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions flag_float64_slice.go
Expand Up @@ -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
}
}
Expand Down
3 changes: 3 additions & 0 deletions flag_int64_slice.go
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions flag_int_slice.go
Expand Up @@ -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
}

Expand Down
13 changes: 11 additions & 2 deletions flag_test.go
Expand Up @@ -52,15 +52,21 @@ func TestBoolFlagApply_SetsAllNames(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 {
s := NewIntSlice(defaults...)
s.hasBeenSet = true
s.hasBeenSet = false
return *s
}

newSetInt64Slice := func(defaults ...int64) Int64Slice {
s := NewInt64Slice(defaults...)
s.hasBeenSet = true
s.hasBeenSet = false
return *s
}

Expand Down Expand Up @@ -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: .*`},
{"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,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: .*`},
Expand Down

0 comments on commit 58d113d

Please sign in to comment.