2015-12-10 18:14:17 +00:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-07 19:28:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-12-10 18:14:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBindFlagValueSet(t *testing.T) {
|
2023-10-07 19:35:21 +00:00
|
|
|
Reset()
|
2015-12-10 18:14:17 +00:00
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
|
2020-09-11 15:48:38 +00:00
|
|
|
testValues := map[string]*string{
|
2015-12-10 18:14:17 +00:00
|
|
|
"host": nil,
|
|
|
|
"port": nil,
|
|
|
|
"endpoint": nil,
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:48:38 +00:00
|
|
|
mutatedTestValues := map[string]string{
|
2015-12-10 18:14:17 +00:00
|
|
|
"host": "localhost",
|
|
|
|
"port": "6060",
|
|
|
|
"endpoint": "/public",
|
|
|
|
}
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
for name := range testValues {
|
2015-12-10 18:14:17 +00:00
|
|
|
testValues[name] = flagSet.String(name, "", "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
flagValueSet := pflagValueSet{flagSet}
|
|
|
|
|
|
|
|
err := BindFlagValues(flagValueSet)
|
2023-10-07 19:28:36 +00:00
|
|
|
require.NoError(t, err, "error binding flag set")
|
2015-12-10 18:14:17 +00:00
|
|
|
|
|
|
|
flagSet.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flag.Value.Set(mutatedTestValues[flag.Name])
|
|
|
|
flag.Changed = true
|
|
|
|
})
|
|
|
|
|
|
|
|
for name, expected := range mutatedTestValues {
|
2023-09-25 12:43:09 +00:00
|
|
|
assert.Equal(t, expected, Get(name))
|
2015-12-10 18:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindFlagValue(t *testing.T) {
|
2020-09-11 15:48:38 +00:00
|
|
|
testString := "testing"
|
|
|
|
testValue := newStringValue(testString, &testString)
|
2015-12-10 18:14:17 +00: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 15:48:38 +00:00
|
|
|
flag.Changed = true // hack for pflag usage
|
2015-12-10 18:14:17 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
|
|
|
}
|