-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_include_errors_test.go
More file actions
318 lines (253 loc) · 8.78 KB
/
config_include_errors_test.go
File metadata and controls
318 lines (253 loc) · 8.78 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
package gitconfig
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestIncludeFileNotFound tests behavior when included files don't exist.
func TestIncludeFileNotFound(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Config with include directive pointing to non-existent file
content := `[include]
path = nonexistent.conf
[user]
name = Test
`
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
// LoadConfig should report the include error
cfg, err := LoadConfig(configPath)
if err != nil {
// Acceptable to error on missing include
assert.Error(t, err)
} else if cfg != nil {
// Or may skip the include silently - check behavior is consistent
assert.NotNil(t, cfg)
}
}
// TestIncludePermissionDenied tests behavior when included files are unreadable.
func TestIncludePermissionDenied(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")
includePath := filepath.Join(td, "include.conf")
// Create include file
err := os.WriteFile(includePath, []byte("[section]\n\tkey = value"), 0o644)
require.NoError(t, err)
// Create main config with include
content := "[include]\n\tpath = " + includePath + "\n[user]\n\tname = Test"
err = os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
// Make include file unreadable
err = os.Chmod(includePath, 0o000)
require.NoError(t, err)
// Should error on permission
cfg, err := LoadConfig(configPath)
// Restore for cleanup
_ = os.Chmod(includePath, 0o644)
if err != nil {
// Acceptable to error
assert.Error(t, err)
} else if cfg != nil {
// Or may handle gracefully - verify some state
assert.NotNil(t, cfg)
}
}
// TestIncludeCircular tests behavior with circular include references.
func TestIncludeCircular(t *testing.T) {
t.Parallel()
td := t.TempDir()
configA := filepath.Join(td, "config-a")
configB := filepath.Join(td, "config-b")
// Create circular includes: A -> B -> A
contentA := "[include]\n\tpath = " + configB + "\n[section]\n\tkey = a"
contentB := "[include]\n\tpath = " + configA + "\n[section]\n\tkey = b"
err := os.WriteFile(configA, []byte(contentA), 0o644)
require.NoError(t, err)
err = os.WriteFile(configB, []byte(contentB), 0o644)
require.NoError(t, err)
// Behavior: either errors on circular include or handles gracefully
cfg, err := LoadConfig(configA)
if err != nil {
// Acceptable to detect and error
assert.Error(t, err)
} else {
// Or succeeds with some depth limit
assert.NotNil(t, cfg)
}
}
// TestIncludeRelativePath tests relative path resolution in includes.
func TestIncludeRelativePath(t *testing.T) {
t.Parallel()
td := t.TempDir()
subdir := filepath.Join(td, "configs")
err := os.MkdirAll(subdir, 0o755)
require.NoError(t, err)
// Create included config in subdirectory
includePath := filepath.Join(subdir, "included.conf")
err = os.WriteFile(includePath, []byte("[core]\n\teditor = vim"), 0o644)
require.NoError(t, err)
// Main config with relative path include
configPath := filepath.Join(td, "config")
// Relative paths are typically resolved from the directory of the config file
content := "[include]\n\tpath = configs/included.conf\n[user]\n\tname = Test"
err = os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
if err != nil {
// Relative path resolution might fail
return
}
require.NotNil(t, cfg)
// If successfully loaded, verify included value is present
editor, ok := cfg.Get("core.editor")
if ok {
assert.Equal(t, "vim", editor)
}
}
// TestIncludeAbsolutePath tests absolute path resolution in includes.
func TestIncludeAbsolutePath(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create include file with absolute path
includePath := filepath.Join(td, "include.conf")
err := os.WriteFile(includePath, []byte("[core]\n\tpager = less"), 0o644)
require.NoError(t, err)
// Main config with absolute path include
content := "[include]\n\tpath = " + filepath.ToSlash(includePath) + "\n[user]\n\tname = Test"
err = os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
// Verify included value is present
pager, ok := cfg.Get("core.pager")
assert.True(t, ok)
assert.Equal(t, "less", pager)
// Verify main config value is present
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Test", name)
}
// TestIncludeMultipleFiles tests including multiple config files.
func TestIncludeMultipleFiles(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create multiple include files
include1 := filepath.Join(td, "config1.conf")
include2 := filepath.Join(td, "config2.conf")
err := os.WriteFile(include1, []byte("[core]\n\teditor = vim"), 0o644)
require.NoError(t, err)
err = os.WriteFile(include2, []byte("[core]\n\tpager = less"), 0o644)
require.NoError(t, err)
// Main config including multiple files
content := "[include]\n\tpath = " + filepath.ToSlash(include1) + "\n[include]\n\tpath = " + filepath.ToSlash(include2) + "\n[user]\n\tname = Test"
err = os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
// Verify all included values are present
editor, ok := cfg.Get("core.editor")
assert.True(t, ok)
assert.Equal(t, "vim", editor)
pager, ok := cfg.Get("core.pager")
assert.True(t, ok)
assert.Equal(t, "less", pager)
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Test", name)
}
// TestIncludeOverride tests include file precedence and value merging.
func TestIncludeOverride(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create include files with overlapping and unique keys
include1 := filepath.Join(td, "base.conf")
include2 := filepath.Join(td, "override.conf")
err := os.WriteFile(include1, []byte("[core]\n\teditor = vim\n\tpager = less"), 0o644)
require.NoError(t, err)
err = os.WriteFile(include2, []byte("[core]\n\teditor = nano"), 0o644)
require.NoError(t, err)
// Main config includes files in order
content := "[include]\n\tpath = " + filepath.ToSlash(include1) + "\n[include]\n\tpath = " + filepath.ToSlash(include2)
err = os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
// Verify values from includes are loaded
editor, ok := cfg.Get("core.editor")
assert.True(t, ok)
// Value may depend on implementation; check it exists and is one of the included values
assert.Contains(t, []string{"vim", "nano"}, editor)
// Pager from first include should be present
pager, ok := cfg.Get("core.pager")
assert.True(t, ok)
assert.Equal(t, "less", pager)
}
// TestIncludeWithConditional tests conditional include directives.
func TestIncludeWithConditional(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
gitDir := filepath.Join(td, ".git")
err := os.MkdirAll(gitDir, 0o755)
require.NoError(t, err)
// Create work-specific config
workConfig := filepath.Join(td, "work.conf")
err = os.WriteFile(workConfig, []byte("[user]\n\temail = work@company.com"), 0o644)
require.NoError(t, err)
// Main config with conditional include
// Note: Conditional syntax might be [includeIf "gitdir:..."]
content := `[user]
email = personal@example.com
[includeIf "gitdir:` + gitDir + `/"]
path = ` + workConfig
err = os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfigWithWorkdir(configPath, td)
if err != nil {
// Not all implementations support conditional includes
t.Skip("Conditional includes not supported")
}
require.NotNil(t, cfg)
// Verify that the conditional include was applied
email, ok := cfg.Get("user.email")
assert.True(t, ok)
// Should have work email if gitdir condition matched
assert.NotEmpty(t, email)
}
// TestIncludeEmptyPath tests handling of includes with empty paths.
func TestIncludeEmptyPath(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Config with empty include path
content := `[include]
path =
[user]
name = Test`
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
// This should either error or be ignored
cfg, err := LoadConfig(configPath)
if err != nil {
// Acceptable to reject invalid syntax
assert.Error(t, err)
} else if cfg != nil {
// Or silently ignore empty path
assert.NotNil(t, cfg)
}
}