1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-27 16:28:27 +02:00

check the default user cachedir if GOCACHE is not explicitly set

This commit is contained in:
Gani Georgiev
2025-09-06 21:19:16 +03:00
parent 40f2ba731c
commit eda90d4555
4 changed files with 78 additions and 51 deletions

View File

@@ -63,17 +63,3 @@ func YesNoPrompt(message string, fallback bool) bool {
}
}
}
// IsProbablyGoRun loosely checks if the current executable is running
// as a result of "go run".
func IsProbablyGoRun() bool {
runDirs := []string{os.TempDir(), os.Getenv("GOCACHE")}
for _, dir := range runDirs {
if dir != "" && strings.HasPrefix(os.Args[0], dir) {
return true
}
}
return false
}

View File

@@ -64,40 +64,3 @@ func TestYesNoPrompt(t *testing.T) {
})
}
}
func TestIsProbablyGoRun(t *testing.T) {
scenarios := []struct {
arg string
envs map[string]string
expected bool
}{
{"", nil, false},
{"a/b/c", nil, false},
{"/a/b/c", nil, false},
{"/a/b/c", map[string]string{"GOCACHE": "/b/"}, false},
{"/a/b/c", map[string]string{"GOCACHE": "/a/"}, true},
{os.TempDir() + "/a/b/c", nil, true},
}
originalArgs := os.Args
defer func() {
os.Args = originalArgs
}()
for _, s := range scenarios {
t.Run(s.arg, func(t *testing.T) {
os.Args = []string{s.arg}
for k, v := range s.envs {
defer os.Setenv(k, os.Getenv(k))
os.Setenv(k, v)
}
result := osutils.IsProbablyGoRun()
if result != s.expected {
t.Fatalf("Expected %v, got %v", s.expected, result)
}
})
}
}

33
tools/osutils/run.go Normal file
View File

@@ -0,0 +1,33 @@
package osutils
import (
"os"
"strings"
)
var runDirs = []string{os.TempDir(), cacheDir()}
// IsProbablyGoRun loosely checks if the current executable is running
// as a result of "go run".
func IsProbablyGoRun() bool {
for _, dir := range runDirs {
if dir != "" && strings.HasPrefix(os.Args[0], dir) {
return true
}
}
return false
}
func cacheDir() string {
dir := os.Getenv("GOCACHE")
if dir == "off" {
return ""
}
if dir == "" {
dir, _ = os.UserCacheDir()
}
return dir
}

45
tools/osutils/run_test.go Normal file
View File

@@ -0,0 +1,45 @@
package osutils
import (
"os"
"strconv"
"testing"
)
func TestIsProbablyGoRun(t *testing.T) {
scenarios := []struct {
arg0 string
runDirs []string
expected bool
}{
{"", nil, false},
{"/a/b", nil, false},
{"/a/b", []string{""}, false},
{"/a/b", []string{"/b/"}, false},
{"/a/b", []string{"/a/"}, true},
{"/a/b", []string{"", "/b/", "/a/"}, true},
}
originalArgs := os.Args
defer func() {
os.Args = originalArgs
}()
originalRunDirs := runDirs
defer func() {
runDirs = originalRunDirs
}()
for i, s := range scenarios {
t.Run(strconv.Itoa(i)+"_"+s.arg0, func(t *testing.T) {
os.Args = []string{s.arg0}
runDirs = s.runDirs
result := IsProbablyGoRun()
if result != s.expected {
t.Fatalf("Expected %v, got %v", s.expected, result)
}
})
}
}