ensure BindPFlag() detects a nil flag parameter before wrapping in pflagValue.

This commit is contained in:
Dan Rollo 2020-08-11 19:26:27 -04:00 committed by Márk Sági-Kazár
parent d9d7dcdc63
commit a0285163e1
2 changed files with 8 additions and 0 deletions

View file

@ -992,6 +992,9 @@ func (v *Viper) BindPFlags(flags *pflag.FlagSet) error {
func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
if flag == nil {
return fmt.Errorf("flag for %q is nil", key)
}
return v.BindFlagValue(key, pflagValue{flag})
}

View file

@ -970,6 +970,11 @@ func TestBindPFlag(t *testing.T) {
assert.Equal(t, "testing_mutate", Get("testvalue"))
}
func TestBindPFlagDetectNilFlag(t *testing.T) {
result := BindPFlag("testvalue", nil)
assert.Error(t, result)
}
func TestBindPFlagStringToString(t *testing.T) {
tests := []struct {
Expected map[string]string