Commit 649b8937 authored by David Heuschmann's avatar David Heuschmann Committed by Ian Lance Taylor

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 <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 6d5caf38
......@@ -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,
......
......@@ -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.
......
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment