| | |
| | |
| | |
| |
|
| | package exec_test |
| |
|
| | import ( |
| | "errors" |
| | "internal/testenv" |
| | "os" |
| | . "os/exec" |
| | "path/filepath" |
| | "runtime" |
| | "strings" |
| | "testing" |
| | ) |
| |
|
| | var pathVar string = func() string { |
| | if runtime.GOOS == "plan9" { |
| | return "path" |
| | } |
| | return "PATH" |
| | }() |
| |
|
| | func TestLookPath(t *testing.T) { |
| | testenv.MustHaveExec(t) |
| | |
| |
|
| | tmpDir := filepath.Join(t.TempDir(), "testdir") |
| | if err := os.Mkdir(tmpDir, 0777); err != nil { |
| | t.Fatal(err) |
| | } |
| |
|
| | executable := "execabs-test" |
| | if runtime.GOOS == "windows" { |
| | executable += ".exe" |
| | } |
| | if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0777); err != nil { |
| | t.Fatal(err) |
| | } |
| | t.Chdir(tmpDir) |
| | t.Logf(". is %#q", tmpDir) |
| |
|
| | origPath := os.Getenv(pathVar) |
| |
|
| | |
| | |
| | for _, errdot := range []string{"1", "0"} { |
| | t.Run("GODEBUG=execerrdot="+errdot, func(t *testing.T) { |
| | t.Setenv("GODEBUG", "execerrdot="+errdot+",execwait=2") |
| | for _, dir := range []string{".", "../testdir"} { |
| | t.Run(pathVar+"="+dir, func(t *testing.T) { |
| | t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath) |
| | good := dir + "/execabs-test" |
| | if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { |
| | t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good) |
| | } |
| | if runtime.GOOS == "windows" { |
| | good = dir + `\execabs-test` |
| | if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { |
| | t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good) |
| | } |
| | } |
| |
|
| | _, err := LookPath("execabs-test") |
| | if errdot == "1" { |
| | if err == nil { |
| | t.Fatalf("LookPath didn't fail when finding a non-relative path") |
| | } else if !errors.Is(err, ErrDot) { |
| | t.Fatalf("LookPath returned unexpected error: want Is ErrDot, got %q", err) |
| | } |
| | } else { |
| | if err != nil { |
| | t.Fatalf("LookPath failed unexpectedly: %v", err) |
| | } |
| | } |
| |
|
| | cmd := Command("execabs-test") |
| | if errdot == "1" { |
| | if cmd.Err == nil { |
| | t.Fatalf("Command didn't fail when finding a non-relative path") |
| | } else if !errors.Is(cmd.Err, ErrDot) { |
| | t.Fatalf("Command returned unexpected error: want Is ErrDot, got %q", cmd.Err) |
| | } |
| | cmd.Err = nil |
| | } else { |
| | if cmd.Err != nil { |
| | t.Fatalf("Command failed unexpectedly: %v", err) |
| | } |
| | } |
| |
|
| | |
| | |
| | if err := cmd.Run(); err == nil { |
| | t.Fatalf("Run did not fail: expected exec error") |
| | } else if errors.Is(err, ErrDot) { |
| | t.Fatalf("Run returned unexpected error ErrDot: want error like ENOEXEC: %q", err) |
| | } |
| | }) |
| | } |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | t.Run(pathVar+"=$PWD", func(t *testing.T) { |
| | t.Setenv(pathVar, tmpDir+string(filepath.ListSeparator)+origPath) |
| | good := filepath.Join(tmpDir, "execabs-test") |
| | if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { |
| | t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, good, found, err, good) |
| | } |
| |
|
| | if found, err := LookPath("execabs-test"); err != nil || !strings.HasPrefix(found, good) { |
| | t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, "execabs-test", found, err, good) |
| | } |
| |
|
| | cmd := Command("execabs-test") |
| | if cmd.Err != nil { |
| | t.Fatalf("Command(%#q).Err = %v; want nil", "execabs-test", cmd.Err) |
| | } |
| | }) |
| |
|
| | t.Run(pathVar+"=$OTHER", func(t *testing.T) { |
| | |
| | |
| | |
| | |
| | wantErrDot := false |
| | t.Setenv(pathVar, "") |
| | if found, err := LookPath("execabs-test"); errors.Is(err, ErrDot) { |
| | wantErrDot = true |
| | } else if err == nil { |
| | t.Fatalf(`with PATH='', LookPath(%#q) = %#q; want non-nil error`, "execabs-test", found) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | dir := t.TempDir() |
| | executable := "execabs-test" |
| | if runtime.GOOS == "windows" { |
| | executable += ".exe" |
| | } |
| | if err := os.WriteFile(filepath.Join(dir, executable), []byte{1, 2, 3}, 0777); err != nil { |
| | t.Fatal(err) |
| | } |
| | t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath) |
| |
|
| | found, err := LookPath("execabs-test") |
| | if wantErrDot { |
| | wantFound := filepath.Join(".", executable) |
| | if found != wantFound || !errors.Is(err, ErrDot) { |
| | t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, Is ErrDot`, "execabs-test", found, err, wantFound) |
| | } |
| | } else { |
| | wantFound := filepath.Join(dir, executable) |
| | if found != wantFound || err != nil { |
| | t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, nil`, "execabs-test", found, err, wantFound) |
| | } |
| | } |
| | }) |
| |
|
| | checker := func(test string) func(t *testing.T) { |
| | return func(t *testing.T) { |
| | t.Helper() |
| | t.Logf("PATH=%s", os.Getenv("PATH")) |
| | p, err := LookPath(test) |
| | if err == nil { |
| | t.Errorf("%q: error expected, got nil", test) |
| | } |
| | if p != "" { |
| | t.Errorf("%q: path returned should be \"\". Got %q", test, p) |
| | } |
| | } |
| | } |
| |
|
| | |
| | t.Run(pathVar+"=$OTHER2", func(t *testing.T) { |
| | t.Run("empty", checker("")) |
| | t.Run("dot", checker(".")) |
| | t.Run("dotdot1", checker("abc/..")) |
| | t.Run("dotdot2", checker("..")) |
| | }) |
| |
|
| | |
| | t.Run(pathVar+"=exe", func(t *testing.T) { |
| | |
| | |
| | t.Setenv(pathVar, testenv.Executable(t)) |
| | t.Run("empty", checker("")) |
| | t.Run("dot", checker(".")) |
| | t.Run("dotdot1", checker("abc/..")) |
| | t.Run("dotdot2", checker("..")) |
| | }) |
| |
|
| | |
| | t.Run(pathVar+"=exe/xx", func(t *testing.T) { |
| | |
| | |
| | t.Setenv(pathVar, filepath.Join(testenv.Executable(t), "xx")) |
| | t.Run("empty", checker("")) |
| | t.Run("dot", checker(".")) |
| | t.Run("dotdot1", checker("abc/..")) |
| | t.Run("dotdot2", checker("..")) |
| | }) |
| | } |
| |
|