Debian Codegraph @

config_edge_cases_test.go

raw
1 package gitconfig
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "runtime"
8 "strings"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 )
14
15 // TestEdgeCaseUnicodeKeys tests handling of special characters in keys.
16 func TestEdgeCaseUnicodeKeys(t *testing.T) {
17 t.Parallel()
18
19 td := t.TempDir()
20 configPath := filepath.Join(td, "config")
21
22 // Config with various key formats (git config keys are typically alphanumeric + dash/dot)
23 content := `[user]
24 name = John Doe
25 [core]
26 ignorecase = true`
27
28 err := os.WriteFile(configPath, []byte(content), 0o644)
29 require.NoError(t, err)
30
31 cfg, err := LoadConfig(configPath)
32 require.NoError(t, err)
33 require.NotNil(t, cfg)
34
35 // Standard keys should be accessible
36 val, ok := cfg.Get("user.name")
37 assert.True(t, ok)
38 assert.Equal(t, "John Doe", val)
39
40 val, ok = cfg.Get("core.ignorecase")
41 assert.True(t, ok)
42 assert.Equal(t, "true", val)
43 }
44
45 // TestEdgeCaseVeryLongValues tests handling of very long configuration values.
46 func TestEdgeCaseVeryLongValues(t *testing.T) {
47 t.Parallel()
48
49 td := t.TempDir()
50 configPath := filepath.Join(td, "config")
51
52 // Create a very long value (e.g., 10KB)
53 longValue := strings.Repeat("x", 10000)
54 content := "[section]\n\tkey = " + longValue + "\n"
55
56 err := os.WriteFile(configPath, []byte(content), 0o644)
57 require.NoError(t, err)
58
59 cfg, err := LoadConfig(configPath)
60 require.NoError(t, err)
61 require.NotNil(t, cfg)
62
63 val, ok := cfg.Get("section.key")
64 assert.True(t, ok)
65 assert.Equal(t, longValue, val)
66 }
67
68 // TestEdgeCaseVeryDeepSections tests handling of deeply nested section hierarchies.
69 func TestEdgeCaseVeryDeepSections(t *testing.T) {
70 t.Parallel()
71
72 td := t.TempDir()
73 configPath := filepath.Join(td, "config")
74
75 // Config with multiple nested levels
76 content := `[core]
77 key1 = value1
78 [filter "smudge"]
79 command = git convert-smudge
80 [filter "clean"]
81 command = git convert-clean
82 [user]
83 name = Test User
84 email = [email protected]
85 [remote "origin"]
86 url = https://github.com/test/repo.git
87 fetch = +refs/heads/*:refs/remotes/origin/*`
88
89 err := os.WriteFile(configPath, []byte(content), 0o644)
90 require.NoError(t, err)
91
92 cfg, err := LoadConfig(configPath)
93 require.NoError(t, err)
94 require.NotNil(t, cfg)
95
96 // Verify subsection access works
97 val, ok := cfg.Get("filter.smudge.command")
98 assert.True(t, ok)
99 assert.Equal(t, "git convert-smudge", val)
100
101 val, ok = cfg.Get("remote.origin.url")
102 assert.True(t, ok)
103 assert.Equal(t, "https://github.com/test/repo.git", val)
104 }
105
106 // TestEdgeCaseEmptyValues tests handling of empty string values.
107 func TestEdgeCaseEmptyValues(t *testing.T) {
108 t.Parallel()
109
110 td := t.TempDir()
111 configPath := filepath.Join(td, "config")
112
113 // Config with empty values
114 content := `[section]
115 empty =
116 noSpace=
117 quoted = ""
118 whitespace = `
119
120 err := os.WriteFile(configPath, []byte(content), 0o644)
121 require.NoError(t, err)
122
123 cfg, err := LoadConfig(configPath)
124 require.NoError(t, err)
125 require.NotNil(t, cfg)
126
127 // Empty values should return empty string
128 val, ok := cfg.Get("section.empty")
129 assert.True(t, ok)
130 assert.Empty(t, val)
131
132 val, ok = cfg.Get("section.noSpace")
133 assert.True(t, ok)
134 assert.Empty(t, val)
135
136 _, ok = cfg.Get("section.quoted")
137 // Quoted empty string should be present
138 assert.True(t, ok)
139 }
140
141 // TestEdgeCaseWhitespacePreservation tests that whitespace in values is preserved or handled correctly.
142 func TestEdgeCaseWhitespacePreservation(t *testing.T) {
143 t.Parallel()
144
145 td := t.TempDir()
146 configPath := filepath.Join(td, "config")
147
148 // Config with various whitespace scenarios
149 content := `[section]
150 leading = value
151 internal = value with spaces
152 trailing = value
153 tabs = value with tabs`
154
155 err := os.WriteFile(configPath, []byte(content), 0o644)
156 require.NoError(t, err)
157
158 cfg, err := LoadConfig(configPath)
159 require.NoError(t, err)
160 require.NotNil(t, cfg)
161
162 // Leading whitespace should be trimmed
163 val, ok := cfg.Get("section.leading")
164 assert.True(t, ok)
165 assert.Equal(t, "value", val)
166
167 // Internal spaces should be preserved
168 val, ok = cfg.Get("section.internal")
169 assert.True(t, ok)
170 assert.Equal(t, "value with spaces", val)
171 }
172
173 // TestEdgeCaseSpecialCharactersInValues tests handling of special characters.
174 func TestEdgeCaseSpecialCharactersInValues(t *testing.T) {
175 t.Parallel()
176
177 td := t.TempDir()
178 configPath := filepath.Join(td, "config")
179
180 // Config with special characters in values
181 content := `[section]
182 semicolon = value;with;semicolons
183 hash = value#with#hash
184 equals = key=value
185 brackets = [value]
186 quotes = value"with"quotes
187 backslash = C:\\Users\\test\\path`
188
189 err := os.WriteFile(configPath, []byte(content), 0o644)
190 require.NoError(t, err)
191
192 cfg, err := LoadConfig(configPath)
193 require.NoError(t, err)
194 require.NotNil(t, cfg)
195
196 // Semicolons should be preserved in unquoted values (or may start comment)
197 val, ok := cfg.Get("section.semicolon")
198 assert.True(t, ok)
199 // Behavior depends on parser - either full value or up to semicolon
200 assert.NotEmpty(t, val)
201
202 // Hash may start comment or be literal depending on quoting
203 val, ok = cfg.Get("section.hash")
204 if ok {
205 assert.NotEmpty(t, val)
206 }
207
208 // Backslash handling
209 val, ok = cfg.Get("section.backslash")
210 assert.True(t, ok)
211 // Value should contain the path
212 assert.True(t, strings.Contains(val, "Users") || strings.Contains(val, "\\\\"))
213 }
214
215 // TestEdgeCaseCommentHandling tests behavior with comments in various positions.
216 func TestEdgeCaseCommentHandling(t *testing.T) {
217 t.Parallel()
218
219 td := t.TempDir()
220 configPath := filepath.Join(td, "config")
221
222 // Config with various comment styles
223 content := `# File comment
224 [section]
225 # This is a comment
226 key1 = value1
227 key2 = value2 # inline comment?
228 ; semicolon comment
229 key3 = value3`
230
231 err := os.WriteFile(configPath, []byte(content), 0o644)
232 require.NoError(t, err)
233
234 cfg, err := LoadConfig(configPath)
235 require.NoError(t, err)
236 require.NotNil(t, cfg)
237
238 // All keys should be accessible
239 val, ok := cfg.Get("section.key1")
240 assert.True(t, ok)
241 assert.Equal(t, "value1", val)
242
243 val, ok = cfg.Get("section.key2")
244 assert.True(t, ok)
245 // Should not include the inline comment
246 assert.Equal(t, "value2", val)
247
248 val, ok = cfg.Get("section.key3")
249 assert.True(t, ok)
250 assert.Equal(t, "value3", val)
251 }
252
253 // TestEdgeCaseMultilineValues tests handling of multiline values (if supported).
254 func TestEdgeCaseMultilineValues(t *testing.T) {
255 t.Parallel()
256
257 td := t.TempDir()
258 configPath := filepath.Join(td, "config")
259
260 // Multiline values with continuation or quoted strings
261 content := `[section]
262 singleline = value on one line
263 quoted = "value on multiple lines
264 can continue here"
265 continuation = value \
266 that continues \
267 on next lines`
268
269 err := os.WriteFile(configPath, []byte(content), 0o644)
270 require.NoError(t, err)
271
272 cfg, err := LoadConfig(configPath)
273 require.NoError(t, err)
274 require.NotNil(t, cfg)
275
276 val, ok := cfg.Get("section.singleline")
277 assert.True(t, ok)
278 assert.Equal(t, "value on one line", val)
279 }
280
281 // TestEdgeCaseNumericValues tests handling of numeric-looking values.
282 func TestEdgeCaseNumericValues(t *testing.T) {
283 t.Parallel()
284
285 td := t.TempDir()
286 configPath := filepath.Join(td, "config")
287
288 // Numeric-looking values should be treated as strings
289 content := `[section]
290 integer = 42
291 float = 3.14159
292 octal = 0755
293 hex = 0xFF
294 negative = -100
295 scientific = 1.5e-10`
296
297 err := os.WriteFile(configPath, []byte(content), 0o644)
298 require.NoError(t, err)
299
300 cfg, err := LoadConfig(configPath)
301 require.NoError(t, err)
302 require.NotNil(t, cfg)
303
304 // All should be accessible as strings
305 val, ok := cfg.Get("section.integer")
306 assert.True(t, ok)
307 assert.Equal(t, "42", val)
308
309 val, ok = cfg.Get("section.float")
310 assert.True(t, ok)
311 assert.Equal(t, "3.14159", val)
312
313 val, ok = cfg.Get("section.octal")
314 assert.True(t, ok)
315 assert.Equal(t, "0755", val)
316 }
317
318 // TestEdgeCaseBooleanValues tests handling of boolean representations.
319 func TestEdgeCaseBooleanValues(t *testing.T) {
320 t.Parallel()
321
322 td := t.TempDir()
323 configPath := filepath.Join(td, "config")
324
325 // Various boolean representations
326 content := `[section]
327 enabled = true
328 disabled = false
329 yes = yes
330 no = no
331 on = on
332 off = off
333 one = 1
334 zero = 0`
335
336 err := os.WriteFile(configPath, []byte(content), 0o644)
337 require.NoError(t, err)
338
339 cfg, err := LoadConfig(configPath)
340 require.NoError(t, err)
341 require.NotNil(t, cfg)
342
343 // All should be accessible as strings
344 val, ok := cfg.Get("section.enabled")
345 assert.True(t, ok)
346 assert.Equal(t, "true", val)
347
348 val, ok = cfg.Get("section.disabled")
349 assert.True(t, ok)
350 assert.Equal(t, "false", val)
351
352 val, ok = cfg.Get("section.zero")
353 assert.True(t, ok)
354 assert.Equal(t, "0", val)
355 }
356
357 // TestEdgeCaseLargeConfigFile tests handling of large configuration files.
358 func TestEdgeCaseLargeConfigFile(t *testing.T) {
359 t.Parallel()
360
361 td := t.TempDir()
362 configPath := filepath.Join(td, "config")
363
364 // Generate a large config with many sections and keys
365 var sb strings.Builder
366 for i := range 20 {
367 fmt.Fprintf(&sb, "[section%d]\n", i)
368 for j := range 5 {
369 fmt.Fprintf(&sb, "\tkey%d = value_%d_%d\n", j, i, j)
370 }
371 }
372
373 err := os.WriteFile(configPath, []byte(sb.String()), 0o644)
374 require.NoError(t, err)
375
376 cfg, err := LoadConfig(configPath)
377 require.NoError(t, err)
378 require.NotNil(t, cfg)
379
380 // Spot check some values
381 val, ok := cfg.Get("section0.key0")
382 assert.True(t, ok)
383 assert.NotEmpty(t, val)
384
385 val, ok = cfg.Get("section10.key3")
386 assert.True(t, ok)
387 assert.NotEmpty(t, val)
388 }
389
390 // TestEdgeCaseDuplicateKeys tests handling of duplicate keys in configuration.
391 func TestEdgeCaseDuplicateKeys(t *testing.T) {
392 t.Parallel()
393
394 td := t.TempDir()
395 configPath := filepath.Join(td, "config")
396
397 // Config with duplicate keys
398 content := `[section]
399 key = value1
400 other = value
401 key = value2
402 key = value3`
403
404 err := os.WriteFile(configPath, []byte(content), 0o644)
405 require.NoError(t, err)
406
407 cfg, err := LoadConfig(configPath)
408 require.NoError(t, err)
409 require.NotNil(t, cfg)
410
411 // Get should return a value (likely the last one)
412 val, ok := cfg.Get("section.key")
413 assert.True(t, ok)
414 assert.NotEmpty(t, val)
415
416 // GetAll should return all values
417 vals, ok := cfg.GetAll("section.key")
418 assert.True(t, ok)
419 assert.GreaterOrEqual(t, len(vals), 1)
420 }
421
422 // TestEdgeCaseCaseSensitivity tests key case sensitivity (typically case-insensitive).
423 func TestEdgeCaseCaseSensitivity(t *testing.T) {
424 t.Parallel()
425
426 td := t.TempDir()
427 configPath := filepath.Join(td, "config")
428
429 // Config with various case keys
430 content := `[section]
431 key = value
432 Key = VALUE
433 KEY = VALUE_UPPER`
434
435 err := os.WriteFile(configPath, []byte(content), 0o644)
436 require.NoError(t, err)
437
438 cfg, err := LoadConfig(configPath)
439 require.NoError(t, err)
440 require.NotNil(t, cfg)
441
442 // Git config treats keys case-insensitively (for keys, not sections usually)
443 val, ok := cfg.Get("section.key")
444 assert.True(t, ok)
445 assert.NotEmpty(t, val)
446 }
447
448 // TestEdgeCaseWindowsLineEndings tests handling of Windows (CRLF) line endings.
449 func TestEdgeCaseWindowsLineEndings(t *testing.T) {
450 t.Parallel()
451
452 td := t.TempDir()
453 configPath := filepath.Join(td, "config")
454
455 // Config with CRLF line endings
456 content := "[section]\r\n\tkey = value\r\n\tother = test"
457
458 err := os.WriteFile(configPath, []byte(content), 0o644)
459 require.NoError(t, err)
460
461 cfg, err := LoadConfig(configPath)
462 require.NoError(t, err)
463 require.NotNil(t, cfg)
464
465 val, ok := cfg.Get("section.key")
466 assert.True(t, ok)
467 assert.Equal(t, "value", val)
468
469 val, ok = cfg.Get("section.other")
470 assert.True(t, ok)
471 assert.Equal(t, "test", val)
472 }
473
474 // TestEdgeCaseLeadingTrailingWhitespace tests config files with leading/trailing whitespace.
475 func TestEdgeCaseLeadingTrailingWhitespace(t *testing.T) {
476 t.Parallel()
477
478 td := t.TempDir()
479 configPath := filepath.Join(td, "config")
480
481 // Config with extra whitespace
482 content := " \n \n[section]\n\tkey = value\n\n\n"
483
484 err := os.WriteFile(configPath, []byte(content), 0o644)
485 require.NoError(t, err)
486
487 cfg, err := LoadConfig(configPath)
488 require.NoError(t, err)
489 require.NotNil(t, cfg)
490
491 val, ok := cfg.Get("section.key")
492 assert.True(t, ok)
493 assert.Equal(t, "value", val)
494 }
495
496 // TestEdgeCaseNullBytes tests handling of null bytes in configuration.
497 func TestEdgeCaseNullBytes(t *testing.T) {
498 // This test may be platform-specific
499 if runtime.GOOS == "windows" {
500 t.Skip("Null byte handling may differ on Windows")
501 }
502
503 t.Parallel()
504
505 td := t.TempDir()
506 configPath := filepath.Join(td, "config")
507
508 // Config with null byte (should not normally appear)
509 content := "[section]\nkey = value\x00end"
510
511 err := os.WriteFile(configPath, []byte(content), 0o644)
512 require.NoError(t, err)
513
514 cfg, err := LoadConfig(configPath)
515 // Behavior: either error or handle gracefully
516 if err != nil {
517 return
518 }
519
520 require.NotNil(t, cfg)
521 val, ok := cfg.Get("section.key")
522 // If it loads, value should be approximately "value"
523 if ok {
524 assert.True(t, strings.HasPrefix(val, "value"))
525 }
526 }