MCPcopy
hub / github.com/spf13/viper / TestIsSet

Function TestIsSet

viper_test.go:1576–1621  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1574}
1575
1576func TestIsSet(t *testing.T) {
1577 v := New()
1578 v.SetConfigType("yaml")
1579
1580 /* config and defaults */
1581 v.ReadConfig(bytes.NewBuffer(yamlExample))
1582 v.SetDefault("clothing.shoes", "sneakers")
1583
1584 assert.True(t, v.IsSet("clothing"))
1585 assert.True(t, v.IsSet("clothing.jacket"))
1586 assert.False(t, v.IsSet("clothing.jackets"))
1587 assert.True(t, v.IsSet("clothing.shoes"))
1588
1589 /* state change */
1590 assert.False(t, v.IsSet("helloworld"))
1591 v.Set("helloworld", "fubar")
1592 assert.True(t, v.IsSet("helloworld"))
1593
1594 /* env */
1595 v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
1596 v.BindEnv("eyes")
1597 v.BindEnv("foo")
1598 v.BindEnv("clothing.hat")
1599 v.BindEnv("clothing.hats")
1600
1601 t.Setenv("FOO", "bar")
1602 t.Setenv("CLOTHING_HAT", "bowler")
1603
1604 assert.True(t, v.IsSet("eyes")) // in the config file
1605 assert.True(t, v.IsSet("foo")) // in the environment
1606 assert.True(t, v.IsSet("clothing.hat")) // in the environment
1607 assert.False(t, v.IsSet("clothing.hats")) // not defined
1608
1609 /* flags */
1610 flagset := pflag.NewFlagSet("testisset", pflag.ContinueOnError)
1611 flagset.Bool("foobaz", false, "foobaz")
1612 flagset.Bool("barbaz", false, "barbaz")
1613 foobaz, barbaz := flagset.Lookup("foobaz"), flagset.Lookup("barbaz")
1614 v.BindPFlag("foobaz", foobaz)
1615 v.BindPFlag("barbaz", barbaz)
1616 barbaz.Value.Set("true")
1617 barbaz.Changed = true // hack for pflag usage
1618
1619 assert.False(t, v.IsSet("foobaz"))
1620 assert.True(t, v.IsSet("barbaz"))
1621}
1622
1623func TestDirsSearch(t *testing.T) {
1624 root, config := initDirs(t)

Callers

nothing calls this directly

Calls 9

NewFunction · 0.85
SetConfigTypeMethod · 0.80
ReadConfigMethod · 0.80
SetDefaultMethod · 0.80
IsSetMethod · 0.80
SetEnvKeyReplacerMethod · 0.80
BindEnvMethod · 0.80
BindPFlagMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected