test: add tests for experimental finder

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-06-04 20:56:30 +02:00 committed by Márk Sági-Kazár
parent 92e330ea77
commit 676d2eb166

View file

@ -365,7 +365,27 @@ func TestGetConfigFile(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("using a finder", func(t *testing.T) { t.Run("experimental finder", func(t *testing.T) {
fs := afero.NewMemMapFs()
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
require.NoError(t, err)
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
require.NoError(t, err)
v := NewWithOptions(ExperimentalFinder())
v.SetFs(fs)
v.AddConfigPath("/etc/viper")
filename, err := v.getConfigFile()
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), testutil.AbsFilePath(t, filename))
assert.NoError(t, err)
})
t.Run("finder", func(t *testing.T) {
fs := afero.NewMemMapFs() fs := afero.NewMemMapFs()
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
@ -445,6 +465,31 @@ func TestReadInConfig(t *testing.T) {
assert.Equal(t, "value", v.Get("key")) assert.Equal(t, "value", v.Get("key"))
}) })
t.Run("find file with experimental finder", func(t *testing.T) {
fs := afero.NewMemMapFs()
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
require.NoError(t, err)
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
require.NoError(t, err)
_, err = file.WriteString(`key: value`)
require.NoError(t, err)
file.Close()
v := NewWithOptions(ExperimentalFinder())
v.SetFs(fs)
v.AddConfigPath("/etc/viper")
err = v.ReadInConfig()
require.NoError(t, err)
assert.Equal(t, "value", v.Get("key"))
})
t.Run("find file using a finder", func(t *testing.T) { t.Run("find file using a finder", func(t *testing.T) {
fs := afero.NewMemMapFs() fs := afero.NewMemMapFs()