github.com/AndrienkoAleksandr/go@v0.0.19/src/os/exec/dot_test.go (about) 1 // Copyright 2022 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 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 // Not parallel: uses os.Chdir and t.Setenv. 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 cwd, err := os.Getwd() 42 if err != nil { 43 t.Fatal(err) 44 } 45 defer func() { 46 if err := os.Chdir(cwd); err != nil { 47 panic(err) 48 } 49 }() 50 if err = os.Chdir(tmpDir); err != nil { 51 t.Fatal(err) 52 } 53 t.Setenv("PWD", tmpDir) 54 t.Logf(". is %#q", tmpDir) 55 56 origPath := os.Getenv(pathVar) 57 58 // Add "." to PATH so that exec.LookPath looks in the current directory on all systems. 59 // And try to trick it with "../testdir" too. 60 for _, errdot := range []string{"1", "0"} { 61 t.Run("GODEBUG=execerrdot="+errdot, func(t *testing.T) { 62 t.Setenv("GODEBUG", "execerrdot="+errdot+",execwait=2") 63 for _, dir := range []string{".", "../testdir"} { 64 t.Run(pathVar+"="+dir, func(t *testing.T) { 65 t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath) 66 good := dir + "/execabs-test" 67 if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { 68 t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good) 69 } 70 if runtime.GOOS == "windows" { 71 good = dir + `\execabs-test` 72 if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { 73 t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good) 74 } 75 } 76 77 _, err := LookPath("execabs-test") 78 if errdot == "1" { 79 if err == nil { 80 t.Fatalf("LookPath didn't fail when finding a non-relative path") 81 } else if !errors.Is(err, ErrDot) { 82 t.Fatalf("LookPath returned unexpected error: want Is ErrDot, got %q", err) 83 } 84 } else { 85 if err != nil { 86 t.Fatalf("LookPath failed unexpectedly: %v", err) 87 } 88 } 89 90 cmd := Command("execabs-test") 91 if errdot == "1" { 92 if cmd.Err == nil { 93 t.Fatalf("Command didn't fail when finding a non-relative path") 94 } else if !errors.Is(cmd.Err, ErrDot) { 95 t.Fatalf("Command returned unexpected error: want Is ErrDot, got %q", cmd.Err) 96 } 97 cmd.Err = nil 98 } else { 99 if cmd.Err != nil { 100 t.Fatalf("Command failed unexpectedly: %v", err) 101 } 102 } 103 104 // Clearing cmd.Err should let the execution proceed, 105 // and it should fail because it's not a valid binary. 106 if err := cmd.Run(); err == nil { 107 t.Fatalf("Run did not fail: expected exec error") 108 } else if errors.Is(err, ErrDot) { 109 t.Fatalf("Run returned unexpected error ErrDot: want error like ENOEXEC: %q", err) 110 } 111 }) 112 } 113 }) 114 } 115 116 // Test the behavior when the first entry in PATH is an absolute name for the 117 // current directory. 118 // 119 // On Windows, "." may or may not be implicitly included before the explicit 120 // %PATH%, depending on the process environment; 121 // see https://go.dev/issue/4394. 122 // 123 // If the relative entry from "." resolves to the same executable as what 124 // would be resolved from an absolute entry in %PATH% alone, LookPath should 125 // return the absolute version of the path instead of ErrDot. 126 // (See https://go.dev/issue/53536.) 127 // 128 // If PATH does not implicitly include "." (such as on Unix platforms, or on 129 // Windows configured with NoDefaultCurrentDirectoryInExePath), then this 130 // lookup should succeed regardless of the behavior for ".", so it may be 131 // useful to run as a control case even on those platforms. 132 t.Run(pathVar+"=$PWD", func(t *testing.T) { 133 t.Setenv(pathVar, tmpDir+string(filepath.ListSeparator)+origPath) 134 good := filepath.Join(tmpDir, "execabs-test") 135 if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { 136 t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, good, found, err, good) 137 } 138 139 if found, err := LookPath("execabs-test"); err != nil || !strings.HasPrefix(found, good) { 140 t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, "execabs-test", found, err, good) 141 } 142 143 cmd := Command("execabs-test") 144 if cmd.Err != nil { 145 t.Fatalf("Command(%#q).Err = %v; want nil", "execabs-test", cmd.Err) 146 } 147 }) 148 149 t.Run(pathVar+"=$OTHER", func(t *testing.T) { 150 // Control case: if the lookup returns ErrDot when PATH is empty, then we 151 // know that PATH implicitly includes ".". If it does not, then we don't 152 // expect to see ErrDot at all in this test (because the path will be 153 // unambiguously absolute). 154 wantErrDot := false 155 t.Setenv(pathVar, "") 156 if found, err := LookPath("execabs-test"); errors.Is(err, ErrDot) { 157 wantErrDot = true 158 } else if err == nil { 159 t.Fatalf(`with PATH='', LookPath(%#q) = %#q; want non-nil error`, "execabs-test", found) 160 } 161 162 // Set PATH to include an explicit directory that contains a completely 163 // independent executable that happens to have the same name as an 164 // executable in ".". If "." is included implicitly, looking up the 165 // (unqualified) executable name will return ErrDot; otherwise, the 166 // executable in "." should have no effect and the lookup should 167 // unambiguously resolve to the directory in PATH. 168 169 dir := t.TempDir() 170 executable := "execabs-test" 171 if runtime.GOOS == "windows" { 172 executable += ".exe" 173 } 174 if err := os.WriteFile(filepath.Join(dir, executable), []byte{1, 2, 3}, 0777); err != nil { 175 t.Fatal(err) 176 } 177 t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath) 178 179 found, err := LookPath("execabs-test") 180 if wantErrDot { 181 wantFound := filepath.Join(".", executable) 182 if found != wantFound || !errors.Is(err, ErrDot) { 183 t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, Is ErrDot`, "execabs-test", found, err, wantFound) 184 } 185 } else { 186 wantFound := filepath.Join(dir, executable) 187 if found != wantFound || err != nil { 188 t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, nil`, "execabs-test", found, err, wantFound) 189 } 190 } 191 }) 192 }