test: fix finder tests

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-06-04 15:44:19 +02:00 committed by Márk Sági-Kazár
parent df70866789
commit 164252315d
1 changed files with 38 additions and 4 deletions

View File

@ -364,6 +364,34 @@ func TestGetConfigFile(t *testing.T) {
// are not considered // are not considered
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("using a 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)
finder := locafero.Finder{
Paths: []string{"/etc/viper"},
Names: locafero.NameWithExtensions("config", SupportedExts...),
Type: locafero.FileTypeFile,
}
v := NewWithOptions(WithFinder(finder))
v.SetFs(fs)
// These should be ineffective
v.AddConfigPath("/etc/something_else")
v.SetConfigName("not-config")
filename, err := v.getConfigFile()
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), filename)
assert.NoError(t, err)
})
} }
func TestReadInConfig(t *testing.T) { func TestReadInConfig(t *testing.T) {
@ -423,9 +451,14 @@ func TestReadInConfig(t *testing.T) {
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777) err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
require.NoError(t, err) require.NoError(t, err)
_, err = fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml")) file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
require.NoError(t, err) require.NoError(t, err)
_, err = file.WriteString(`key: value`)
require.NoError(t, err)
file.Close()
finder := locafero.Finder{ finder := locafero.Finder{
Paths: []string{"/etc/viper"}, Paths: []string{"/etc/viper"},
Names: locafero.NameWithExtensions("config", SupportedExts...), Names: locafero.NameWithExtensions("config", SupportedExts...),
@ -440,9 +473,10 @@ func TestReadInConfig(t *testing.T) {
v.AddConfigPath("/etc/something_else") v.AddConfigPath("/etc/something_else")
v.SetConfigName("not-config") v.SetConfigName("not-config")
filename, err := v.getConfigFile() err = v.ReadInConfig()
assert.Equal(t, testutil.AbsFilePath(t, "/etc/viper/config.yaml"), filename) require.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, "value", v.Get("key"))
}) })
} }