mirror of https://github.com/spf13/viper.git
viper: fix the ReadInConfig return when using SetConfigFile
The traditional program only uses one absolute configure file path with using SetConfigFile(). If the file doesn't exist, return ConfigFileNotFoundError. Signed-off-by: Li Feng <fengli@smartx.com>
This commit is contained in:
parent
29c3027c49
commit
f9ae92c0e9
4
viper.go
4
viper.go
|
@ -2038,6 +2038,10 @@ func (v *Viper) getConfigFile() (string, error) {
|
|||
return "", err
|
||||
}
|
||||
v.configFile = cf
|
||||
} else {
|
||||
if b, _ := exists(v.fs, v.configFile); !b {
|
||||
return "", ConfigFileNotFoundError{"", v.configFile}
|
||||
}
|
||||
}
|
||||
return v.configFile, nil
|
||||
}
|
||||
|
|
|
@ -301,9 +301,11 @@ func (s *stringValue) String() string {
|
|||
|
||||
func TestBasics(t *testing.T) {
|
||||
SetConfigFile("/tmp/config.yaml")
|
||||
filename, err := v.getConfigFile()
|
||||
assert.Equal(t, "/tmp/config.yaml", filename)
|
||||
assert.NoError(t, err)
|
||||
_, err := v.getConfigFile()
|
||||
assert.Error(t, err)
|
||||
err = ReadInConfig()
|
||||
_, ok := err.(ConfigFileNotFoundError)
|
||||
assert.True(t, ok)
|
||||
}
|
||||
|
||||
func TestSearchInPath_WithoutConfigTypeSet(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue