-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_errors_test.go
More file actions
466 lines (361 loc) · 10.7 KB
/
config_errors_test.go
File metadata and controls
466 lines (361 loc) · 10.7 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package gitconfig
import (
"bytes"
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestConfigParseErrors tests error handling during config file parsing.
func TestConfigParseErrors(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
content string
}{
{
name: "valid config",
content: "[user]\n\tname = Test",
},
{
name: "empty file",
content: "",
},
{
name: "only comments",
content: "; This is a comment\n# Another comment",
},
{
name: "whitespace only",
content: " \n\t\n ",
},
{
name: "nested sections",
content: "[user]\n\tname = Test\n[core]\n\teditor = vim",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
err := os.WriteFile(configPath, []byte(tc.content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
// Most valid formats should load successfully
if err != nil {
return // Skip validation if load failed
}
assert.NotNil(t, cfg)
})
}
}
// TestConfigFileNotFound tests behavior when config file doesn't exist.
func TestConfigFileNotFound(t *testing.T) {
t.Parallel()
nonExistentPath := "/nonexistent/path/.git/config"
cfg, err := LoadConfig(nonExistentPath)
require.Error(t, err)
assert.Nil(t, cfg)
// Error should indicate file not found
assert.True(t, errors.Is(err, os.ErrNotExist) ||
strings.Contains(err.Error(), "no such file") ||
strings.Contains(err.Error(), "cannot find"))
}
// TestConfigPermissionDenied tests behavior when config file is not readable.
func TestConfigPermissionDenied(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("Permission test not reliable on Windows")
}
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create a readable config
err := os.WriteFile(configPath, []byte("[user]\n\tname = Test"), 0o644)
require.NoError(t, err)
// Make it unreadable
err = os.Chmod(configPath, 0o000)
require.NoError(t, err)
// Should get permission error
cfg, err := LoadConfig(configPath)
// Restore permissions for cleanup
_ = os.Chmod(configPath, 0o644)
require.Error(t, err)
assert.Nil(t, cfg)
assert.True(t, errors.Is(err, os.ErrPermission) ||
strings.Contains(err.Error(), "permission"))
}
// TestConfigFlushRawErrors tests error handling during write operations.
func TestConfigFlushRawErrors(t *testing.T) {
t.Parallel()
t.Run("flush to read-only file", func(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("Read-only file test not reliable on Windows")
}
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create initial config
content := "[user]\n\tname = Test"
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Make file read-only
err = os.Chmod(configPath, 0o444)
require.NoError(t, err)
// Try to write
_ = cfg.Set("user.email", "test@example.com")
err = cfg.flushRaw()
// Restore permissions
_ = os.Chmod(configPath, 0o644)
// Should get an error
assert.Error(t, err)
})
t.Run("flush to directory without permissions", func(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("Directory permission test not reliable on Windows")
}
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create initial config
content := "[user]\n\tname = Test"
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Remove write permission from directory
err = os.Chmod(td, 0o555)
require.NoError(t, err)
// Try to write
_ = cfg.Set("user.email", "test@example.com")
err = cfg.flushRaw()
// Restore permissions
_ = os.Chmod(td, 0o755)
// Should get an error or succeed (depending on implementation)
// The important thing is that the directory permissions are restored
_ = err
})
}
// TestSetGetErrors tests error handling for Set/Get operations.
func TestSetGetErrors(t *testing.T) {
t.Parallel()
t.Run("get invalid key format", func(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
err := os.WriteFile(configPath, []byte("[user]\n\tname = Test"), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Try to get with invalid key format (no dot separator)
value, ok := cfg.Get("invalid")
assert.False(t, ok)
assert.Empty(t, value)
})
t.Run("set with special characters", func(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
err := os.WriteFile(configPath, []byte(""), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Set with special characters
_ = cfg.Set("user.name", "Test User")
_ = cfg.Set("user.email", "test@example.com")
// Verify values are preserved
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Test User", name)
email, ok := cfg.Get("user.email")
assert.True(t, ok)
assert.Equal(t, "test@example.com", email)
})
t.Run("multivalue handling", func(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := `[user]
name = Test
multivalue = first
multivalue = second
`
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Get single value (should return first)
value, ok := cfg.Get("user.multivalue")
assert.True(t, ok)
assert.Equal(t, "first", value)
// GetAll should return both
values, ok := cfg.GetAll("user.multivalue")
assert.True(t, ok)
assert.Equal(t, []string{"first", "second"}, values)
})
t.Run("invalid key errors", func(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
err := os.WriteFile(configPath, []byte(""), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
err = cfg.Set("invalid", "value")
require.Error(t, err)
require.ErrorIs(t, err, ErrInvalidKey)
err = cfg.Unset("invalid")
require.Error(t, err)
require.ErrorIs(t, err, ErrInvalidKey)
})
}
func TestConfigsWorkdirErrors(t *testing.T) {
t.Parallel()
cs := New()
err := cs.SetLocal("core.editor", "vim")
require.Error(t, err)
require.ErrorIs(t, err, ErrWorkdirNotSet)
}
// TestConfigUnsetErrors tests error handling for Unset operations.
func TestConfigUnsetErrors(t *testing.T) {
t.Parallel()
t.Run("unset then get returns not found", func(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := "[user]\n\tname = Test"
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Verify key exists
_, ok := cfg.Get("user.name")
require.True(t, ok)
// Unset the key
err = cfg.Unset("user.name")
require.NoError(t, err)
// Verify key is gone
_, ok = cfg.Get("user.name")
assert.False(t, ok)
})
}
// TestEmptyConfigurationPersistence tests loading and setting on initially empty config.
func TestEmptyConfigurationPersistence(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create empty config file
err := os.WriteFile(configPath, []byte("[user]\n\tname = Initial"), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
// Verify initial value
initial, ok := cfg.Get("user.name")
require.True(t, ok)
assert.Equal(t, "Initial", initial)
// Modify and persist
err = cfg.Set("user.name", "Modified")
require.NoError(t, err)
err = cfg.flushRaw()
require.NoError(t, err)
// Reload and verify
cfg2, err := LoadConfig(configPath)
require.NoError(t, err)
value, ok := cfg2.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Modified", value)
}
// TestParseConfigFromReader tests parsing from io.Reader.
func TestParseConfigFromReader(t *testing.T) {
t.Parallel()
t.Run("parse valid config", func(t *testing.T) {
t.Parallel()
content := `[user]
name = Test User
email = test@example.com
[core]
editor = vim
`
cfg := ParseConfig(strings.NewReader(content))
require.NotNil(t, cfg)
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Test User", name)
editor, ok := cfg.Get("core.editor")
assert.True(t, ok)
assert.Equal(t, "vim", editor)
})
t.Run("parse config with comments", func(t *testing.T) {
t.Parallel()
content := `
# Global git configuration
[user]
# My name
name = Test User
; Email address
email = test@example.com
`
cfg := ParseConfig(strings.NewReader(content))
require.NotNil(t, cfg)
// Comments should be preserved in raw
rawContent := cfg.raw.String()
assert.Contains(t, rawContent, "Test User")
})
t.Run("parse empty reader", func(t *testing.T) {
t.Parallel()
cfg := ParseConfig(bytes.NewReader([]byte("")))
if cfg == nil {
t.Skip("ParseConfig returns nil for empty input")
}
// Note: ParseConfig may not mark an empty input as IsEmpty
// because it initializes the raw buffer
assert.NotNil(t, cfg)
})
}
// TestLoadConfigWithWorkdir tests loading config with workdir context.
func TestLoadConfigWithWorkdir(t *testing.T) {
t.Parallel()
td := t.TempDir()
gitDir := filepath.Join(td, ".git")
err := os.MkdirAll(gitDir, 0o755)
require.NoError(t, err)
configPath := filepath.Join(gitDir, "config")
content := "[user]\n\tname = Test"
err = os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
// LoadConfigWithWorkdir can resolve includes relative to workdir
cfg, err := LoadConfigWithWorkdir(configPath, td)
require.NoError(t, err)
require.NotNil(t, cfg)
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Test", name)
}
// TestConfigWithNoWrites tests noWrites flag.
func TestConfigWithNoWrites(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := "[user]\n\tname = Original"
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Set noWrites flag
cfg.noWrites = true
// Try to set and flush
_ = cfg.Set("user.name", "Modified")
_ = cfg.flushRaw()
// With noWrites, flushRaw should silently skip the write
// File should still have original content
fileContent, err := os.ReadFile(configPath)
require.NoError(t, err)
assert.Contains(t, string(fileContent), "Original")
}