(t *testing.T)
| 10 | ) |
| 11 | |
| 12 | func Test_executable(t *testing.T) { |
| 13 | testExe, err := os.Executable() |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | |
| 18 | testExeName := filepath.Base(testExe) |
| 19 | |
| 20 | // Create 3 extra PATH entries that each contain an executable with the same name as the running test |
| 21 | // process. The first is a symlink, but to an unrelated executable, the second is a symlink to our test |
| 22 | // process and thus represents the result we want, and the third one is an unrelated executable. |
| 23 | dir := t.TempDir() |
| 24 | bin1 := filepath.Join(dir, "bin1") |
| 25 | bin1Exe := filepath.Join(bin1, testExeName) |
| 26 | bin2 := filepath.Join(dir, "bin2") |
| 27 | bin2Exe := filepath.Join(bin2, testExeName) |
| 28 | bin3 := filepath.Join(dir, "bin3") |
| 29 | bin3Exe := filepath.Join(bin3, testExeName) |
| 30 | |
| 31 | if err := os.MkdirAll(bin1, 0755); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if err := os.MkdirAll(bin2, 0755); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if err := os.MkdirAll(bin3, 0755); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if f, err := os.OpenFile(bin3Exe, os.O_CREATE, 0755); err == nil { |
| 41 | f.Close() |
| 42 | } else { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err := os.Symlink(testExe, bin2Exe); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if err := os.Symlink(bin3Exe, bin1Exe); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | |
| 52 | oldPath := os.Getenv("PATH") |
| 53 | t.Setenv("PATH", strings.Join([]string{bin1, bin2, bin3, oldPath}, string(os.PathListSeparator))) |
| 54 | |
| 55 | if got := executable(""); got != bin2Exe { |
| 56 | t.Errorf("executable() = %q, want %q", got, bin2Exe) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func Test_executable_relative(t *testing.T) { |
| 61 | testExe, err := os.Executable() |
nothing calls this directly
no test coverage detected