-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdgdirs_test.go
210 lines (174 loc) · 4.64 KB
/
xdgdirs_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
package xdgdirs_test
import (
"os"
"os/user"
"path/filepath"
"github.com/redforks/osutil"
"github.com/redforks/hal"
"github.com/redforks/testing/iotest"
"github.com/redforks/testing/reset"
. "github.com/redforks/xdgdirs"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Xdgdirs", func() {
var (
envs map[string]string
Getenv = func(key string) string {
return envs[key]
}
homeDir = ""
setXDGDataDirs = func(value string) {
envs["XDG_DATA_DIRS"] = value
}
)
BeforeEach(func() {
reset.Enable()
hal.Getenv = Getenv
homeDir = "/user/foo"
envs = map[string]string{
"HOME": homeDir,
}
})
AfterEach(func() {
reset.Disable()
})
It("Home", func() {
Ω(Home()).Should(Equal(homeDir))
})
It("Home env not defined", func() {
delete(envs, "HOME")
Ω(Home()).Should(Equal("/"))
})
Context("DataHome", func() {
It("environment defined", func() {
envs["XDG_DATA_HOME"] = "/usr/share/data"
Ω(DataHome()).Should(Equal("/usr/share/data"))
})
It("environment not defined", func() {
Ω(DataHome()).Should(Equal("/user/foo/.local/share"))
})
})
Context("ConfigHome", func() {
It("environment defined", func() {
envs["XDG_CONFIG_HOME"] = "/foo"
Ω(ConfigHome()).Should(Equal("/foo"))
})
It("environment not defined", func() {
Ω(ConfigHome()).Should(Equal("/user/foo/.config"))
})
})
Context("CacheHome", func() {
It("environment defined", func() {
envs["XDG_CACHE_HOME"] = "/foo"
Ω(CacheHome()).Should(Equal("/foo"))
})
It("environment not defined", func() {
Ω(CacheHome()).Should(Equal("/user/foo/.cache"))
})
})
Context("RuntimeHome", func() {
It("environment defined", func() {
envs["XDG_RUNTIME_DIR"] = "/foo"
Ω(RuntimeHome()).Should(Equal("/foo"))
})
It("default for root", func() {
hal.CurrentUser = func() (*user.User, error) {
return &user.User{
Uid: "0",
Name: "root",
}, nil
}
Ω(RuntimeHome()).Should(Equal("/run"))
})
It("default for non-root", func() {
hal.CurrentUser = func() (*user.User, error) {
return &user.User{
Uid: "100",
Name: "user",
}, nil
}
Ω(RuntimeHome()).Should(Equal("/tmp/user"))
})
})
Context("DataDirs", func() {
It("environment not defined", func() {
Ω(DataDirs()).Should(Equal([]string{
"/user/foo/.local/share",
"/usr/local/share",
"/usr/share",
}))
})
It("environment defined with one entry", func() {
setXDGDataDirs("/share/data")
Ω(DataDirs()).Should(Equal([]string{
"/user/foo/.local/share",
"/share/data",
}))
})
It("environment defined with multi entries", func() {
setXDGDataDirs("/share/data:/data:/share")
Ω(DataDirs()).Should(Equal([]string{
"/user/foo/.local/share",
"/share/data",
"/data",
"/share",
}))
})
})
Context("ConfigDirs", func() {
It("environment not defined", func() {
Ω(ConfigDirs()).Should(Equal([]string{
"/user/foo/.config",
"/etc/xdg",
}))
})
It("environment defined with multi entries", func() {
envs["XDG_CONFIG_DIRS"] = "/share/data:/data:/share"
Ω(ConfigDirs()).Should(Equal([]string{
"/user/foo/.config",
"/share/data",
"/data",
"/share",
}))
})
})
Context("Resolve file path", func() {
BeforeEach(func() {
homeDir = iotest.NewTempTestDir().Dir()
envs["HOME"] = homeDir
setXDGDataDirs("")
})
Context("ResolveDataFile", func() {
It("Found in 1st dir", func() {
fooFile := filepath.Join(homeDir, ".local/share/foo")
Ω(osutil.WriteFile(fooFile, nil, 0700, 0600)).Should(Succeed())
Ω(ResolveDataFile("foo")).Should(Equal(fooFile))
})
It("Found in 2nd dir", func() {
path2 := filepath.Join(homeDir, "data")
fooFile := filepath.Join(path2, "foo")
setXDGDataDirs(path2)
Ω(osutil.WriteFile(fooFile, nil, 0700, 0600)).Should(Succeed())
Ω(ResolveDataFile("foo")).Should(Equal(fooFile))
})
It("File not found", func() {
_, err := ResolveDataFile("foo")
Ω(err).Should(MatchError("[xdgdirs] Can not found data file: foo"))
})
It("Exist but not regular file", func() {
Ω(os.MkdirAll(filepath.Join(homeDir, ".local/share/foo"), 0700)).Should(Succeed())
_, err := ResolveDataFile("foo")
Ω(err).ShouldNot(BeNil())
Ω(err.Error()).Should(ContainSubstring("foo\" exist but not regular file"))
})
})
It("ResolveConfigFile", func() {
// ResolveConfigFile share the same implementation with ResolveDataFile(), so
// no need doing detailed tests
fooFile := filepath.Join(homeDir, ".config/foo")
Ω(osutil.WriteFile(fooFile, nil, 0700, 0600)).Should(Succeed())
Ω(ResolveConfigFile("foo")).Should(Equal(fooFile))
})
})
})