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