| 1 |
package xdg |
| 2 |
|
| 3 |
import ( |
| 4 |
"runtime" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestCachePath(t *testing.T) { |
| 9 |
t.Setenv("HOME", "/home/user") |
| 10 |
vectors := []struct { |
| 11 |
args []string |
| 12 |
env map[string]string |
| 13 |
expected map[string]string |
| 14 |
}{ |
| 15 |
{ |
| 16 |
args: []string{"aerc", "foo", "history"}, |
| 17 |
env: map[string]string{"XDG_CACHE_HOME": ""}, |
| 18 |
expected: map[string]string{ |
| 19 |
"": "/home/user/.cache/aerc/foo/history", |
| 20 |
"darwin": "/home/user/Library/Caches/aerc/foo/history", |
| 21 |
}, |
| 22 |
}, |
| 23 |
{ |
| 24 |
args: []string{"aerc", "foo/zuul"}, |
| 25 |
env: map[string]string{"XDG_CACHE_HOME": "/home/x/.cache"}, |
| 26 |
expected: map[string]string{"": "/home/x/.cache/aerc/foo/zuul"}, |
| 27 |
}, |
| 28 |
{ |
| 29 |
args: []string{}, |
| 30 |
env: map[string]string{"XDG_CACHE_HOME": "/blah"}, |
| 31 |
expected: map[string]string{"": "/blah"}, |
| 32 |
}, |
| 33 |
} |
| 34 |
for _, vec := range vectors { |
| 35 |
expected, found := vec.expected[runtime.GOOS] |
| 36 |
if !found { |
| 37 |
expected = vec.expected[""] |
| 38 |
} |
| 39 |
t.Run(expected, func(t *testing.T) { |
| 40 |
for key, value := range vec.env { |
| 41 |
t.Setenv(key, value) |
| 42 |
} |
| 43 |
res := CachePath(vec.args...) |
| 44 |
if res != expected { |
| 45 |
t.Errorf("got %q expected %q", res, expected) |
| 46 |
} |
| 47 |
}) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func TestConfigPath(t *testing.T) { |
| 52 |
t.Setenv("HOME", "/home/user") |
| 53 |
vectors := []struct { |
| 54 |
args []string |
| 55 |
env map[string]string |
| 56 |
expected map[string]string |
| 57 |
}{ |
| 58 |
{ |
| 59 |
args: []string{"aerc", "accounts.conf"}, |
| 60 |
env: map[string]string{"XDG_CONFIG_HOME": ""}, |
| 61 |
expected: map[string]string{ |
| 62 |
"": "/home/user/.config/aerc/accounts.conf", |
| 63 |
"darwin": "/home/user/Library/Preferences/aerc/accounts.conf", |
| 64 |
}, |
| 65 |
}, |
| 66 |
{ |
| 67 |
args: []string{"aerc", "accounts.conf"}, |
| 68 |
env: map[string]string{"XDG_CONFIG_HOME": "/users/x/.config"}, |
| 69 |
expected: map[string]string{"": "/users/x/.config/aerc/accounts.conf"}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
args: []string{}, |
| 73 |
env: map[string]string{"XDG_CONFIG_HOME": "/blah"}, |
| 74 |
expected: map[string]string{"": "/blah"}, |
| 75 |
}, |
| 76 |
} |
| 77 |
for _, vec := range vectors { |
| 78 |
expected, found := vec.expected[runtime.GOOS] |
| 79 |
if !found { |
| 80 |
expected = vec.expected[""] |
| 81 |
} |
| 82 |
t.Run(expected, func(t *testing.T) { |
| 83 |
for key, value := range vec.env { |
| 84 |
t.Setenv(key, value) |
| 85 |
} |
| 86 |
res := ConfigPath(vec.args...) |
| 87 |
if res != expected { |
| 88 |
t.Errorf("got %q expected %q", res, expected) |
| 89 |
} |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestDataPath(t *testing.T) { |
| 95 |
t.Setenv("HOME", "/home/user") |
| 96 |
vectors := []struct { |
| 97 |
args []string |
| 98 |
env map[string]string |
| 99 |
expected map[string]string |
| 100 |
}{ |
| 101 |
{ |
| 102 |
args: []string{"aerc", "templates"}, |
| 103 |
env: map[string]string{"XDG_DATA_HOME": ""}, |
| 104 |
expected: map[string]string{ |
| 105 |
"": "/home/user/.local/share/aerc/templates", |
| 106 |
"darwin": "/home/user/Library/Application Support/aerc/templates", |
| 107 |
}, |
| 108 |
}, |
| 109 |
{ |
| 110 |
args: []string{"aerc", "templates"}, |
| 111 |
env: map[string]string{"XDG_DATA_HOME": "/users/x/.local/share"}, |
| 112 |
expected: map[string]string{"": "/users/x/.local/share/aerc/templates"}, |
| 113 |
}, |
| 114 |
{ |
| 115 |
args: []string{}, |
| 116 |
env: map[string]string{"XDG_DATA_HOME": "/blah"}, |
| 117 |
expected: map[string]string{"": "/blah"}, |
| 118 |
}, |
| 119 |
} |
| 120 |
for _, vec := range vectors { |
| 121 |
expected, found := vec.expected[runtime.GOOS] |
| 122 |
if !found { |
| 123 |
expected = vec.expected[""] |
| 124 |
} |
| 125 |
t.Run(expected, func(t *testing.T) { |
| 126 |
for key, value := range vec.env { |
| 127 |
t.Setenv(key, value) |
| 128 |
} |
| 129 |
res := DataPath(vec.args...) |
| 130 |
if res != expected { |
| 131 |
t.Errorf("got %q expected %q", res, expected) |
| 132 |
} |
| 133 |
}) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestRuntimePath(t *testing.T) { |
| 138 |
// poor man's function mocking |
| 139 |
orig := userRuntimePath |
| 140 |
userRuntimePath = func() string { return "/run/user/1000" } |
| 141 |
defer func() { userRuntimePath = orig }() |
| 142 |
t.Setenv("HOME", "/home/user") |
| 143 |
|
| 144 |
vectors := []struct { |
| 145 |
args []string |
| 146 |
env map[string]string |
| 147 |
expected map[string]string |
| 148 |
}{ |
| 149 |
{ |
| 150 |
args: []string{"aerc.sock"}, |
| 151 |
env: map[string]string{"XDG_RUNTIME_DIR": ""}, |
| 152 |
expected: map[string]string{ |
| 153 |
"": "/run/user/1000/aerc.sock", |
| 154 |
"darwin": "/home/user/Library/Application Support/aerc.sock", |
| 155 |
}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
args: []string{"aerc.sock"}, |
| 159 |
env: map[string]string{"XDG_RUNTIME_DIR": "/run/user/1234"}, |
| 160 |
expected: map[string]string{"": "/run/user/1234/aerc.sock"}, |
| 161 |
}, |
| 162 |
{ |
| 163 |
args: []string{}, |
| 164 |
env: map[string]string{"XDG_RUNTIME_DIR": "/blah"}, |
| 165 |
expected: map[string]string{"": "/blah"}, |
| 166 |
}, |
| 167 |
} |
| 168 |
for _, vec := range vectors { |
| 169 |
expected, found := vec.expected[runtime.GOOS] |
| 170 |
if !found { |
| 171 |
expected = vec.expected[""] |
| 172 |
} |
| 173 |
t.Run(expected, func(t *testing.T) { |
| 174 |
for key, value := range vec.env { |
| 175 |
t.Setenv(key, value) |
| 176 |
} |
| 177 |
res := RuntimePath(vec.args...) |
| 178 |
if res != expected { |
| 179 |
t.Errorf("got %q expected %q", res, expected) |
| 180 |
} |
| 181 |
}) |
| 182 |
} |
| 183 |
} |