forked from go-git/go-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_git_test.go
More file actions
360 lines (288 loc) · 10.4 KB
/
Copy pathauto_git_test.go
File metadata and controls
360 lines (288 loc) · 10.4 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package config
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/go-git/go-billy/v6/memfs"
"github.com/go-git/go-billy/v6/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-git/go-git/v6/config"
)
// These tests verify that Auto resolves configuration identically to
// upstream git for every scenario documented in git-config(1).
// All file I/O is backed by memfs so the tests are hermetic and fast.
const testHome = "/testhome"
// git-config(1): "read only from global ~/.gitconfig and from
// $XDG_CONFIG_HOME/git/config rather than from all available files."
func TestGitBehaviour_GlobalOnlyGitconfig(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envGitConfigGlobal, "")
os.Unsetenv(envGitConfigGlobal)
t.Setenv(envXDGConfigHome, "")
src := memAuto(t, map[string]string{
filepath.Join(testHome, ".gitconfig"): "[user]\n\tname = HomeUser\n",
})
assert.Equal(t, "HomeUser", loadUserName(t, src, config.GlobalScope))
}
func TestGitBehaviour_GlobalOnlyXDGDefault(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envGitConfigGlobal, "")
os.Unsetenv(envGitConfigGlobal)
t.Setenv(envXDGConfigHome, "")
setTestAppData(t, filepath.Join(testHome, "AppData"))
xdgPath := xdgConfigPath(testHome)
if xdgPath == "" {
t.Skip("no default XDG config path on this platform")
}
src := memAuto(t, map[string]string{
xdgPath: "[user]\n\tname = XDGUser\n",
})
assert.Equal(t, "XDGUser", loadUserName(t, src, config.GlobalScope))
}
func TestGitBehaviour_GlobalNoFilesExist(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envGitConfigGlobal, "")
os.Unsetenv(envGitConfigGlobal)
t.Setenv(envXDGConfigHome, "")
src := NewAuto(WithFilesystem(memfs.New()))
assert.Empty(t, loadUserName(t, src, config.GlobalScope))
}
// git treats ~/.gitconfig and the XDG config as alternatives: when
// ~/.gitconfig exists the XDG location is ignored entirely.
func TestGitBehaviour_GlobalGitconfigIgnoresXDG(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envGitConfigGlobal, "")
os.Unsetenv(envGitConfigGlobal)
t.Setenv(envXDGConfigHome, "")
setTestAppData(t, filepath.Join(testHome, "AppData"))
xdgPath := xdgConfigPath(testHome)
if xdgPath == "" {
t.Skip("no default XDG config path on this platform")
}
src := memAuto(t, map[string]string{
xdgPath: "[user]\n\tname = XDGUser\n\temail = xdg@example.com\n",
filepath.Join(testHome, ".gitconfig"): "[user]\n\tname = HomeUser\n",
})
assert.Equal(t, "HomeUser", loadUserName(t, src, config.GlobalScope))
// XDG email is NOT visible because ~/.gitconfig exists.
assert.Empty(t, loadUserEmail(t, src, config.GlobalScope))
}
func TestGitBehaviour_GlobalXDGEnvOverridesDefaultPath(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envXDGConfigHome, "/custom/xdg")
t.Setenv(envGitConfigGlobal, "")
os.Unsetenv(envGitConfigGlobal)
setTestAppData(t, filepath.Join(testHome, "AppData"))
xdgDefault := xdgConfigPath(testHome)
// The env XDG_CONFIG_HOME is set, so this returns the custom path.
customXDG := filepath.Join("/custom/xdg", "git", "config")
require.Equal(t, customXDG, xdgDefault)
src := memAuto(t, map[string]string{
customXDG: "[user]\n\tname = CustomXDG\n",
})
assert.Equal(t, "CustomXDG", loadUserName(t, src, config.GlobalScope))
}
// When ~/.gitconfig exists, XDG_CONFIG_HOME is ignored even if set.
func TestGitBehaviour_GlobalXDGEnvIgnoredWhenGitconfigExists(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envXDGConfigHome, "/custom/xdg")
t.Setenv(envGitConfigGlobal, "")
os.Unsetenv(envGitConfigGlobal)
src := memAuto(t, map[string]string{
filepath.Join(testHome, ".gitconfig"): "[user]\n\tname = HomeUser\n",
filepath.Join("/custom/xdg", "git", "config"): "[user]\n\temail = xdg@example.com\n",
})
assert.Equal(t, "HomeUser", loadUserName(t, src, config.GlobalScope))
assert.Empty(t, loadUserEmail(t, src, config.GlobalScope))
}
// git-config(1): GIT_CONFIG_GLOBAL "replaces ~/.gitconfig" and XDG.
func TestGitBehaviour_GlobalEnvReplacesAll(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envGitConfigGlobal, "/override.cfg")
t.Setenv(envXDGConfigHome, "")
src := memAuto(t, map[string]string{
filepath.Join(testHome, ".gitconfig"): "[user]\n\tname = HomeUser\n\temail = home@example.com\n",
"/override.cfg": "[user]\n\tname = EnvUser\n",
})
assert.Equal(t, "EnvUser", loadUserName(t, src, config.GlobalScope))
// Neither ~/.gitconfig nor XDG values leak through.
assert.Empty(t, loadUserEmail(t, src, config.GlobalScope))
}
func TestGitBehaviour_GlobalEnvNonexistentFile(t *testing.T) {
t.Setenv(envGitConfigGlobal, "/nonexistent/path/gitconfig")
src := NewAuto(WithFilesystem(memfs.New()))
assert.Empty(t, loadUserName(t, src, config.GlobalScope))
}
// git-config(1): GIT_CONFIG_GLOBAL="" explicitly disables global config.
func TestGitBehaviour_GlobalEnvEmptyDisablesGlobal(t *testing.T) {
setTestHome(t, testHome)
t.Setenv(envGitConfigGlobal, "")
t.Setenv(envXDGConfigHome, "")
src := memAuto(t, map[string]string{
filepath.Join(testHome, ".gitconfig"): "[user]\n\tname = HomeUser\n",
})
assert.Empty(t, loadUserName(t, src, config.GlobalScope))
}
func TestGitBehaviour_SystemEnv(t *testing.T) {
t.Setenv(envGitConfigSystem, "/custom/system.cfg")
t.Setenv(envGitConfigNoSystem, "")
src := memAuto(t, map[string]string{
"/custom/system.cfg": "[user]\n\tname = SysUser\n",
})
assert.Equal(t, "SysUser", loadUserName(t, src, config.SystemScope))
}
func TestGitBehaviour_SystemEnvNonexistentFile(t *testing.T) {
t.Setenv(envGitConfigSystem, "/nonexistent/path/gitconfig")
t.Setenv(envGitConfigNoSystem, "")
src := NewAuto(WithFilesystem(memfs.New()))
assert.Empty(t, loadUserName(t, src, config.SystemScope))
}
// git-config(1): GIT_CONFIG_SYSTEM="" explicitly disables system config.
func TestGitBehaviour_SystemEnvEmptyDisablesSystem(t *testing.T) {
t.Setenv(envGitConfigSystem, "")
t.Setenv(envGitConfigNoSystem, "")
src := memAuto(t, map[string]string{
"/etc/gitconfig": "[user]\n\tname = SysUser\n",
})
assert.Empty(t, loadUserName(t, src, config.SystemScope))
}
// System scope falls back to the platform default path when
// GIT_CONFIG_SYSTEM is unset and GIT_CONFIG_NOSYSTEM is not truthy.
func TestGitBehaviour_SystemDefaultPath(t *testing.T) {
t.Setenv(envGitConfigSystem, "")
os.Unsetenv(envGitConfigSystem)
t.Setenv(envGitConfigNoSystem, "")
paths := systemPaths()
if len(paths) == 0 {
t.Skip("no default system config path on this platform")
}
src := memAuto(t, map[string]string{
paths[0]: "[user]\n\tname = SystemDefault\n",
})
assert.Equal(t, "SystemDefault", loadUserName(t, src, config.SystemScope))
}
func TestGitBehaviour_SystemDefaultPathMissing(t *testing.T) {
t.Setenv(envGitConfigSystem, "")
os.Unsetenv(envGitConfigSystem)
t.Setenv(envGitConfigNoSystem, "")
src := NewAuto(WithFilesystem(memfs.New()))
assert.Empty(t, loadUserName(t, src, config.SystemScope))
}
// git-config(1): "If GIT_CONFIG_NOSYSTEM is set to a true value, the
// system configuration file is not read."
func TestGitBehaviour_NoSystemOverridesSystemEnv(t *testing.T) {
t.Setenv(envGitConfigSystem, "/custom/system.cfg")
t.Setenv(envGitConfigNoSystem, "1")
src := memAuto(t, map[string]string{
"/custom/system.cfg": "[user]\n\tname = SysUser\n",
})
assert.Empty(t, loadUserName(t, src, config.SystemScope))
}
func TestGitBehaviour_NoSystemOverridesDefaultPath(t *testing.T) {
t.Setenv(envGitConfigSystem, "")
os.Unsetenv(envGitConfigSystem)
t.Setenv(envGitConfigNoSystem, "1")
src := memAuto(t, map[string]string{
"/etc/gitconfig": "[user]\n\tname = SysUser\n",
})
assert.Empty(t, loadUserName(t, src, config.SystemScope))
}
func TestGitBehaviour_NoSystemUnset(t *testing.T) {
t.Setenv(envGitConfigSystem, "/custom/system.cfg")
t.Setenv(envGitConfigNoSystem, "")
src := memAuto(t, map[string]string{
"/custom/system.cfg": "[user]\n\tname = SysUser\n",
})
assert.Equal(t, "SysUser", loadUserName(t, src, config.SystemScope))
}
// Verify truthy/falsy parsing of GIT_CONFIG_NOSYSTEM matches git's
// git_env_bool semantics.
func TestGitBehaviour_NoSystemBooleans(t *testing.T) {
tests := []struct {
value string
want string // "" if NOSYSTEM is truthy, "SysUser" if falsy
}{
// Truthy values.
{"1", ""},
{"true", ""},
{"yes", ""},
{"on", ""},
{"TRUE", ""},
{"Yes", ""},
{"ON", ""},
// Falsy values.
{"0", "SysUser"},
{"false", "SysUser"},
{"no", "SysUser"},
{"off", "SysUser"},
{"FALSE", "SysUser"},
{"No", "SysUser"},
{"OFF", "SysUser"},
}
for _, tt := range tests {
t.Run(tt.value, func(t *testing.T) {
t.Setenv(envGitConfigSystem, "/custom/system.cfg")
t.Setenv(envGitConfigNoSystem, tt.value)
src := memAuto(t, map[string]string{
"/custom/system.cfg": "[user]\n\tname = SysUser\n",
})
assert.Equal(t, tt.want, loadUserName(t, src, config.SystemScope),
"GIT_CONFIG_NOSYSTEM=%q", tt.value)
})
}
}
func TestGitBehaviour_UnsupportedScope(t *testing.T) {
t.Parallel()
src := NewAuto(WithFilesystem(memfs.New()))
_, err := src.Load(config.LocalScope)
require.Error(t, err)
}
// setTestHome sets HOME (and USERPROFILE on Windows) so that
// os.UserHomeDir returns the given path on every platform.
func setTestHome(t *testing.T, home string) {
t.Helper()
t.Setenv("HOME", home)
if runtime.GOOS == "windows" {
t.Setenv("USERPROFILE", home)
}
}
// setTestAppData sets APPDATA on Windows so that the XDG fallback
// path is deterministic. No-op on other platforms.
func setTestAppData(t *testing.T, appData string) {
t.Helper()
if runtime.GOOS == "windows" {
t.Setenv("APPDATA", appData)
}
}
// memAuto returns an Auto backed by an in-memory filesystem pre-populated
// with the given path→content pairs.
func memAuto(t *testing.T, files map[string]string) *auto {
t.Helper()
fs := memfs.New()
for p, c := range files {
require.NoError(t, util.WriteFile(fs, p, []byte(c), 0o644))
}
return NewAuto(WithFilesystem(fs))
}
// loadUserName is a convenience that loads the given scope and returns
// cfg.User.Name.
func loadUserName(t *testing.T, src *auto, scope config.Scope) string {
t.Helper()
s, err := src.Load(scope)
require.NoError(t, err)
cfg, err := s.Config()
require.NoError(t, err)
return cfg.User.Name
}
// loadUserEmail is a convenience that loads the given scope and returns
// cfg.User.Email.
func loadUserEmail(t *testing.T, src *auto, scope config.Scope) string {
t.Helper()
s, err := src.Load(scope)
require.NoError(t, err)
cfg, err := s.Config()
require.NoError(t, err)
return cfg.User.Email
}