From: David Heuschmann Date: Tue, 20 Nov 2018 11:30:14 +0000 (+0100) Subject: os: return an error from UserHomeDir to match UserCacheDir X-Git-Tag: go1.12beta1~285 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=649b89377e91ad6dbe710784f9e662082d31a1ff;p=gostls13.git os: return an error from UserHomeDir to match UserCacheDir UserHomeDir used to return an empty string if the corresponding environment variable was not set. Changed it to return an error if the variable is not set, to have the same signature and behaviour as UserCacheDir. Fixes #28562 Change-Id: I42c497e8011ecfbbadebe7de1751575273be221c Reviewed-on: https://go-review.googlesource.com/c/150418 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/crypto/x509/root_darwin.go b/src/crypto/x509/root_darwin.go index ae69a2fadd..4a02c07596 100644 --- a/src/crypto/x509/root_darwin.go +++ b/src/crypto/x509/root_darwin.go @@ -66,10 +66,10 @@ func execSecurityRoots() (*CertPool, error) { "/Library/Keychains/System.keychain", } - home := os.UserHomeDir() - if home == "" { + home, err := os.UserHomeDir() + if err != nil { if debugExecDarwinRoots { - println("crypto/x509: can't get user home directory") + println("crypto/x509: can't get user home directory: %v", err) } } else { args = append(args, diff --git a/src/os/file.go b/src/os/file.go index d9c5c57c17..9b7863e9b6 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -386,22 +386,24 @@ func UserCacheDir() (string, error) { // On Unix, including macOS, it returns the $HOME environment variable. // On Windows, it returns %USERPROFILE%. // On Plan 9, it returns the $home environment variable. -func UserHomeDir() string { +func UserHomeDir() (string, error) { + env, enverr := "HOME", "$HOME" switch runtime.GOOS { case "windows": - return Getenv("USERPROFILE") + env, enverr = "USERPROFILE", "%userprofile%" case "plan9": - return Getenv("home") + env, enverr = "home", "$home" case "nacl", "android": - return "/" + return "/", nil case "darwin": if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" { - return "/" + return "/", nil } - fallthrough - default: - return Getenv("HOME") } + if v := Getenv(env); v != "" { + return v, nil + } + return "", errors.New(enverr + " is not defined") } // Chmod changes the mode of the named file to mode. diff --git a/src/os/os_test.go b/src/os/os_test.go index d838272215..9c4d5dada9 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -2300,9 +2300,12 @@ func TestDoubleCloseError(t *testing.T) { } func TestUserHomeDir(t *testing.T) { - dir := UserHomeDir() - if dir == "" { - t.Fatal("UserHomeDir returned an empty string") + dir, err := UserHomeDir() + if dir == "" && err == nil { + t.Fatal("UserHomeDir returned an empty string but no error") + } + if err != nil { + t.Skipf("UserHomeDir failed: %v", err) } fi, err := Stat(dir) if err != nil {