Skip to content

Commit

Permalink
fix urfave#1239: slice flag value don't append to default values from…
Browse files Browse the repository at this point in the history
… ENV or file
  • Loading branch information
vipally committed Feb 8, 2021
1 parent 92d7784 commit 0a9e9b7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions flag_float64_slice.go
Expand Up @@ -129,6 +129,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 @@ -129,6 +129,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 @@ -140,6 +140,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
14 changes: 12 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 Expand Up @@ -143,6 +152,7 @@ func TestFlagsFromEnv(t *testing.T) {
t.Errorf("expected error to match %q, got none", test.errRegexp)
} else {
if matched, _ := regexp.MatchString(test.errRegexp, err.Error()); !matched {
fmt.Printf("%s\n%s\n", test.errRegexp, err.Error())
t.Errorf("expected error to match %q, got error %s", test.errRegexp, err)
}
}
Expand Down

0 comments on commit 0a9e9b7

Please sign in to comment.