2015-12-10 21:14:17 +03:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBindFlagValueSet(t *testing.T) {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
|
2020-09-11 18:48:38 +03:00
|
|
|
testValues := map[string]*string{
|
2015-12-10 21:14:17 +03:00
|
|
|
"host": nil,
|
|
|
|
"port": nil,
|
|
|
|
"endpoint": nil,
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:48:38 +03:00
|
|
|
mutatedTestValues := map[string]string{
|
2015-12-10 21:14:17 +03:00
|
|
|
"host": "localhost",
|
|
|
|
"port": "6060",
|
|
|
|
"endpoint": "/public",
|
|
|
|
}
|
|
|
|
|
2016-09-20 11:17:41 +03:00
|
|
|
for name := range testValues {
|
2015-12-10 21:14:17 +03:00
|
|
|
testValues[name] = flagSet.String(name, "", "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
flagValueSet := pflagValueSet{flagSet}
|
|
|
|
|
|
|
|
err := BindFlagValues(flagValueSet)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error binding flag set, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
flagSet.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flag.Value.Set(mutatedTestValues[flag.Name])
|
|
|
|
flag.Changed = true
|
|
|
|
})
|
|
|
|
|
|
|
|
for name, expected := range mutatedTestValues {
|
2023-09-25 15:43:09 +03:00
|
|
|
assert.Equal(t, expected, Get(name))
|
2015-12-10 21:14:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindFlagValue(t *testing.T) {
|
2020-09-11 18:48:38 +03:00
|
|
|
testString := "testing"
|
|
|
|
testValue := newStringValue(testString, &testString)
|
2015-12-10 21:14:17 +03:00
|
|
|
|
|
|
|
flag := &pflag.Flag{
|
|
|
|
Name: "testflag",
|
|
|
|
Value: testValue,
|
|
|
|
Changed: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
flagValue := pflagValue{flag}
|
|
|
|
BindFlagValue("testvalue", flagValue)
|
|
|
|
|
|
|
|
assert.Equal(t, testString, Get("testvalue"))
|
|
|
|
|
|
|
|
flag.Value.Set("testing_mutate")
|
2020-09-11 18:48:38 +03:00
|
|
|
flag.Changed = true // hack for pflag usage
|
2015-12-10 21:14:17 +03:00
|
|
|
|
|
|
|
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
|
|
|
}
|