]> Cypherpunks repositories - gostls13.git/commitdiff
os: return an error from UserHomeDir to match UserCacheDir
authorDavid Heuschmann <heuschmann.d@gmail.com>
Tue, 20 Nov 2018 11:30:14 +0000 (12:30 +0100)
committerIan Lance Taylor <iant@golang.org>
Thu, 22 Nov 2018 20:49:16 +0000 (20:49 +0000)
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 <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/crypto/x509/root_darwin.go
src/os/file.go
src/os/os_test.go

index ae69a2faddd847cb1a9c2851e4581e4b60c0f824..4a02c075960c32f246351f6dcaa3e7411a9a0d60 100644 (file)
@@ -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,
index d9c5c57c17354cc5cfd37f543732998725098fc1..9b7863e9b6b84a06d181b848357892f9aee2c854 100644 (file)
@@ -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.
index d838272215594158caac2b2f1b34452521b8075a..9c4d5dada9a106b9c20c593bd157a88b804e6371 100644 (file)
@@ -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 {