mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
test: add tests for reading config files
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
08ba8ca7fc
commit
5a4e555471
1 changed files with 52 additions and 0 deletions
|
@ -437,6 +437,58 @@ func TestGetConfigFile(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestReadInConfig(t *testing.T) {
|
||||
t.Run("config file set", func(t *testing.T) {
|
||||
fs := afero.NewMemMapFs()
|
||||
|
||||
err := fs.Mkdir("/etc/viper", 0777)
|
||||
require.NoError(t, err)
|
||||
|
||||
file, err := fs.Create("/etc/viper/config.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = file.Write([]byte(`key: value`))
|
||||
require.NoError(t, err)
|
||||
|
||||
file.Close()
|
||||
|
||||
v := New()
|
||||
|
||||
v.SetFs(fs)
|
||||
v.SetConfigFile("/etc/viper/config.yaml")
|
||||
|
||||
err = v.ReadInConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "value", v.Get("key"))
|
||||
})
|
||||
|
||||
t.Run("find file", func(t *testing.T) {
|
||||
fs := afero.NewMemMapFs()
|
||||
|
||||
err := fs.Mkdir("/etc/viper", 0777)
|
||||
require.NoError(t, err)
|
||||
|
||||
file, err := fs.Create("/etc/viper/config.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = file.Write([]byte(`key: value`))
|
||||
require.NoError(t, err)
|
||||
|
||||
file.Close()
|
||||
|
||||
v := New()
|
||||
|
||||
v.SetFs(fs)
|
||||
v.AddConfigPath("/etc/viper")
|
||||
|
||||
err = v.ReadInConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "value", v.Get("key"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
SetDefault("age", 45)
|
||||
assert.Equal(t, 45, Get("age"))
|
||||
|
|
Loading…
Reference in a new issue