Skip to content

Commit bb4ea80

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
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 <[email protected]> Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent dfd2ddd commit bb4ea80

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/os/file.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ func UserConfigDir() (string, error) {
486486
// On Unix, including macOS, it returns the $HOME environment variable.
487487
// On Windows, it returns %USERPROFILE%.
488488
// On Plan 9, it returns the $home environment variable.
489+
//
490+
// If the expected variable is not set in the environment, UserHomeDir
491+
// returns either a platform-specific default value or a non-nil error.
489492
func UserHomeDir() (string, error) {
490493
env, enverr := "HOME", "$HOME"
491494
switch runtime.GOOS {

src/os/os_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2639,10 +2639,20 @@ func TestUserHomeDir(t *testing.T) {
26392639
t.Fatal("UserHomeDir returned an empty string but no error")
26402640
}
26412641
if err != nil {
2642-
t.Skipf("UserHomeDir failed: %v", err)
2642+
// UserHomeDir may return a non-nil error if the environment variable
2643+
// for the home directory is empty or unset in the environment.
2644+
t.Skipf("skipping: %v", err)
26432645
}
2646+
26442647
fi, err := Stat(dir)
26452648
if err != nil {
2649+
if os.IsNotExist(err) {
2650+
// The user's home directory has a well-defined location, but does not
2651+
// exist. (Maybe nothing has written to it yet? That could happen, for
2652+
// example, on minimal VM images used for CI testing.)
2653+
t.Log(err)
2654+
return
2655+
}
26462656
t.Fatal(err)
26472657
}
26482658
if !fi.IsDir() {

0 commit comments

Comments
 (0)