Fix file path lookup

This commit is contained in:
Mark Sagi-Kazar 2019-12-06 17:23:59 +01:00 committed by Márk Sági-Kazár
parent 5ad4bc05cf
commit eabbc68a3e
2 changed files with 23 additions and 3 deletions

View File

@ -114,11 +114,11 @@ func absPathify(inPath string) string {
return "" return ""
} }
// Check if File / Directory Exists // Check if file Exists
func exists(fs afero.Fs, path string) (bool, error) { func exists(fs afero.Fs, path string) (bool, error) {
_, err := fs.Stat(path) stat, err := fs.Stat(path)
if err == nil { if err == nil {
return true, nil return !stat.IsDir(), nil
} }
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false, nil return false, nil

View File

@ -323,6 +323,26 @@ func TestSearchInPath(t *testing.T) {
assert.NoError(t, err) 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) { func TestDefault(t *testing.T) {
SetDefault("age", 45) SetDefault("age", 45)
assert.Equal(t, 45, Get("age")) assert.Equal(t, 45, Get("age"))