Source file
src/os/exec/dot_test.go
Documentation: os/exec
1
2
3
4
5 package exec_test
6
7 import (
8 "errors"
9 "internal/testenv"
10 "os"
11 . "os/exec"
12 "path/filepath"
13 "runtime"
14 "strings"
15 "testing"
16 )
17
18 var pathVar string = func() string {
19 if runtime.GOOS == "plan9" {
20 return "path"
21 }
22 return "PATH"
23 }()
24
25 func TestLookPath(t *testing.T) {
26 testenv.MustHaveExec(t)
27
28
29 tmpDir := filepath.Join(t.TempDir(), "testdir")
30 if err := os.Mkdir(tmpDir, 0777); err != nil {
31 t.Fatal(err)
32 }
33
34 executable := "execabs-test"
35 if runtime.GOOS == "windows" {
36 executable += ".exe"
37 }
38 if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0777); err != nil {
39 t.Fatal(err)
40 }
41 chdir(t, tmpDir)
42 t.Setenv("PWD", tmpDir)
43 t.Logf(". is %#q", tmpDir)
44
45 origPath := os.Getenv(pathVar)
46
47
48
49 for _, errdot := range []string{"1", "0"} {
50 t.Run("GODEBUG=execerrdot="+errdot, func(t *testing.T) {
51 t.Setenv("GODEBUG", "execerrdot="+errdot+",execwait=2")
52 for _, dir := range []string{".", "../testdir"} {
53 t.Run(pathVar+"="+dir, func(t *testing.T) {
54 t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath)
55 good := dir + "/execabs-test"
56 if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) {
57 t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good)
58 }
59 if runtime.GOOS == "windows" {
60 good = dir + `\execabs-test`
61 if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) {
62 t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good)
63 }
64 }
65
66 _, err := LookPath("execabs-test")
67 if errdot == "1" {
68 if err == nil {
69 t.Fatalf("LookPath didn't fail when finding a non-relative path")
70 } else if !errors.Is(err, ErrDot) {
71 t.Fatalf("LookPath returned unexpected error: want Is ErrDot, got %q", err)
72 }
73 } else {
74 if err != nil {
75 t.Fatalf("LookPath failed unexpectedly: %v", err)
76 }
77 }
78
79 cmd := Command("execabs-test")
80 if errdot == "1" {
81 if cmd.Err == nil {
82 t.Fatalf("Command didn't fail when finding a non-relative path")
83 } else if !errors.Is(cmd.Err, ErrDot) {
84 t.Fatalf("Command returned unexpected error: want Is ErrDot, got %q", cmd.Err)
85 }
86 cmd.Err = nil
87 } else {
88 if cmd.Err != nil {
89 t.Fatalf("Command failed unexpectedly: %v", err)
90 }
91 }
92
93
94
95 if err := cmd.Run(); err == nil {
96 t.Fatalf("Run did not fail: expected exec error")
97 } else if errors.Is(err, ErrDot) {
98 t.Fatalf("Run returned unexpected error ErrDot: want error like ENOEXEC: %q", err)
99 }
100 })
101 }
102 })
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 t.Run(pathVar+"=$PWD", func(t *testing.T) {
122 t.Setenv(pathVar, tmpDir+string(filepath.ListSeparator)+origPath)
123 good := filepath.Join(tmpDir, "execabs-test")
124 if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) {
125 t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, good, found, err, good)
126 }
127
128 if found, err := LookPath("execabs-test"); err != nil || !strings.HasPrefix(found, good) {
129 t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, "execabs-test", found, err, good)
130 }
131
132 cmd := Command("execabs-test")
133 if cmd.Err != nil {
134 t.Fatalf("Command(%#q).Err = %v; want nil", "execabs-test", cmd.Err)
135 }
136 })
137
138 t.Run(pathVar+"=$OTHER", func(t *testing.T) {
139
140
141
142
143 wantErrDot := false
144 t.Setenv(pathVar, "")
145 if found, err := LookPath("execabs-test"); errors.Is(err, ErrDot) {
146 wantErrDot = true
147 } else if err == nil {
148 t.Fatalf(`with PATH='', LookPath(%#q) = %#q; want non-nil error`, "execabs-test", found)
149 }
150
151
152
153
154
155
156
157
158 dir := t.TempDir()
159 executable := "execabs-test"
160 if runtime.GOOS == "windows" {
161 executable += ".exe"
162 }
163 if err := os.WriteFile(filepath.Join(dir, executable), []byte{1, 2, 3}, 0777); err != nil {
164 t.Fatal(err)
165 }
166 t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath)
167
168 found, err := LookPath("execabs-test")
169 if wantErrDot {
170 wantFound := filepath.Join(".", executable)
171 if found != wantFound || !errors.Is(err, ErrDot) {
172 t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, Is ErrDot`, "execabs-test", found, err, wantFound)
173 }
174 } else {
175 wantFound := filepath.Join(dir, executable)
176 if found != wantFound || err != nil {
177 t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, nil`, "execabs-test", found, err, wantFound)
178 }
179 }
180 })
181 }
182
View as plain text