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:
Feng Li 2020-09-28 19:48:16 +08:00 committed by Li Feng
parent 29c3027c49
commit f9ae92c0e9
2 changed files with 9 additions and 3 deletions

View file

@ -2038,6 +2038,10 @@ func (v *Viper) getConfigFile() (string, error) {
return "", err return "", err
} }
v.configFile = cf v.configFile = cf
} else {
if b, _ := exists(v.fs, v.configFile); !b {
return "", ConfigFileNotFoundError{"", v.configFile}
}
} }
return v.configFile, nil return v.configFile, nil
} }

View file

@ -301,9 +301,11 @@ func (s *stringValue) String() string {
func TestBasics(t *testing.T) { func TestBasics(t *testing.T) {
SetConfigFile("/tmp/config.yaml") SetConfigFile("/tmp/config.yaml")
filename, err := v.getConfigFile() _, err := v.getConfigFile()
assert.Equal(t, "/tmp/config.yaml", filename) assert.Error(t, err)
assert.NoError(t, err) err = ReadInConfig()
_, ok := err.(ConfigFileNotFoundError)
assert.True(t, ok)
} }
func TestSearchInPath_WithoutConfigTypeSet(t *testing.T) { func TestSearchInPath_WithoutConfigTypeSet(t *testing.T) {