-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
which_test.go
186 lines (142 loc) · 4.54 KB
/
which_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//go:build !windows
// +build !windows
package which
import (
"fmt"
"os"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
)
func initFS(_ *testing.T) fstest.MapFS {
fsys := fstest.MapFS{}
contents := []byte("#!/bin/sh\necho hello world")
// some executables
fsys["bin/foo"] = &fstest.MapFile{Data: contents, Mode: 0o755}
fsys["usr/bin/foo"] = &fstest.MapFile{Data: contents, Mode: 0o755}
fsys["bin/bar"] = &fstest.MapFile{Data: contents, Mode: 0o755}
fsys["usr/local/bin/bar"] = &fstest.MapFile{Data: contents, Mode: 0o755}
fsys["usr/local/bin/qux"] = &fstest.MapFile{Data: contents, Mode: 0o755}
// some non-executable files
fsys["usr/local/bin/foo"] = &fstest.MapFile{Data: contents, Mode: 0o644}
fsys["opt/bar"] = &fstest.MapFile{Data: contents, Mode: 0o644}
fsys["opt/qux"] = &fstest.MapFile{Data: contents, Mode: 0o644}
return fsys
}
func TestWhich(t *testing.T) {
origFS := testFS
defer func() { testFS = origFS }()
testFS = initFS(t)
os.Setenv("PATH", "/usr/local/bin:/usr/bin:/bin:/opt")
assert.Equal(t, "", Which())
assert.Equal(t, "", Which(""))
assert.Equal(t, "", Which("baz"))
assert.Equal(t, "/usr/bin/foo", Which("foo"))
assert.Equal(t, "/usr/local/bin/bar", Which("bar"))
assert.Equal(t, "", Which("bin"))
assert.Equal(t, "/usr/bin/foo", Which("foo", "bar", "baz"))
}
func TestAll(t *testing.T) {
origFS := testFS
defer func() { testFS = origFS }()
testFS = initFS(t)
os.Setenv("PATH", "/usr/local/bin:/usr/bin:/bin:/opt")
assert.EqualValues(t, []string{}, All())
assert.EqualValues(t, []string{}, All(""))
assert.EqualValues(t, []string{}, All("baz"))
assert.EqualValues(t, []string{"/usr/bin/foo", "/bin/foo"}, All("foo"))
assert.EqualValues(t, []string{"/usr/local/bin/bar", "/bin/bar"}, All("bar"))
assert.EqualValues(t, []string{}, All("bin"))
assert.EqualValues(t, []string{
"/usr/bin/foo", "/bin/foo",
"/usr/local/bin/bar", "/bin/bar",
},
All("foo", "bar", "baz"))
}
func TestFound(t *testing.T) {
origFS := testFS
defer func() { testFS = origFS }()
testFS = initFS(t)
os.Setenv("PATH", "/usr/local/bin:/usr/bin:/bin:/opt")
assert.False(t, Found())
assert.False(t, Found(""))
assert.False(t, Found("baz"))
assert.True(t, Found("foo"))
assert.True(t, Found("bar"))
assert.False(t, Found("bin"))
assert.False(t, Found("foo", "bar", "baz"))
assert.True(t, Found("foo", "bar", "qux"))
}
func TestFsysFor(t *testing.T) {
origFS := testFS
defer func() { testFS = origFS }()
testFS = nil
fsys, p := fsysFor("")
assert.Equal(t, "/", fmt.Sprintf("%v", fsys))
assert.Equal(t, ".", p)
fsys, p = fsysFor("/")
assert.Equal(t, "/", fmt.Sprintf("%v", fsys))
assert.Equal(t, ".", p)
fsys, p = fsysFor("/tmp/foo/bar")
assert.Equal(t, "/", fmt.Sprintf("%v", fsys))
assert.Equal(t, "tmp/foo/bar", p)
fsys, p = fsysFor("C:/Users/foo")
assert.Equal(t, "C:/", fmt.Sprintf("%v", fsys))
assert.Equal(t, "Users/foo", p)
fsys, p = fsysFor("d:/tmp/foo")
assert.Equal(t, "d:/", fmt.Sprintf("%v", fsys))
assert.Equal(t, "tmp/foo", p)
}
//nolint:gochecknoinits
func init() {
data := []byte("#!/bin/sh\necho hello world\n")
memfs := fstest.MapFS{}
memfs["bin/sh"] = &fstest.MapFile{Data: data, Mode: 0o755}
memfs["usr/local/bin/zsh"] = &fstest.MapFile{Data: data, Mode: 0o755}
memfs["bin/zsh"] = &fstest.MapFile{Data: data, Mode: 0o755}
memfs["bin/bash"] = &fstest.MapFile{Data: data, Mode: 0o755}
testFS = memfs
}
func ExampleWhich() {
path := Which("sh")
fmt.Printf("Found sh at: %s", path)
// Output: Found sh at: /bin/sh
}
// When given multiple arguments, `Which` will return the path for the first found
func ExampleWhich_multiples() {
path := Which("bogus", "sh")
fmt.Printf("First found was: %s", path)
// Output: First found was: /bin/sh
}
func ExampleAll() {
path := All("zsh")
fmt.Printf("%v", path)
// Output: [/usr/local/bin/zsh /bin/zsh]
}
// When given multiple arguments, `All` will return all paths, sorted by argument order
func ExampleAll_multiples() {
path := All("zsh", "bash")
fmt.Printf("%v", path)
// Output: [/usr/local/bin/zsh /bin/zsh /bin/bash]
}
func ExampleFound() {
if Found("zsh") {
fmt.Println("got it!")
}
if !Found("bogon") {
fmt.Println("phew, no bogons")
}
// Output: got it!
// phew, no bogons
}
// When given multiple arguments, `Found` will return all paths, sorted by argument order
func ExampleFound_multiples() {
if Found("zsh", "bash") {
fmt.Println("a decent collection of shells")
}
if !Found("zsh", "bash", "ash") {
fmt.Println("just missing the ashes...")
}
// Output: a decent collection of shells
// just missing the ashes...
}