-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
paths_unix_test.go
172 lines (157 loc) · 4.9 KB
/
paths_unix_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
//go:build !darwin && !windows
// +build !darwin,!windows
package gap
import (
"fmt"
"testing"
)
func TestPaths(t *testing.T) {
tests := []struct {
scope *Scope
dataDirs []string
configDirs []string
cacheDir string
configFile string
dataFile string
logFile string
}{
{
scope: NewScope(System, "foobar"),
dataDirs: []string{"/usr/local/share/foobar", "/usr/share/foobar"},
configDirs: []string{"/etc/xdg/foobar", "/etc/foobar"},
cacheDir: "/var/cache/foobar",
configFile: "/etc/xdg/foobar/foobar.conf",
dataFile: "/usr/local/share/foobar/foobar.data",
logFile: "/var/log/foobar/foobar.log",
},
{
scope: NewVendorScope(System, "barcorp", "foobar"),
dataDirs: []string{"/usr/local/share/barcorp/foobar", "/usr/share/barcorp/foobar"},
configDirs: []string{"/etc/xdg/barcorp/foobar", "/etc/barcorp/foobar"},
cacheDir: "/var/cache/barcorp/foobar",
configFile: "/etc/xdg/barcorp/foobar/foobar.conf",
dataFile: "/usr/local/share/barcorp/foobar/foobar.data",
logFile: "/var/log/barcorp/foobar/foobar.log",
},
{
scope: NewScope(User, "foobar"),
dataDirs: []string{"~/.local/share/foobar", "/usr/local/share/foobar", "/usr/share/foobar"},
configDirs: []string{"~/.config/foobar", "/etc/xdg/foobar", "/etc/foobar"},
cacheDir: "~/.cache/foobar",
configFile: "~/.config/foobar/foobar.conf",
dataFile: "~/.local/share/foobar/foobar.data",
logFile: "~/.local/share/foobar/foobar.log",
},
{
scope: NewCustomHomeScope("/tmp", "", "foobar"),
dataDirs: []string{"/tmp/.local/share/foobar"},
configDirs: []string{"/tmp/.config/foobar"},
cacheDir: "/tmp/.cache/foobar",
configFile: "/tmp/.config/foobar/foobar.conf",
dataFile: "/tmp/.local/share/foobar/foobar.data",
logFile: "/tmp/.local/share/foobar/foobar.log",
},
}
for _, tt := range tests {
paths, err := tt.scope.DataDirs()
if err != nil {
t.Errorf("Error retrieving data dir: %s", err)
}
if len(paths) != len(tt.dataDirs) {
fmt.Println(paths)
t.Fatalf("Expected %d results, got %d", len(tt.dataDirs), len(paths))
}
for i := range paths {
if paths[i] != expandUser(tt.dataDirs[i]) {
t.Errorf("Expected data dir: %s - got: %s", tt.dataDirs[i], paths[i])
}
}
paths, err = tt.scope.ConfigDirs()
if err != nil {
t.Errorf("Error retrieving data dir: %s", err)
}
if len(paths) != len(tt.configDirs) {
fmt.Println(paths)
t.Fatalf("Expected %d results, got %d", len(tt.configDirs), len(paths))
}
for i := range paths {
if paths[i] != expandUser(tt.configDirs[i]) {
t.Errorf("Expected data dir: %s - got: %s", tt.configDirs[i], paths[i])
}
}
path, err := tt.scope.CacheDir()
if err != nil {
t.Errorf("Error retrieving cache dir: %s", err)
}
if path != expandUser(tt.cacheDir) {
t.Errorf("Expected cache dir: %s - got: %s", tt.cacheDir, path)
}
path, err = tt.scope.ConfigPath(tt.scope.App + ".conf")
if err != nil {
t.Errorf("Error retrieving config path: %s", err)
}
if path != expandUser(tt.configFile) {
t.Errorf("Expected config path: %s - got: %s", tt.configFile, path)
}
path, err = tt.scope.DataPath(tt.scope.App + ".data")
if err != nil {
t.Errorf("Error retrieving data path: %s", err)
}
if path != expandUser(tt.dataFile) {
t.Errorf("Expected data path: %s - got: %s", tt.dataFile, path)
}
path, err = tt.scope.LogPath(tt.scope.App + ".log")
if err != nil {
t.Errorf("Error retrieving log path: %s", err)
}
if path != expandUser(tt.logFile) {
t.Errorf("Expected log path: %s - got: %s", tt.logFile, path)
}
}
}
func TestConfigLookups(t *testing.T) {
tests := []struct {
scope *Scope
configFile string
result []string
}{
{NewScope(System, "ssh"), "sshd_config", []string{"/etc/ssh/sshd_config"}},
{NewScope(User, "ssh"), "sshd_config", []string{"/etc/ssh/sshd_config"}},
}
for _, tt := range tests {
r, err := tt.scope.LookupConfig(tt.configFile)
if err != nil {
t.Errorf("Error looking up config: %s", err)
}
if len(r) != 1 {
fmt.Println(r)
t.Fatalf("Expected 1 result, got %d results", len(r))
}
if r[0] != tt.result[0] {
t.Errorf("Expected config file: %s - got: %s", tt.result[0], r[0])
}
}
}
func TestDataLookups(t *testing.T) {
tests := []struct {
scope *Scope
dataFile string
result []string
}{
{NewVendorScope(System, "terminfo", "x"), "xterm+256color", []string{"/usr/share/terminfo/x/xterm+256color"}},
{NewVendorScope(User, "terminfo", "x"), "xterm+256color", []string{"/usr/share/terminfo/x/xterm+256color"}},
}
for _, tt := range tests {
r, err := tt.scope.LookupDataFile(tt.dataFile)
if err != nil {
t.Errorf("Error looking up data file: %s", err)
}
if len(r) != 1 {
fmt.Println(r)
t.Fatalf("Expected 1 result, got %d results", len(r))
}
if r[0] != tt.result[0] {
t.Errorf("Expected data file: %s - got: %s", tt.result[0], r[0])
}
}
}