-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdg_test.go
337 lines (321 loc) · 7.89 KB
/
xdg_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package xdg
import (
"os"
"path/filepath"
"reflect"
"testing"
)
// Verify if paths are correctly classified as absolute or relative.
func TestIsAbsolute(t *testing.T) {
data := []struct {
name string
path string
expected bool
}{
{"absolute", "/usr/bin", true},
{"root", "/", true},
{"relative", "../bin", false},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
result := isAbsolute(d.path)
if result != d.expected {
t.Errorf("Expected %t; got %t", d.expected, result)
}
})
}
}
// Check if Path.require() allows the verify whether Path meets some condition.
func TestPathStructRquire(t *testing.T) {
data := []struct {
name string
path path
checker func(string) bool
expected bool
}{
{"absolute", path{"/etc"}, isAbsolute, true},
{"root", path{"/"}, isAbsolute, true},
{"relative", path{"../"}, isAbsolute, false},
{"root exists", path{"/"}, isExist, true},
{"is a list", path{"/bin:/usr/bin"}, isList, true},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
v := filterAdapter(d.checker)
if result := d.path.require(v); result != d.expected {
t.Errorf("Expected: %t; got %t", d.expected, result)
}
})
}
}
// Test if the following XDG environmental variables are set.
func TestGettingXdgEnv(t *testing.T) {
tmpSetEnv := func(key, val string) func() {
tmp := os.Getenv(key)
os.Setenv(key, val)
return func() {
os.Setenv(key, tmp)
}
}
data := []struct {
name string
key env
expected string
set func(string, string) func()
}{
{string(dataHome), dataHome, "/some/path", tmpSetEnv},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
reset := d.set(string(d.key), d.expected)
defer reset()
if result := valueOf(d.key, d.expected); result != d.expected {
t.Errorf("Expected %s; got %s", d.expected, result)
}
})
}
}
// Verify if joinHome prepends $HOME to the path in a controlled fashion.
func TestJoinHome(t *testing.T) {
tmp := os.Getenv("HOME")
os.Setenv("HOME", "/home/user")
defer os.Setenv("HOME", tmp)
data := []struct {
name string
path string
}{
{".local/share", ".local/share"},
{".local/state", ".local/state"},
{".config", ".config"},
{".cache", ".cache"},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil {
t.Fatalf("Failed to execute test %s", err)
}
expected := filepath.Join(home, d.path)
if result := joinHome(d.path); result != expected {
t.Errorf("Expected: %s; got: %s", expected, result)
}
})
}
}
// Check if the isExist check returns the expected boolean value.
func TestIsExist(t *testing.T) {
data := []struct {
path string
expected bool
}{
{"/", true},
{"/usr/bin", true},
{"/etc/", true},
{"/user", false},
{"/home/anonymous", false},
}
for _, d := range data {
t.Run(d.path, func(t *testing.T) {
if result := isExist(d.path); result != d.expected {
t.Errorf("Expected: %t; got: %t", d.expected, result)
}
})
}
}
// Test if a given string can be seen as a list of paths.
func TestIsList(t *testing.T) {
data := []struct {
path string
expected bool
}{
{"/home", false},
{"/usr/bin:/bin", true},
{"", false},
}
for _, d := range data {
t.Run(d.path, func(t *testing.T) {
if result := isList(d.path); result != d.expected {
t.Errorf("Expected: %t; got: %t", d.expected, result)
}
})
}
}
// Test splitting up the path on the Unix path list separator.
func TestPathSplits(t *testing.T) {
data := []struct {
path string
expected []path
}{
{"/usr/local/share/:/usr/share/",
[]path{
{"/usr/local/share/"},
{"/usr/share/"},
},
},
{"/bin:/usr/bin:/usr/local/bin",
[]path{
{"/bin"},
{"/usr/bin"},
{"/usr/local/bin"},
},
},
{"/usr/local/share/,/usr/share/",
[]path{
{"/usr/local/share/,/usr/share/"},
},
},
{"",
[]path{
{""},
},
},
}
for _, d := range data {
t.Run(d.path, func(t *testing.T) {
p := path{d.path}
if result := p.split(); !reflect.DeepEqual(result, d.expected) {
t.Errorf("Expected: %v; got: %v", d.expected, result)
}
})
}
}
// Test filtering paths with a filter function.
func TestFilterPath(t *testing.T) {
paths := []path{
{"/"},
{"/etc"},
{"/usr/bin"},
{"Documents"},
}
expected := []path{
{"/"},
{"/etc"},
{"/usr/bin"},
}
result := applyFilter(paths, filterAdapter(isAbsolute))
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected: %v; got: %v", expected, result)
}
}
// Test if the same paths are returned without any filter being applied to them.
func TestApplyNoFilter(t *testing.T) {
paths := []path{
{"/"},
{"/etc"},
{"/usr/bin"},
{".."},
{"../Documents"},
}
result := applyFilter(paths)
if !reflect.DeepEqual(result, paths) {
t.Errorf("Expected: %v; got: %v", paths, result)
}
}
// Test the joined value of the returned path struct.
func TestJoinPath(t *testing.T) {
p := path{"/usr/local/share"}
result := p.join("prog/file")
expected := path{"/usr/local/share/prog/file"}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected: %v; got: %v", expected, result)
}
}
// Verify detaching struct method and mapping it over a list of path structs.
func TestMapJoin(t *testing.T) {
data := []struct {
name string
paths []path
s string
expected []path
}{
{
"/usr/share/prog/file",
[]path{{"/usr/share"}, {"/usr/local/share"}},
"prog/file",
[]path{{"/usr/share/prog/file"}, {"/usr/local/share/prog/file"}},
},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
result := mapJoin(d.paths, d.s, joinerAdapter(path.join))
if !reflect.DeepEqual(result, d.expected) {
t.Errorf("Expected: %v; got: %v", d.expected, result)
}
})
}
}
// Check whether Find discovers the first valid path from the preference ordered
// set.
func TestFind(t *testing.T) {
// NOTE: I am testing for empty default dirs. I do not want to create any dirs.
data := []struct {
name string
dir dir
}{
{"data", Data},
{"config", Config},
{"state", State},
{"cache", Cache},
{"runtime", Runtime},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
if _, ok := Find(d.dir, ""); !ok {
t.Errorf("Find failed for %s", d.name)
}
})
}
}
// Verify if unexported find() fails when len(paths) == 0.
func TestFindFails(t *testing.T) {
dirs := []struct {
name string
dir string
expected bool
}{
{"non-existent", "/non/existent/dir", false},
{"non-absolute", "../non_absolute", false},
}
for _, d := range dirs {
t.Run(d.name, func(t *testing.T) {
if _, ok := find("missing/file", d.dir); ok != d.expected {
t.Errorf("Expected: %t; got: %t", d.expected, ok)
}
})
}
}
// Test if variables for XDG base directories are dynamically changed when the
// value is changed.
func TestDynamic(t *testing.T) {
tmpSetEnv := func(key, val string) func() {
tmp := os.Getenv(key)
os.Setenv(key, val)
return func() {
os.Setenv(key, tmp)
}
}
data := []struct {
name string
key env
expected string
callable func() string
set func(string, string) func()
}{
{string(dataHome), dataHome, "/some/data/home", DataHomeDir, tmpSetEnv},
{string(configHome), configHome, "/some/config/path", ConfigHomeDir, tmpSetEnv},
{string(stateHome), stateHome, "/some/state/home", StateHomeDir, tmpSetEnv},
{string(dataDirs), dataDirs, "/some/data/:/data/dirs/", DataDirs, tmpSetEnv},
{string(configDirs), configDirs, "/some/config/:/config/dirs/", ConfigDirs, tmpSetEnv},
{string(cacheHome), cacheHome, "/random/cache/home", CacheHomeDir, tmpSetEnv},
{string(runtimeDir), runtimeDir, "/local/temp/dir", RuntimeDir, tmpSetEnv},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
reset := d.set(string(d.key), d.expected)
defer reset()
if result := d.callable(); result != d.expected {
t.Errorf("Expected %s; got %s", d.expected, result)
}
})
}
}