Skip to content

Commit

Permalink
Fix file path lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed Dec 6, 2019
1 parent 5ad4bc0 commit eabbc68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit eabbc68

Please sign in to comment.