-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
xdg_test.go
230 lines (192 loc) · 5.79 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
package xdg_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/adrg/xdg"
)
type envSample struct {
name string
value string
expected interface{}
actual interface{}
}
func testDirs(t *testing.T, samples ...*envSample) {
// Reset environment after test execution.
environ := os.Environ()
defer func() {
os.Clearenv()
for _, env := range environ {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
continue
}
os.Setenv(parts[0], parts[1])
}
xdg.Reload()
}()
// Test home directory.
require.NotEmpty(t, xdg.Home)
t.Logf("Home: %s", xdg.Home)
// Set environment variables.
for _, sample := range samples {
require.NoError(t, os.Setenv(sample.name, sample.value))
}
xdg.Reload()
// Test results.
for _, sample := range samples {
var actual interface{}
switch v := sample.actual.(type) {
case *string:
actual = *v
case *[]string:
actual = *v
}
require.Equal(t, sample.expected, actual)
t.Logf("%s: %v", sample.name, actual)
}
}
type testInputData struct {
relPaths []string
pathFunc func(string) (string, error)
searchFunc func(string) (string, error)
}
func TestBaseDirFuncs(t *testing.T) {
inputs := []*testInputData{
{
relPaths: []string{"app.data", "appname/app.data"},
pathFunc: xdg.DataFile,
searchFunc: xdg.SearchDataFile,
},
{
relPaths: []string{"app.yaml", "appname/app.yaml"},
pathFunc: xdg.ConfigFile,
searchFunc: xdg.SearchConfigFile,
},
{
relPaths: []string{"app.state", "appname/app.state"},
pathFunc: xdg.StateFile,
searchFunc: xdg.SearchStateFile,
},
{
relPaths: []string{"app.cache", "appname/app.cache"},
pathFunc: xdg.CacheFile,
searchFunc: xdg.SearchCacheFile,
},
{
relPaths: []string{"app.pid", "appname/app.pid"},
pathFunc: xdg.RuntimeFile,
searchFunc: xdg.SearchRuntimeFile,
},
}
// Test base directories for regular files.
testBaseDirsRegular(t, inputs)
// Test base directories for symbolic links.
for _, input := range inputs {
input.relPaths = []string{input.relPaths[1]}
}
testBaseDirsSymlinks(t, inputs)
}
func testBaseDirsRegular(t *testing.T, inputs []*testInputData) {
for _, input := range inputs {
for _, relPath := range input.relPaths {
// Get suitable path for input file.
expFullPath, err := input.pathFunc(relPath)
require.NoError(t, err)
// Create input file.
f, err := os.Create(expFullPath)
require.NoError(t, err)
require.NoError(t, f.Close())
// Search input file after creation.
actFullPath, err := input.searchFunc(relPath)
require.NoError(t, err)
require.Equal(t, expFullPath, actFullPath)
// Remove created file.
require.NoError(t, os.Remove(expFullPath))
// Search input file after removal.
_, err = input.searchFunc(relPath)
require.Error(t, err)
// Check that the same path is returned.
actFullPath, err = input.pathFunc(relPath)
require.NoError(t, err)
require.Equal(t, expFullPath, actFullPath)
}
}
}
func testBaseDirsSymlinks(t *testing.T, inputs []*testInputData) {
for _, input := range inputs {
for _, relPath := range input.relPaths {
// Get suitable path for input file.
expFullPath, err := input.pathFunc(relPath)
require.NoError(t, err)
// Replace input directory with symlink.
symlinkDir := filepath.Dir(expFullPath)
inputDir := filepath.Join(filepath.Dir(symlinkDir), "inputdir")
require.NoError(t, os.Remove(symlinkDir))
require.NoError(t, os.Mkdir(inputDir, os.ModeDir|0700))
require.NoError(t, os.Symlink(inputDir, symlinkDir))
// Create input file.
inputPath := filepath.Join(symlinkDir, "input.file")
f, err := os.Create(inputPath)
require.NoError(t, err)
require.NoError(t, f.Close())
// Create symbolic link.
require.NoError(t, os.Symlink(inputPath, expFullPath))
// Search input file after creation.
actFullPath, err := input.searchFunc(relPath)
require.NoError(t, err)
require.Equal(t, expFullPath, actFullPath)
// Remove created symbolic links, files and directories.
require.NoError(t, os.Remove(expFullPath))
require.NoError(t, os.Remove(inputPath))
require.NoError(t, os.Remove(symlinkDir))
require.NoError(t, os.Remove(inputDir))
// Search input file after removal.
_, err = input.searchFunc(relPath)
require.Error(t, err)
// Check that the same path is returned.
actFullPath, err = input.pathFunc(relPath)
require.NoError(t, err)
require.Equal(t, expFullPath, actFullPath)
}
}
}
func TestInvalidPaths(t *testing.T) {
inputs := map[string]func(string) (string, error){
"\000/app.data": xdg.DataFile,
"appname\000/app.yaml": xdg.ConfigFile,
"appname/\000/app.state": xdg.StateFile,
"\000appname/app.cache": xdg.CacheFile,
"\000/appname/app.pid": xdg.RuntimeFile,
}
for inputPath, xdgFunc := range inputs {
_, err := xdgFunc(inputPath)
require.Error(t, err)
}
}
func TestNonExistentRuntimeDir(t *testing.T) {
var (
runtimeFiles = []string{"app.pid", "appname/app.pid"}
envRuntimeDirVar = "XDG_RUNTIME_DIR"
originalRuntimeDir = xdg.RuntimeDir
nonExistentRuntimeDir = filepath.Join(xdg.Home, "runtime")
)
defer os.Setenv(envRuntimeDirVar, originalRuntimeDir)
require.NoError(t, os.Setenv(envRuntimeDirVar, nonExistentRuntimeDir))
xdg.Reload()
require.Equal(t, nonExistentRuntimeDir, xdg.RuntimeDir)
for _, runtimeFile := range runtimeFiles {
suggestedPath, err := xdg.RuntimeFile(runtimeFile)
require.NoError(t, err)
require.True(t, strings.HasPrefix(suggestedPath, os.TempDir()))
f, err := os.Create(suggestedPath)
require.NoError(t, err)
defer os.Remove(suggestedPath)
require.NoError(t, f.Close())
foundPath, err := xdg.SearchRuntimeFile(runtimeFile)
require.NoError(t, err)
require.Equal(t, suggestedPath, foundPath)
}
}