From 3968772673dadf6f84d9dd11ae2b5ec748aea4b9 Mon Sep 17 00:00:00 2001 From: Julien Kauffmann Date: Tue, 24 Jan 2017 09:09:02 -0500 Subject: [PATCH 1/3] Fixed tests not passing on Windows --- viper_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/viper_test.go b/viper_test.go index cd7b65c..6b9754a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -11,8 +11,9 @@ import ( "io" "io/ioutil" "os" - "path" + path "path/filepath" "reflect" + "runtime" "sort" "strings" "testing" @@ -183,10 +184,16 @@ func initHcl() { func initDirs(t *testing.T) (string, string, func()) { var ( - testDirs = []string{`a a`, `b`, `c\c`, `D_`} + testDirs []string config = `improbable` ) + if runtime.GOOS == "windows" { + testDirs = []string{`a a`, `b`, `D_`} + } else { + testDirs = []string{`a a`, `b`, `c\c`, `D_`} + } + root, err := ioutil.TempDir("", "") cleanup := true From 4257721a8b72a215e17cbcf1344b1f50d69abc87 Mon Sep 17 00:00:00 2001 From: Julien Kauffmann Date: Fri, 2 Dec 2016 14:11:59 -0500 Subject: [PATCH 2/3] Fixed environment variable handling on Windows (fixes #282) --- util.go | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/util.go b/util.go index 3ebada9..d4bc11b 100644 --- a/util.go +++ b/util.go @@ -94,16 +94,33 @@ func insensitiviseMap(m map[string]interface{}) { } } +func getEnv(key string) string { + if key == "HOME" && runtime.GOOS == "windows" { + home := os.Getenv(key) + + if home != "" { + return home + } + + home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + + if home != "" { + return home + } + + return os.Getenv("USERPROFILE") + } + + return os.Getenv(key) +} + func absPathify(inPath string) string { jww.INFO.Println("Trying to resolve absolute path to", inPath) - if strings.HasPrefix(inPath, "$HOME") { - inPath = userHomeDir() + inPath[5:] - } + inPath = filepath.FromSlash(inPath) - if strings.HasPrefix(inPath, "$") { - end := strings.Index(inPath, string(os.PathSeparator)) - inPath = os.Getenv(inPath[1:end]) + inPath[end:] + if strings.ContainsRune(inPath, '$') { + inPath = os.Expand(inPath, getEnv) } if filepath.IsAbs(inPath) { @@ -141,17 +158,6 @@ func stringInSlice(a string, list []string) bool { return false } -func userHomeDir() string { - if runtime.GOOS == "windows" { - home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") - if home == "" { - home = os.Getenv("USERPROFILE") - } - return home - } - return os.Getenv("HOME") -} - func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error { buf := new(bytes.Buffer) buf.ReadFrom(in) From 1803d14360572fcc2ccd1ec4cadd8c04e5f25148 Mon Sep 17 00:00:00 2001 From: Julien Kauffmann Date: Tue, 24 Jan 2017 09:08:30 -0500 Subject: [PATCH 3/3] Replaced ContainsRune() usage with IndexRune() for portability --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index d4bc11b..34062e2 100644 --- a/util.go +++ b/util.go @@ -119,7 +119,7 @@ func absPathify(inPath string) string { inPath = filepath.FromSlash(inPath) - if strings.ContainsRune(inPath, '$') { + if strings.IndexRune(inPath, '$') >= 0 { inPath = os.Expand(inPath, getEnv) }