Skip to content

Commit

Permalink
Added tests to TestIsSet()
Browse files Browse the repository at this point in the history
Added tests for:
- nested elements
- environment values
- flags (currently fails => IsSet() always returns true,
                            due to the default value of the flag)
  • Loading branch information
benoitmasson committed May 22, 2018
1 parent 1573881 commit 4d745f9
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,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) {
Expand Down

0 comments on commit 4d745f9

Please sign in to comment.