-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdg_test.go
105 lines (91 loc) · 2.26 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
package xdg_test
import (
"os"
"testing"
"github.com/adrg/xdg"
"github.com/stretchr/testify/assert"
)
type envSample struct {
name string
value string
expected interface{}
actual interface{}
}
func testDirs(t *testing.T, samples ...*envSample) {
// Test home directory.
if !assert.NotEmpty(t, xdg.Home) {
t.FailNow()
}
t.Logf("Home: %s", xdg.Home)
// Set environment variables.
for _, sample := range samples {
assert.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
}
assert.Equal(t, sample.expected, actual)
t.Logf("%s: %v", sample.name, actual)
}
}
func TestBaseDirFuncs(t *testing.T) {
type inputData struct {
relPaths []string
pathFunc func(string) (string, error)
searchFunc func(string) (string, error)
}
inputs := []*inputData{
{
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.cache", "appname/app.cache"},
pathFunc: xdg.CacheFile,
searchFunc: xdg.SearchCacheFile,
},
{
relPaths: []string{"app.pid", "appname/app.pid"},
pathFunc: xdg.RuntimeFile,
searchFunc: xdg.SearchRuntimeFile,
},
{
relPaths: []string{"app.state", "appname/app.state"},
pathFunc: xdg.StateFile,
searchFunc: xdg.SearchStateFile,
},
}
for _, input := range inputs {
for _, relPath := range input.relPaths {
// Get suitable path for input file.
expFullPath, err := input.pathFunc(relPath)
assert.NoError(t, err)
// Create input file.
f, err := os.Create(expFullPath)
assert.NoError(t, err)
assert.NoError(t, f.Close())
// Search input file after creation.
actFullPath, err := input.searchFunc(relPath)
assert.NoError(t, err)
assert.Equal(t, expFullPath, actFullPath)
// Remove created file.
assert.NoError(t, os.Remove(expFullPath))
// Search input file after removal.
_, err = input.searchFunc(relPath)
assert.Error(t, err)
}
}
}