From 4e1ebbdaba650cf4bb51d8a7e241b80b8d6d9553 Mon Sep 17 00:00:00 2001 From: Benoit Masson Date: Tue, 11 Apr 2017 17:49:10 +0200 Subject: [PATCH] Added tests to TestIsSet() Added tests for: - nested elements - environment values - flags (currently fails => IsSet() always returns true, due to the default value of the flag) --- viper_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/viper_test.go b/viper_test.go index 8dcfd1a..677a14b 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1039,12 +1039,47 @@ func TestReadBufConfig(t *testing.T) { func TestIsSet(t *testing.T) { v := New() v.SetConfigType("yaml") + + /* config and defaults */ v.ReadConfig(bytes.NewBuffer(yamlExample)) + v.SetDefault("clothing.shoes", "sneakers") + + assert.True(t, v.IsSet("clothing")) assert.True(t, v.IsSet("clothing.jacket")) assert.False(t, v.IsSet("clothing.jackets")) + assert.True(t, v.IsSet("clothing.shoes")) + + /* state change */ assert.False(t, v.IsSet("helloworld")) v.Set("helloworld", "fubar") assert.True(t, v.IsSet("helloworld")) + + /* env */ + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + v.BindEnv("eyes") + v.BindEnv("foo") + v.BindEnv("clothing.hat") + v.BindEnv("clothing.hats") + os.Setenv("FOO", "bar") + os.Setenv("CLOTHING_HAT", "bowler") + + assert.True(t, v.IsSet("eyes")) // in the config file + assert.True(t, v.IsSet("foo")) // in the environment + assert.True(t, v.IsSet("clothing.hat")) // in the environment + assert.False(t, v.IsSet("clothing.hats")) // not defined + + /* flags */ + flagset := pflag.NewFlagSet("testisset", pflag.ContinueOnError) + flagset.Bool("foobaz", false, "foobaz") + flagset.Bool("barbaz", false, "barbaz") + foobaz, barbaz := flagset.Lookup("foobaz"), flagset.Lookup("barbaz") + v.BindPFlag("foobaz", foobaz) + v.BindPFlag("barbaz", barbaz) + barbaz.Value.Set("true") + barbaz.Changed = true // hack for pflag usage + + assert.False(t, v.IsSet("foobaz")) + assert.True(t, v.IsSet("barbaz")) } func TestDirsSearch(t *testing.T) {