test: improve tests for searching files

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2021-09-21 11:04:49 +02:00 committed by Márk Sági-Kazár
parent 8e71595a4a
commit 557c5d64e0

View file

@ -301,65 +301,140 @@ func (s *stringValue) String() string {
return string(*s) return string(*s)
} }
func TestBasics(t *testing.T) { func TestGetConfigFile(t *testing.T) {
SetConfigFile("/tmp/config.yaml") t.Run("config file set", func(t *testing.T) {
filename, err := v.getConfigFile() fs := afero.NewMemMapFs()
assert.Equal(t, "/tmp/config.yaml", filename)
assert.NoError(t, err)
}
func TestSearchInPath_WithoutConfigTypeSet(t *testing.T) { err := fs.Mkdir("/etc/viper", 0777)
filename := ".dotfilenoext" require.NoError(t, err)
path := "/tmp"
file := filepath.Join(path, filename)
SetConfigName(filename)
AddConfigPath(path)
_, createErr := v.fs.Create(file)
defer func() {
_ = v.fs.Remove(file)
}()
assert.NoError(t, createErr)
_, err := v.getConfigFile()
// unless config type is set, files without extension
// are not considered
assert.Error(t, err)
}
func TestSearchInPath(t *testing.T) { _, err = fs.Create("/etc/viper/config.yaml")
filename := ".dotfilenoext" require.NoError(t, err)
path := "/tmp"
file := filepath.Join(path, filename)
SetConfigName(filename)
SetConfigType("yaml")
AddConfigPath(path)
_, createErr := v.fs.Create(file)
defer func() {
_ = v.fs.Remove(file)
}()
assert.NoError(t, createErr)
filename, err := v.getConfigFile()
assert.Equal(t, file, filename)
assert.NoError(t, err)
}
func TestSearchInPath_FilesOnly(t *testing.T) { v := New()
fs := afero.NewMemMapFs()
err := fs.Mkdir("/tmp/config", 0777) v.SetFs(fs)
require.NoError(t, err) v.AddConfigPath("/etc/viper")
v.SetConfigFile("/etc/viper/config.json")
_, err = fs.Create("/tmp/config/config.yaml") filename, err := v.getConfigFile()
require.NoError(t, err) assert.Equal(t, "/etc/viper/config.json", filename)
assert.NoError(t, err)
})
v := New() t.Run("find file", func(t *testing.T) {
fs := afero.NewMemMapFs()
v.SetFs(fs) err := fs.Mkdir("/etc/viper", 0777)
v.AddConfigPath("/tmp") require.NoError(t, err)
v.AddConfigPath("/tmp/config")
filename, err := v.getConfigFile() _, err = fs.Create("/etc/viper/config.yaml")
assert.Equal(t, "/tmp/config/config.yaml", filename) require.NoError(t, err)
assert.NoError(t, err)
v := New()
v.SetFs(fs)
v.AddConfigPath("/etc/viper")
filename, err := v.getConfigFile()
assert.Equal(t, "/etc/viper/config.yaml", filename)
assert.NoError(t, err)
})
t.Run("find files only", func(t *testing.T) {
fs := afero.NewMemMapFs()
err := fs.Mkdir("/etc/config", 0777)
require.NoError(t, err)
_, err = fs.Create("/etc/config/config.yaml")
require.NoError(t, err)
v := New()
v.SetFs(fs)
v.AddConfigPath("/etc")
v.AddConfigPath("/etc/config")
filename, err := v.getConfigFile()
assert.Equal(t, "/etc/config/config.yaml", filename)
assert.NoError(t, err)
})
t.Run("precedence", func(t *testing.T) {
fs := afero.NewMemMapFs()
err := fs.Mkdir("/home/viper", 0777)
require.NoError(t, err)
_, err = fs.Create("/home/viper/config.zml")
require.NoError(t, err)
err = fs.Mkdir("/etc/viper", 0777)
require.NoError(t, err)
_, err = fs.Create("/etc/viper/config.bml")
require.NoError(t, err)
err = fs.Mkdir("/var/viper", 0777)
require.NoError(t, err)
_, err = fs.Create("/var/viper/config.yaml")
require.NoError(t, err)
v := New()
v.SetFs(fs)
v.AddConfigPath("/home/viper")
v.AddConfigPath("/etc/viper")
v.AddConfigPath("/var/viper")
filename, err := v.getConfigFile()
assert.Equal(t, "/var/viper/config.yaml", filename)
assert.NoError(t, err)
})
t.Run("without extension", func(t *testing.T) {
fs := afero.NewMemMapFs()
err := fs.Mkdir("/etc/viper", 0777)
require.NoError(t, err)
_, err = fs.Create("/etc/viper/.dotfilenoext")
require.NoError(t, err)
v := New()
v.SetFs(fs)
v.AddConfigPath("/etc/viper")
v.SetConfigName(".dotfilenoext")
v.SetConfigType("yaml")
filename, err := v.getConfigFile()
assert.Equal(t, "/etc/viper/.dotfilenoext", filename)
assert.NoError(t, err)
})
t.Run("without extension and config type", func(t *testing.T) {
fs := afero.NewMemMapFs()
err := fs.Mkdir("/etc/viper", 0777)
require.NoError(t, err)
_, err = fs.Create("/etc/viper/.dotfilenoext")
require.NoError(t, err)
v := New()
v.SetFs(fs)
v.AddConfigPath("/etc/viper")
v.SetConfigName(".dotfilenoext")
_, err = v.getConfigFile()
// unless config type is set, files without extension
// are not considered
assert.Error(t, err)
})
} }
func TestDefault(t *testing.T) { func TestDefault(t *testing.T) {