.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / sys@v0.0.0-20210124154548-22da62e12c0c / execabs / execabs_test.go
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.
4
5 package execabs
6
7 import (
8         "context"
9         "fmt"
10         "io/ioutil"
11         "os"
12         "os/exec"
13         "path/filepath"
14         "runtime"
15         "testing"
16 )
17
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
21 func hasExec() bool {
22         switch runtime.GOOS {
23         case "js", "ios":
24                 return false
25         }
26         return true
27 }
28
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) {
34         if !hasExec() {
35                 t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH)
36         }
37 }
38
39 func TestFixCmd(t *testing.T) {
40         cmd := &exec.Cmd{Path: "hello"}
41         fixCmd("hello", cmd)
42         if cmd.Path != "" {
43                 t.Errorf("fixCmd didn't clear cmd.Path")
44         }
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())
50         }
51 }
52
53 func TestCommand(t *testing.T) {
54         mustHaveExec(t)
55
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) },
59         } {
60                 tmpDir, err := ioutil.TempDir("", "execabs-test")
61                 if err != nil {
62                         t.Fatalf("ioutil.TempDir failed: %s", err)
63                 }
64                 defer os.RemoveAll(tmpDir)
65                 executable := "execabs-test"
66                 if runtime.GOOS == "windows" {
67                         executable += ".exe"
68                 }
69                 if err = ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
70                         t.Fatalf("ioutil.WriteFile failed: %s", err)
71                 }
72                 cwd, err := os.Getwd()
73                 if err != nil {
74                         t.Fatalf("os.Getwd failed: %s", err)
75                 }
76                 defer os.Chdir(cwd)
77                 if err = os.Chdir(tmpDir); err != nil {
78                         t.Fatalf("os.Chdir failed: %s", err)
79                 }
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))
86                 }
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())
92                 }
93         }
94 }
95
96 func TestLookPath(t *testing.T) {
97         mustHaveExec(t)
98
99         tmpDir, err := ioutil.TempDir("", "execabs-test")
100         if err != nil {
101                 t.Fatalf("ioutil.TempDir failed: %s", err)
102         }
103         defer os.RemoveAll(tmpDir)
104         executable := "execabs-test"
105         if runtime.GOOS == "windows" {
106                 executable += ".exe"
107         }
108         if err = ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
109                 t.Fatalf("ioutil.WriteFile failed: %s", err)
110         }
111         cwd, err := os.Getwd()
112         if err != nil {
113                 t.Fatalf("os.Getwd failed: %s", err)
114         }
115         defer os.Chdir(cwd)
116         if err = os.Chdir(tmpDir); err != nil {
117                 t.Fatalf("os.Chdir failed: %s", err)
118         }
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))
125         }
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())
131         }
132 }