mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Fix file path lookup
This commit is contained in:
parent
5ad4bc05cf
commit
eabbc68a3e
2 changed files with 23 additions and 3 deletions
6
util.go
6
util.go
|
@ -114,11 +114,11 @@ func absPathify(inPath string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// Check if File / Directory Exists
|
||||
// Check if file Exists
|
||||
func exists(fs afero.Fs, path string) (bool, error) {
|
||||
_, err := fs.Stat(path)
|
||||
stat, err := fs.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
return !stat.IsDir(), nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
|
|
|
@ -323,6 +323,26 @@ func TestSearchInPath(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestSearchInPath_FilesOnly(t *testing.T) {
|
||||
fs := afero.NewMemMapFs()
|
||||
|
||||
err := fs.Mkdir("/tmp/config", 0777)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = fs.Create("/tmp/config/config.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
v := New()
|
||||
|
||||
v.SetFs(fs)
|
||||
v.AddConfigPath("/tmp")
|
||||
v.AddConfigPath("/tmp/config")
|
||||
|
||||
filename, err := v.getConfigFile()
|
||||
assert.Equal(t, "/tmp/config/config.yaml", filename)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
SetDefault("age", 45)
|
||||
assert.Equal(t, 45, Get("age"))
|
||||
|
|
Loading…
Reference in a new issue