You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-11-27 08:27:06 +02:00
check the default user cachedir if GOCACHE is not explicitly set
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
33
tools/osutils/run.go
Normal 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
45
tools/osutils/run_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user