From 713fc392d79c1568f37b053fce8361ac860e27ae Mon Sep 17 00:00:00 2001 From: Rob Playford Date: Wed, 25 Nov 2020 15:54:05 +0000 Subject: [PATCH 1/3] fix home directory config not loading --- README.md | 1 + cobra/cmd/root.go | 1 + cobra/cmd/testdata/root.go.golden | 1 + cobra/tpl/main.go | 1 + 4 files changed, 4 insertions(+) diff --git a/README.md b/README.md index 3cf1b25d8..dbf95f21a 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,7 @@ func initConfig() { // Search config in home directory with name ".cobra" (without extension). viper.AddConfigPath(home) + viper.SetConfigType("yaml") viper.SetConfigName(".cobra") } diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 97f404bbb..038444a3f 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -69,6 +69,7 @@ func initConfig() { // Search config in home directory with name ".cobra" (without extension). viper.AddConfigPath(home) + viper.SetConfigType("yaml") viper.SetConfigName(".cobra") } diff --git a/cobra/cmd/testdata/root.go.golden b/cobra/cmd/testdata/root.go.golden index 1db829c71..3e2ca72cd 100644 --- a/cobra/cmd/testdata/root.go.golden +++ b/cobra/cmd/testdata/root.go.golden @@ -79,6 +79,7 @@ func initConfig() { // Search config in home directory with name ".testproject" (without extension). viper.AddConfigPath(home) + viper.SetConfigType("yaml") viper.SetConfigName(".testproject") } diff --git a/cobra/tpl/main.go b/cobra/tpl/main.go index 4348e5616..9e7851ae9 100644 --- a/cobra/tpl/main.go +++ b/cobra/tpl/main.go @@ -93,6 +93,7 @@ func initConfig() { // Search config in home directory with name ".{{ .AppName }}" (without extension). viper.AddConfigPath(home) + viper.SetConfigType("yaml") viper.SetConfigName(".{{ .AppName }}") } From 41dc6e7f5c5c241de67aab2071e9995e04cb8614 Mon Sep 17 00:00:00 2001 From: Rob Playford Date: Mon, 21 Dec 2020 12:16:54 +0000 Subject: [PATCH 2/3] add test for proof of fix --- cobra/cmd/root_test.go | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 cobra/cmd/root_test.go diff --git a/cobra/cmd/root_test.go b/cobra/cmd/root_test.go new file mode 100644 index 000000000..2e89b04a7 --- /dev/null +++ b/cobra/cmd/root_test.go @@ -0,0 +1,72 @@ +package cmd + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "testing" + + homedir "github.com/mitchellh/go-homedir" + "github.com/spf13/viper" +) + +func TestConfigFileDefaultFormat(t *testing.T) { + var ( + defaultConfigName = ".cobra" + configFileTestSuffix = ".test.bak" + testKey = "aKey" + testValue0 = "aValue0" + testValue1 = "aValue1" + defaultConfigFileContent = []byte(fmt.Sprintf( + "---\n%s:\n - %s\n - %s\n", + testKey, testValue0, testValue1, + )) + home string + err error + ) + + if home, err = homedir.Dir(); err != nil { + t.Fatalf("Error discovering home directory: %v", err) + } + + configFilePath := filepath.Join(home, defaultConfigName) + + // if file already exists, move to backup + if fileInfo, _ := os.Stat(configFilePath); fileInfo != nil { + configFilePathBackup := configFilePath + configFileTestSuffix + os.Remove(configFilePathBackup) + if err = os.Rename(configFilePath, configFilePathBackup); err != nil { + t.Fatalf("Error backing up existing config file '%v' :'%v'", configFilePath, err) + } + // defer moving backup back to original + defer func() { + os.Remove(configFilePath) + os.Rename(configFilePathBackup, configFilePath) + }() + } + + // create test file + os.Remove(configFilePath) + ioutil.WriteFile(configFilePath, defaultConfigFileContent, 0666) + if _, err = os.Stat(configFilePath); os.IsNotExist(err) { + t.Fatalf("Expected to find file '%s', but got '%v'", configFilePath, err) + } + + // attempt to use config file + initConfig() + + result := viper.GetStringSlice(testKey) + expected := []string{testValue0, testValue1} + if len(result) != len(expected) { + t.Fatalf("Expected '%T' with %d elements, but got '%v'", expected, len(expected), result) + } + for i := range result { + if result[i] != expected[i] { + t.Fatalf("Expected '%v', but got '%v'", expected, result) + } + } + + // delete test file + os.Remove(configFilePath) +} From a22d49f6b7ce1d51fb311b94b3ae51364d355d0c Mon Sep 17 00:00:00 2001 From: Rob Playford Date: Tue, 15 Jun 2021 08:59:35 +0100 Subject: [PATCH 3/3] Revert "add test for proof of fix" This reverts commit 41dc6e7f5c5c241de67aab2071e9995e04cb8614. --- cobra/cmd/root_test.go | 72 ------------------------------------------ 1 file changed, 72 deletions(-) delete mode 100644 cobra/cmd/root_test.go diff --git a/cobra/cmd/root_test.go b/cobra/cmd/root_test.go deleted file mode 100644 index 2e89b04a7..000000000 --- a/cobra/cmd/root_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package cmd - -import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" - "testing" - - homedir "github.com/mitchellh/go-homedir" - "github.com/spf13/viper" -) - -func TestConfigFileDefaultFormat(t *testing.T) { - var ( - defaultConfigName = ".cobra" - configFileTestSuffix = ".test.bak" - testKey = "aKey" - testValue0 = "aValue0" - testValue1 = "aValue1" - defaultConfigFileContent = []byte(fmt.Sprintf( - "---\n%s:\n - %s\n - %s\n", - testKey, testValue0, testValue1, - )) - home string - err error - ) - - if home, err = homedir.Dir(); err != nil { - t.Fatalf("Error discovering home directory: %v", err) - } - - configFilePath := filepath.Join(home, defaultConfigName) - - // if file already exists, move to backup - if fileInfo, _ := os.Stat(configFilePath); fileInfo != nil { - configFilePathBackup := configFilePath + configFileTestSuffix - os.Remove(configFilePathBackup) - if err = os.Rename(configFilePath, configFilePathBackup); err != nil { - t.Fatalf("Error backing up existing config file '%v' :'%v'", configFilePath, err) - } - // defer moving backup back to original - defer func() { - os.Remove(configFilePath) - os.Rename(configFilePathBackup, configFilePath) - }() - } - - // create test file - os.Remove(configFilePath) - ioutil.WriteFile(configFilePath, defaultConfigFileContent, 0666) - if _, err = os.Stat(configFilePath); os.IsNotExist(err) { - t.Fatalf("Expected to find file '%s', but got '%v'", configFilePath, err) - } - - // attempt to use config file - initConfig() - - result := viper.GetStringSlice(testKey) - expected := []string{testValue0, testValue1} - if len(result) != len(expected) { - t.Fatalf("Expected '%T' with %d elements, but got '%v'", expected, len(expected), result) - } - for i := range result { - if result[i] != expected[i] { - t.Fatalf("Expected '%v', but got '%v'", expected, result) - } - } - - // delete test file - os.Remove(configFilePath) -}