83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestEnsurePathContainsSiblings_PrependsExecDir(t *testing.T) {
|
|
// Drop the test exec dir from PATH to prove it gets re-added.
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Skip("no os.Executable")
|
|
}
|
|
if real, err := filepath.EvalSymlinks(exe); err == nil {
|
|
exe = real
|
|
}
|
|
execDir := filepath.Dir(exe)
|
|
|
|
t.Setenv("PATH", "/usr/bin:/bin")
|
|
t.Setenv("GOBIN", "")
|
|
t.Setenv("GOPATH", "")
|
|
|
|
EnsurePathContainsSiblings()
|
|
|
|
dirs := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
|
|
found := false
|
|
for i, d := range dirs {
|
|
if d == execDir {
|
|
found = true
|
|
if i != 0 {
|
|
t.Fatalf("execDir should be first, got at index %d: %v", i, dirs)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("execDir %s not in PATH: %v", execDir, dirs)
|
|
}
|
|
}
|
|
|
|
func TestEnsurePathContainsSiblings_Idempotent(t *testing.T) {
|
|
t.Setenv("PATH", "/usr/bin:/bin")
|
|
t.Setenv("GOBIN", "")
|
|
t.Setenv("GOPATH", "")
|
|
EnsurePathContainsSiblings()
|
|
first := os.Getenv("PATH")
|
|
EnsurePathContainsSiblings()
|
|
if os.Getenv("PATH") != first {
|
|
t.Fatalf("second call mutated PATH:\n before: %s\n after: %s", first, os.Getenv("PATH"))
|
|
}
|
|
}
|
|
|
|
func TestEnsurePathContainsSiblings_HomeGoBin(t *testing.T) {
|
|
home := t.TempDir()
|
|
gobin := filepath.Join(home, "go", "bin")
|
|
if err := os.MkdirAll(gobin, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("PATH", "/usr/bin")
|
|
t.Setenv("GOBIN", "")
|
|
t.Setenv("GOPATH", "")
|
|
EnsurePathContainsSiblings()
|
|
if !strings.Contains(os.Getenv("PATH"), gobin) {
|
|
t.Fatalf("expected $HOME/go/bin (%s) in PATH, got %s", gobin, os.Getenv("PATH"))
|
|
}
|
|
}
|
|
|
|
func TestEnsurePathContainsSiblings_SkipsNonexistent(t *testing.T) {
|
|
t.Setenv("HOME", "/this/does/not/exist")
|
|
t.Setenv("PATH", "/usr/bin")
|
|
t.Setenv("GOBIN", "/also/does/not/exist")
|
|
t.Setenv("GOPATH", "/yet/another/missing/path")
|
|
EnsurePathContainsSiblings()
|
|
got := os.Getenv("PATH")
|
|
for _, missing := range []string{"/also/does/not/exist", "/yet/another/missing/path/bin", "/this/does/not/exist/go/bin"} {
|
|
if strings.Contains(got, missing) {
|
|
t.Fatalf("PATH should not contain missing dir %q: %s", missing, got)
|
|
}
|
|
}
|
|
}
|