aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-12-08 14:10:59 -0500
committerGopher Robot <gobot@golang.org>2023-01-31 18:47:06 +0000
commitbb4ea80befa5f91aa50ccb170e0abdcbc0d01627 (patch)
tree0b9e70c931f0717543b4a3d6625354d96c9de76a
parentdfd2ddd6717acb16b43ecde5150a7d8916ac5484 (diff)
downloadgo-bb4ea80befa5f91aa50ccb170e0abdcbc0d01627.tar.xz
os: allow $HOME to not exist in TestUserHomeDir
This seems like a cleaner fix for the situation Debian has patched around with https://sources.debian.org/patches/golang-1.19/1.19.3-1/0001-Disable-test-for-UserHomeDir.patch/ Tested with 'GOCACHE=$(go env GOCACHE) HOME=/home/nobody go test os'. Change-Id: I9fd00da38b86df2edfcd8cb87629e2875573903e Reviewed-on: https://go-review.googlesource.com/c/go/+/456126 Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
-rw-r--r--src/os/file.go3
-rw-r--r--src/os/os_test.go12
2 files changed, 14 insertions, 1 deletions
diff --git a/src/os/file.go b/src/os/file.go
index 3d71ac068e..c41adc7da6 100644
--- a/src/os/file.go
+++ b/src/os/file.go
@@ -486,6 +486,9 @@ func UserConfigDir() (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.
+//
+// If the expected variable is not set in the environment, UserHomeDir
+// returns either a platform-specific default value or a non-nil error.
func UserHomeDir() (string, error) {
env, enverr := "HOME", "$HOME"
switch runtime.GOOS {
diff --git a/src/os/os_test.go b/src/os/os_test.go
index 7d39eb3e02..a8488a11f8 100644
--- a/src/os/os_test.go
+++ b/src/os/os_test.go
@@ -2639,10 +2639,20 @@ func TestUserHomeDir(t *testing.T) {
t.Fatal("UserHomeDir returned an empty string but no error")
}
if err != nil {
- t.Skipf("UserHomeDir failed: %v", err)
+ // UserHomeDir may return a non-nil error if the environment variable
+ // for the home directory is empty or unset in the environment.
+ t.Skipf("skipping: %v", err)
}
+
fi, err := Stat(dir)
if err != nil {
+ if os.IsNotExist(err) {
+ // The user's home directory has a well-defined location, but does not
+ // exist. (Maybe nothing has written to it yet? That could happen, for
+ // example, on minimal VM images used for CI testing.)
+ t.Log(err)
+ return
+ }
t.Fatal(err)
}
if !fi.IsDir() {