Skip to content

Commit 0afb045

Browse files
spacez320sagikazarmark
authored andcommitted
fix: return not found error if setting config
1 parent 2da50fe commit 0afb045

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

‎file.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func (v *Viper) findConfigFileWithFinder(finder Finder) (string, error) {
5050
}
5151

5252
if len(results) == 0 {
53-
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
53+
return "", ConfigFileNotFoundError{
54+
name: v.configName, locations: fmt.Sprintf("%s", v.configPaths)}
5455
}
5556

5657
// We call clean on the final result to ensure that the path is in its canonical form.
@@ -69,7 +70,8 @@ func (v *Viper) findConfigFileOld() (string, error) {
6970
return file, nil
7071
}
7172
}
72-
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
73+
return "", ConfigFileNotFoundError{
74+
name: v.configName, locations: fmt.Sprintf("%s", v.configPaths)}
7375
}
7476

7577
func (v *Viper) searchInPath(in string) (filename string) {

‎viper.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ type ConfigFileNotFoundError struct {
7676

7777
// Error returns the formatted configuration error.
7878
func (fnfe ConfigFileNotFoundError) Error() string {
79-
return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
79+
message := fmt.Sprintf("Config file %q Not Found", fnfe.name)
80+
if fnfe.locations != "" {
81+
message += fmt.Sprintf(" in %q", fnfe.locations)
82+
}
83+
84+
return message
8085
}
8186

8287
// ConfigFileAlreadyExistsError denotes failure to write new configuration file.
@@ -1527,11 +1532,17 @@ func (v *Viper) ReadInConfig() error {
15271532
}
15281533

15291534
v.logger.Debug("reading file", "file", filename)
1535+
exists, err := afero.Exists(v.fs, filename)
1536+
if err != nil {
1537+
return err
1538+
}
1539+
if !exists {
1540+
return ConfigFileNotFoundError{name: filename, locations: ""}
1541+
}
15301542
file, err := afero.ReadFile(v.fs, filename)
15311543
if err != nil {
15321544
return err
15331545
}
1534-
15351546
config := make(map[string]any)
15361547

15371548
err = v.unmarshalReader(bytes.NewReader(file), config)

‎viper_test.go‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func TestReadInConfig(t *testing.T) {
359359
t.Run("config file set", func(t *testing.T) {
360360
fs := afero.NewMemMapFs()
361361

362-
err := fs.Mkdir("/etc/viper", 0o777)
362+
err := fs.Mkdir(testutil.AbsFilePath(t, "/etc/viper"), 0o777)
363363
require.NoError(t, err)
364364

365365
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
@@ -1573,6 +1573,23 @@ func TestReadConfigWithSetConfigFile(t *testing.T) {
15731573
assert.Equal(t, 45000, v.GetInt("hello.pop"))
15741574
}
15751575

1576+
func TestWrongConfigWithSetConfigFileNotFound(t *testing.T) {
1577+
_, config := initDirs(t)
1578+
1579+
v := New()
1580+
v.SetConfigName(config)
1581+
v.SetDefault(`key`, `default`)
1582+
1583+
v.SetConfigFile(`whatareyoutalkingabout.yaml`)
1584+
1585+
err := v.ReadInConfig()
1586+
assert.IsType(t, ConfigFileNotFoundError{name: "", locations: ""}, err)
1587+
1588+
// Even though config did not load and the error might have
1589+
// been ignored by the client, the default still loads
1590+
assert.Equal(t, `default`, v.GetString(`key`))
1591+
}
1592+
15761593
func TestIsSet(t *testing.T) {
15771594
v := New()
15781595
v.SetConfigType("yaml")
@@ -1652,7 +1669,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
16521669
v.AddConfigPath(`thispathaintthere`)
16531670

16541671
err := v.ReadInConfig()
1655-
assert.IsType(t, ConfigFileNotFoundError{"", ""}, err)
1672+
assert.IsType(t, ConfigFileNotFoundError{name: "", locations: ""}, err)
16561673

16571674
// Even though config did not load and the error might have
16581675
// been ignored by the client, the default still loads
@@ -1670,7 +1687,7 @@ func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
16701687
v.AddConfigPath(`thispathaintthere`)
16711688

16721689
err := v.MergeInConfig()
1673-
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err))
1690+
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{name: "", locations: ""}), reflect.TypeOf(err))
16741691

16751692
// Even though config did not load and the error might have
16761693
// been ignored by the client, the default still loads

0 commit comments

Comments
 (0)