MCPcopy
hub / github.com/cli/cli / Test_executable_relative

Function Test_executable_relative

internal/ghcmd/executable_test.go:60–116  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

58}
59
60func Test_executable_relative(t *testing.T) {
61 testExe, err := os.Executable()
62 if err != nil {
63 t.Fatal(err)
64 }
65
66 testExeName := filepath.Base(testExe)
67
68 // Create 3 extra PATH entries that each contain an executable with the same name as the running test
69 // process. The first is a relative symlink, but to an unrelated executable, the second is a relative
70 // symlink to our test process and thus represents the result we want, and the third one is an unrelated
71 // executable.
72 dir := t.TempDir()
73 bin1 := filepath.Join(dir, "bin1")
74 bin1Exe := filepath.Join(bin1, testExeName)
75 bin2 := filepath.Join(dir, "bin2")
76 bin2Exe := filepath.Join(bin2, testExeName)
77 bin3 := filepath.Join(dir, "bin3")
78 bin3Exe := filepath.Join(bin3, testExeName)
79
80 if err := os.MkdirAll(bin1, 0755); err != nil {
81 t.Fatal(err)
82 }
83 if err := os.MkdirAll(bin2, 0755); err != nil {
84 t.Fatal(err)
85 }
86 if err := os.MkdirAll(bin3, 0755); err != nil {
87 t.Fatal(err)
88 }
89
90 if f, err := os.OpenFile(bin3Exe, os.O_CREATE, 0755); err == nil {
91 f.Close()
92 } else {
93 t.Fatal(err)
94 }
95 bin2Rel, err := filepath.Rel(bin2, testExe)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if err := os.Symlink(bin2Rel, bin2Exe); err != nil {
100 t.Fatal(err)
101 }
102 bin1Rel, err := filepath.Rel(bin1, bin3Exe)
103 if err != nil {
104 t.Fatal(err)
105 }
106 if err := os.Symlink(bin1Rel, bin1Exe); err != nil {
107 t.Fatal(err)
108 }
109
110 oldPath := os.Getenv("PATH")
111 t.Setenv("PATH", strings.Join([]string{bin1, bin2, bin3, oldPath}, string(os.PathListSeparator)))
112
113 if got := executable(""); got != bin2Exe {
114 t.Errorf("executable() = %q, want %q", got, bin2Exe)
115 }
116}
117

Callers

nothing calls this directly

Calls 6

BaseMethod · 0.80
JoinMethod · 0.80
executableFunction · 0.70
ExecutableMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected