-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommon_test.go
93 lines (83 loc) · 2.49 KB
/
common_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
package configdir_test
import (
"os"
"testing"
"github.com/kirsle/configdir"
)
// TestCase provides common inputs and outputs for the functions being tested.
type TestCase struct {
Env string // Value to put into the relevant environment variable, when Refresh=true
Refresh bool // Whether to call the Refresh() function before running
Paths []string // Path suffixes to give to the path function
Values []string // What we expect the return value(s) to contain
}
// On init, call the reset() function provided by the various OS-specific tests
// to reset environment variables to a known deterministic state.
func init() {
reset()
}
// Common logic for the local paths, which return single values.
//
// Parameters:
// t (*testing.T)
// pathType (string): "config" or "cache", controls which environment
// variable to play with and which path function to call.
// defaultPrefix (string): the default path prefix for the kind of path
// being tested, e.g. "/home/user/.config" for config paths.
// customPrefix (string): when a custom value is set for the environment
// variable, this is that path prefix instead of the default.
func testLocalCommon(t *testing.T, pathType, defaultPrefix, customPrefix string) {
reset()
// Cases to test.
var tests = []TestCase{
TestCase{
Paths: []string{},
Values: []string{defaultPrefix},
},
TestCase{
Paths: []string{"vendor-name"},
Values: []string{defaultPrefix + "/vendor-name"},
},
TestCase{
Paths: []string{"vendor-name", "app-name"},
Values: []string{defaultPrefix + "/vendor-name/app-name"},
},
// With custom XDG paths...
TestCase{
Env: customPrefix,
Refresh: true,
Paths: []string{},
Values: []string{customPrefix},
},
TestCase{
Paths: []string{"vendor-name"},
Values: []string{customPrefix + "/vendor-name"},
},
TestCase{
Paths: []string{"vendor-name", "app-name"},
Values: []string{customPrefix + "/vendor-name/app-name"},
},
}
for _, test := range tests {
if test.Refresh {
if pathType == "config" {
os.Setenv("XDG_CONFIG_HOME", test.Env)
} else {
os.Setenv("XDG_CACHE_HOME", test.Env)
}
configdir.Refresh()
}
var result string
if pathType == "config" {
result = configdir.LocalConfig(test.Paths...)
} else {
result = configdir.LocalCache(test.Paths...)
}
if result != test.Values[0] {
t.Errorf("Got wrong path result. Expected %s, got %s\n",
test.Values[0],
result,
)
}
}
}