Skip to content

Commit

Permalink
fix: search file by supported extension use manually specific config …
Browse files Browse the repository at this point in the history
…type first
  • Loading branch information
AuZhoomLee committed May 24, 2024
1 parent f17acb4 commit 7c147e4
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 2 deletions.
17 changes: 15 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,24 @@ func (v *Viper) searchInPath(in string) (filename string) {
for _, ext := range SupportedExts {
v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext))
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext))
return filepath.Join(in, v.configName+"."+ext)
// record the first found
if len(filename) < 1 {
filename = filepath.Join(in, v.configName+"."+ext)
}
// if specific configType are same with the current extension type, then return current
if v.configType == ext {
filename = filepath.Join(in, v.configName+"."+ext)
break
}
}
}

// return only if file exists
if len(filename) > 0 {
v.logger.Debug("found file", "file", filename)
return filename
}

if v.configType != "" {
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
return filepath.Join(in, v.configName)
Expand Down
142 changes: 142 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package viper

import (
"fmt"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/spf13/viper/internal/testutil"
)

func Test_searchInPath(t *testing.T) {
t.Run("find file without extension", 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"))
require.NoError(t, err)

_, err = file.WriteString(`key: value`)
require.NoError(t, err)

file.Close()

v := New()

v.SetFs(fs)
v.AddConfigPath("/etc/viper")
v.SetConfigType("yaml")

err = v.ReadInConfig()
require.NoError(t, err)

assert.Equal(t, "value", v.Get("key"))
})

t.Run("cannot find file with extension", func(t *testing.T) {
fs := afero.NewMemMapFs()

err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
require.NoError(t, err)

v := New()

v.SetFs(fs)
v.AddConfigPath("/etc/viper")
v.SetConfigType("yaml")

err = v.ReadInConfig()
require.EqualError(t, err, ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}.Error())
})

t.Run("find file with supported extension", 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 := New()

v.SetFs(fs)
v.AddConfigPath("/etc/viper")
v.SetConfigType("yaml")

err = v.ReadInConfig()
require.NoError(t, err)

assert.Equal(t, "value", v.Get("key"))
})

t.Run("find file with specific extension from multiple supported files", func(t *testing.T) {
fs := afero.NewMemMapFs()

err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
require.NoError(t, err)

jsonFile, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.json"))
require.NoError(t, err)

jsonFile.Close()

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 := New()

v.SetFs(fs)
v.AddConfigPath("/etc/viper")
v.SetConfigType("yaml")

err = v.ReadInConfig()
require.NoError(t, err)

assert.Equal(t, "value", v.Get("key"))
})

t.Run("find file with unsupported extension", func(t *testing.T) {
fs := afero.NewMemMapFs()

err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
require.NoError(t, err)

jsonFile, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.json"))
require.NoError(t, err)

jsonFile.Close()

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 := New()

v.SetFs(fs)
v.AddConfigPath("/etc/viper")
v.SetConfigType("xyz")

err = v.ReadInConfig()
require.EqualError(t, err, UnsupportedConfigError("xyz").Error())
})
}

0 comments on commit 7c147e4

Please sign in to comment.