From f9ae92c0e99a634f14c79ff299445ee3bd46eaad Mon Sep 17 00:00:00 2001 From: Feng Li Date: Mon, 28 Sep 2020 19:48:16 +0800 Subject: [PATCH] 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 --- viper.go | 4 ++++ viper_test.go | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/viper.go b/viper.go index 8889f87..c40a233 100644 --- a/viper.go +++ b/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 } diff --git a/viper_test.go b/viper_test.go index c38c665..3b41b1e 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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) {