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

This commit is contained in:
Dan Rollo
2020-09-23 19:29:16 +02:00
committed by Márk Sági-Kazár
parent d9d7dcdc63
commit a0285163e1
2 changed files with 8 additions and 0 deletions
+3
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})
}
+5
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