From e4a8fb0fa04a95876e8c5cc6ce6850a06d3d670a Mon Sep 17 00:00:00 2001 From: Josh Kraft Date: Wed, 16 Aug 2023 18:36:46 -0600 Subject: [PATCH] os: add tests for UserCacheDir and UserConfigDir Adds basic test scenarios for UserCacheDir and UserConfigDir. Fixes #57638 Change-Id: Ieb86e95faff44287bfa13daa0cb26e7b5401373b Reviewed-on: https://go-review.googlesource.com/c/go/+/520262 Run-TryBot: Bryan Mills TryBot-Bypass: Bryan Mills Reviewed-by: Matthew Dempsky Reviewed-by: Bryan Mills Auto-Submit: Bryan Mills --- misc/go_android_exec/main.go | 1 + src/os/os_test.go | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/misc/go_android_exec/main.go b/misc/go_android_exec/main.go index 554810c55d..33b669399c 100644 --- a/misc/go_android_exec/main.go +++ b/misc/go_android_exec/main.go @@ -204,6 +204,7 @@ func runMain() (int, error) { `; export GOPROXY=` + os.Getenv("GOPROXY") + `; export GOCACHE="` + deviceRoot + `/gocache"` + `; export PATH="` + deviceGoroot + `/bin":$PATH` + + `; export HOME="` + deviceRoot + `/home"` + `; cd "` + deviceCwd + `"` + "; '" + deviceBin + "' " + strings.Join(os.Args[2:], " ") code, err := adbRun(cmd) diff --git a/src/os/os_test.go b/src/os/os_test.go index 94c3ad04f3..a9de3f1014 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -2823,6 +2823,53 @@ func TestDoubleCloseError(t *testing.T) { t.Run("dir", testDoubleCloseError(sfdir)) } +func TestUserCacheDir(t *testing.T) { + t.Parallel() + + dir, err := UserCacheDir() + if err != nil { + t.Skipf("skipping: %v", err) + } + if dir == "" { + t.Fatalf("UserCacheDir returned %q; want non-empty path or error", dir) + } + + if err := MkdirAll(dir, 0777); err != nil { + t.Fatalf("could not create UserCacheDir: %v", err) + } + d, err := MkdirTemp(dir, "TestUserCacheDir") + if err != nil { + t.Fatalf("could not create a directory in UserCacheDir: %v", err) + } + if err := Remove(d); err != nil { + t.Fatal(err) + } +} + +func TestUserConfigDir(t *testing.T) { + t.Parallel() + + dir, err := UserConfigDir() + if err != nil { + t.Skipf("skipping: %v", err) + } + if dir == "" { + t.Fatalf("UserConfigDir returned %q; want non-empty path or error", dir) + } + + if err := MkdirAll(dir, 0777); err != nil { + t.Fatalf("could not create UserConfigDir: %v", err) + } + + d, err := MkdirTemp(dir, "TestUserConfigDir") + if err != nil { + t.Fatalf("could not create a directory in UserConfigDir: %v", err) + } + if err := Remove(d); err != nil { + t.Fatal(err) + } +} + func TestUserHomeDir(t *testing.T) { t.Parallel()