1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 // hasExec reports whether the current system can start new processes
19 // using os.StartProcess or (more commonly) exec.Command.
20 // Copied from internal/testenv.HasExec
29 // mustHaveExec checks that the current system can start new processes
30 // using os.StartProcess or (more commonly) exec.Command.
31 // If not, mustHaveExec calls t.Skip with an explanation.
32 // Copied from internal/testenv.MustHaveExec
33 func mustHaveExec(t testing.TB) {
35 t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH)
39 func TestFixCmd(t *testing.T) {
40 cmd := &exec.Cmd{Path: "hello"}
43 t.Errorf("fixCmd didn't clear cmd.Path")
45 expectedErr := fmt.Sprintf("hello resolves to executable in current directory (.%chello)", filepath.Separator)
46 if err := cmd.Run(); err == nil {
47 t.Fatal("Command.Run didn't fail")
48 } else if err.Error() != expectedErr {
49 t.Fatalf("Command.Run returned unexpected error: want %q, got %q", expectedErr, err.Error())
53 func TestCommand(t *testing.T) {
56 for _, cmd := range []func(string) *Cmd{
57 func(s string) *Cmd { return Command(s) },
58 func(s string) *Cmd { return CommandContext(context.Background(), s) },
60 tmpDir, err := ioutil.TempDir("", "execabs-test")
62 t.Fatalf("ioutil.TempDir failed: %s", err)
64 defer os.RemoveAll(tmpDir)
65 executable := "execabs-test"
66 if runtime.GOOS == "windows" {
69 if err = ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
70 t.Fatalf("ioutil.WriteFile failed: %s", err)
72 cwd, err := os.Getwd()
74 t.Fatalf("os.Getwd failed: %s", err)
77 if err = os.Chdir(tmpDir); err != nil {
78 t.Fatalf("os.Chdir failed: %s", err)
80 if runtime.GOOS != "windows" {
81 // add "." to PATH so that exec.LookPath looks in the current directory on
82 // non-windows platforms as well
83 origPath := os.Getenv("PATH")
84 defer os.Setenv("PATH", origPath)
85 os.Setenv("PATH", fmt.Sprintf(".:%s", origPath))
87 expectedErr := fmt.Sprintf("execabs-test resolves to executable in current directory (.%c%s)", filepath.Separator, executable)
88 if err = cmd("execabs-test").Run(); err == nil {
89 t.Fatalf("Command.Run didn't fail when exec.LookPath returned a relative path")
90 } else if err.Error() != expectedErr {
91 t.Errorf("Command.Run returned unexpected error: want %q, got %q", expectedErr, err.Error())
96 func TestLookPath(t *testing.T) {
99 tmpDir, err := ioutil.TempDir("", "execabs-test")
101 t.Fatalf("ioutil.TempDir failed: %s", err)
103 defer os.RemoveAll(tmpDir)
104 executable := "execabs-test"
105 if runtime.GOOS == "windows" {
108 if err = ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
109 t.Fatalf("ioutil.WriteFile failed: %s", err)
111 cwd, err := os.Getwd()
113 t.Fatalf("os.Getwd failed: %s", err)
116 if err = os.Chdir(tmpDir); err != nil {
117 t.Fatalf("os.Chdir failed: %s", err)
119 if runtime.GOOS != "windows" {
120 // add "." to PATH so that exec.LookPath looks in the current directory on
121 // non-windows platforms as well
122 origPath := os.Getenv("PATH")
123 defer os.Setenv("PATH", origPath)
124 os.Setenv("PATH", fmt.Sprintf(".:%s", origPath))
126 expectedErr := fmt.Sprintf("execabs-test resolves to executable in current directory (.%c%s)", filepath.Separator, executable)
127 if _, err := LookPath("execabs-test"); err == nil {
128 t.Fatalf("LookPath didn't fail when finding a non-relative path")
129 } else if err.Error() != expectedErr {
130 t.Errorf("LookPath returned unexpected error: want %q, got %q", expectedErr, err.Error())