From fb16a6b8d52881f46d9e7ca3c01cf3abc238436f Mon Sep 17 00:00:00 2001 From: Vlad Didenko Date: Wed, 27 May 2015 15:30:04 -0500 Subject: [PATCH 1/4] spf13/viper#73 Tests to document current behavior and expose the bug --- viper_test.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/viper_test.go b/viper_test.go index 7ad0245..d513e25 100644 --- a/viper_test.go +++ b/viper_test.go @@ -8,7 +8,9 @@ package viper import ( "bytes" "fmt" + "io/ioutil" "os" + "path" "sort" "strings" "testing" @@ -124,6 +126,47 @@ func initTOML() { marshalReader(r, v.config) } +// make directories for testing +func initDirs(t *testing.T) (string, string, func()) { + + var ( + testDirs = []string{`a a`, `b`, `c\c`, `D:`} + config = `improbable` + ) + + root, err := ioutil.TempDir("", "") + + cleanup := true + defer func() { + if cleanup { + os.Chdir("..") + os.RemoveAll(root) + } + }() + + assert.Nil(t, err) + + err = os.Chdir(root) + assert.Nil(t, err) + + err = ioutil.WriteFile(path.Join(root, config+".toml"), []byte("key = \"root\"\n"), 0640) + assert.Nil(t, err) + + for _, dir := range testDirs { + err = os.Mkdir(dir, 0750) + assert.Nil(t, err) + + err = ioutil.WriteFile(path.Join(dir, config+".toml"), []byte("key = \"value is "+dir+"\"\n"), 0640) + assert.Nil(t, err) + } + + cleanup = false + return root, config, func() { + os.Chdir("..") + os.RemoveAll(root) + } +} + //stubs for PFlag Values type stringValue string @@ -551,3 +594,59 @@ func TestReadBufConfig(t *testing.T) { assert.Equal(t, map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, v.Get("clothing")) assert.Equal(t, 35, v.Get("age")) } + +func TestCWDSearch(t *testing.T) { + + _, config, cleanup := initDirs(t) + defer cleanup() + + v := New() + v.SetConfigName(config) + v.SetDefault(`key`, `default`) + + err := v.ReadInConfig() + assert.Nil(t, err) + + assert.Equal(t, `root`, v.GetString(`key`)) +} + +func TestDirsSearch(t *testing.T) { + + root, config, cleanup := initDirs(t) + defer cleanup() + + v := New() + v.SetConfigName(config) + v.SetDefault(`key`, `default`) + + entries, err := ioutil.ReadDir(root) + for _, e := range entries { + if e.IsDir() { + v.AddConfigPath(e.Name()) + } + } + + err = v.ReadInConfig() + assert.Nil(t, err) + + assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`)) +} + +func TestWrongDirsSearchNotFoundOK(t *testing.T) { + + _, config, cleanup := initDirs(t) + defer cleanup() + + v := New() + v.SetConfigName(config) + v.SetDefault(`key`, `default`) + + v.AddConfigPath(`whattayoutalkingbout`) + v.AddConfigPath(`thispathaintthere`) + + err := v.ReadInConfig() + assert.Nil(t, err) + + // Should not see the value "root" which comes from config in CWD + assert.Equal(t, `default`, v.GetString(`key`)) +} From 033e966e68dffc844835105e17b8dbbb13df65a3 Mon Sep 17 00:00:00 2001 From: Vlad Didenko Date: Wed, 27 May 2015 16:03:21 -0500 Subject: [PATCH 2/4] spf13/viper#73 More specific test to document current behavior --- viper_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/viper_test.go b/viper_test.go index d513e25..554ff7a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "os" "path" + "reflect" "sort" "strings" "testing" @@ -632,7 +633,7 @@ func TestDirsSearch(t *testing.T) { assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`)) } -func TestWrongDirsSearchNotFoundOK(t *testing.T) { +func TestWrongDirsSearchNotFoundHasCWDConfig(t *testing.T) { _, config, cleanup := initDirs(t) defer cleanup() @@ -650,3 +651,25 @@ func TestWrongDirsSearchNotFoundOK(t *testing.T) { // Should not see the value "root" which comes from config in CWD assert.Equal(t, `default`, v.GetString(`key`)) } + +func TestWrongDirsSearchNotFoundNoCWDConfig(t *testing.T) { + + _, config, cleanup := initDirs(t) + defer cleanup() + + os.Remove(config + ".toml") + + v := New() + v.SetConfigName(config) + v.SetDefault(`key`, `default`) + + v.AddConfigPath(`whattayoutalkingbout`) + v.AddConfigPath(`thispathaintthere`) + + err := v.ReadInConfig() + assert.Equal(t, reflect.TypeOf(UnsupportedConfigError("")), reflect.TypeOf(err)) + + // Even though config did not load and the error might have + // been ignored by the client, the default still loads + assert.Equal(t, `default`, v.GetString(`key`)) +} From c53a6dbc69a5ffb884e8129ab409094248581251 Mon Sep 17 00:00:00 2001 From: Vlad Didenko Date: Wed, 27 May 2015 16:29:08 -0500 Subject: [PATCH 3/4] spf13/viper#73 Fix when no config found in specified directories No config in specified directories now behave the same regardless if the config file present in CWD or not --- viper.go | 37 +++++++++++++++++++++++++++++++++---- viper_test.go | 3 ++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/viper.go b/viper.go index 11f3a77..ff80206 100644 --- a/viper.go +++ b/viper.go @@ -72,6 +72,16 @@ func (rce RemoteConfigError) Error() string { return fmt.Sprintf("Remote Configurations Error: %s", string(rce)) } +// Denotes failing to find configuration file. +type ConfigFileNotFoundError struct { + name, locations string +} + +// Returns the formatted configuration error. +func (fnfe ConfigFileNotFoundError) Error() string { + return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations) +} + // Viper is a prioritized configuration registry. It // maintains a set of configuration sources, fetches // values to populate those, and provides them according @@ -955,9 +965,22 @@ func (v *Viper) searchInPath(in string) (filename string) { return "" } -// search all configPaths for any config file. -// Returns the first path that exists (and is a config file) +// Choose where to look for a config file: either +// in provided directories or in the working directory func (v *Viper) findConfigFile() (string, error) { + + if len(v.configPaths) > 0 { + return v.findConfigInPaths() + } else { + return v.findConfigInCWD() + } + +} + +// Search all configPaths for any config file. +// Returns the first path that exists (and has a config file) +func (v *Viper) findConfigInPaths() (string, error) { + jww.INFO.Println("Searching for config in ", v.configPaths) for _, cp := range v.configPaths { @@ -966,14 +989,20 @@ func (v *Viper) findConfigFile() (string, error) { return file, nil } } + return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} +} + +// Search the current working directory for any config file. +func (v *Viper) findConfigInCWD() (string, error) { - // try the current working directory wd, _ := os.Getwd() + jww.INFO.Println("Searching for config in ", wd) + file := v.searchInPath(wd) if file != "" { return file, nil } - return "", fmt.Errorf("config file not found in: %s", v.configPaths) + return "", ConfigFileNotFoundError{v.configName, wd} } // Prints all configuration registries for debugging diff --git a/viper_test.go b/viper_test.go index 554ff7a..2379fcd 100644 --- a/viper_test.go +++ b/viper_test.go @@ -646,7 +646,7 @@ func TestWrongDirsSearchNotFoundHasCWDConfig(t *testing.T) { v.AddConfigPath(`thispathaintthere`) err := v.ReadInConfig() - assert.Nil(t, err) + assert.Equal(t, reflect.TypeOf(UnsupportedConfigError("")), reflect.TypeOf(err)) // Should not see the value "root" which comes from config in CWD assert.Equal(t, `default`, v.GetString(`key`)) @@ -657,6 +657,7 @@ func TestWrongDirsSearchNotFoundNoCWDConfig(t *testing.T) { _, config, cleanup := initDirs(t) defer cleanup() + // Remove the config file in CWD os.Remove(config + ".toml") v := New() From 5fa6a974f222e7a7229b5d414b95cfa584fde5ec Mon Sep 17 00:00:00 2001 From: Vlad Didenko Date: Wed, 27 May 2015 16:35:34 -0500 Subject: [PATCH 4/4] spf13/viper#73 Test to document no config in CWD when expected --- viper_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/viper_test.go b/viper_test.go index 2379fcd..60d2f22 100644 --- a/viper_test.go +++ b/viper_test.go @@ -611,6 +611,24 @@ func TestCWDSearch(t *testing.T) { assert.Equal(t, `root`, v.GetString(`key`)) } +func TestCWDSearchNoConfig(t *testing.T) { + + _, config, cleanup := initDirs(t) + defer cleanup() + + // Remove the config file in CWD + os.Remove(config + ".toml") + + v := New() + v.SetConfigName(config) + v.SetDefault(`key`, `default`) + + err := v.ReadInConfig() + assert.Equal(t, reflect.TypeOf(UnsupportedConfigError("")), reflect.TypeOf(err)) + + assert.Equal(t, `default`, v.GetString(`key`)) +} + func TestDirsSearch(t *testing.T) { root, config, cleanup := initDirs(t)