diff --git a/tools/osutils/cmd.go b/tools/osutils/cmd.go index 5e7ecc33..d21f8865 100644 --- a/tools/osutils/cmd.go +++ b/tools/osutils/cmd.go @@ -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 -} diff --git a/tools/osutils/cmd_test.go b/tools/osutils/cmd_test.go index 8d15012c..38beeab1 100644 --- a/tools/osutils/cmd_test.go +++ b/tools/osutils/cmd_test.go @@ -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) - } - }) - } -} diff --git a/tools/osutils/run.go b/tools/osutils/run.go new file mode 100644 index 00000000..b21dcdf8 --- /dev/null +++ b/tools/osutils/run.go @@ -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 +} diff --git a/tools/osutils/run_test.go b/tools/osutils/run_test.go new file mode 100644 index 00000000..c7265569 --- /dev/null +++ b/tools/osutils/run_test.go @@ -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) + } + }) + } +}