2015-12-10 18:14:17 +00: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)
|
|
|
|
|
|
|
|
var testValues = map[string]*string{
|
|
|
|
"host": nil,
|
|
|
|
"port": nil,
|
|
|
|
"endpoint": nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
var mutatedTestValues = map[string]string{
|
|
|
|
"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)
|
|
|
|
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 {
|
|
|
|
assert.Equal(t, Get(name), expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindFlagValue(t *testing.T) {
|
|
|
|
var testString = "testing"
|
|
|
|
var testValue = newStringValue(testString, &testString)
|
|
|
|
|
|
|
|
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")
|
|
|
|
flag.Changed = true //hack for pflag usage
|
|
|
|
|
|
|
|
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
|
|
|
|
|
|
|
}
|