Debian Codegraph @

config_include_errors_test.go

raw
1 package gitconfig
2
3 import (
4 "os"
5 "path/filepath"
6 "runtime"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 )
12
13 // TestIncludeFileNotFound tests behavior when included files don't exist.
14 func TestIncludeFileNotFound(t *testing.T) {
15 t.Parallel()
16
17 td := t.TempDir()
18 configPath := filepath.Join(td, "config")
19
20 // Config with include directive pointing to non-existent file
21 content := `[include]
22 path = nonexistent.conf
23 [user]
24 name = Test
25 `
26 err := os.WriteFile(configPath, []byte(content), 0o644)
27 require.NoError(t, err)
28
29 // LoadConfig should report the include error
30 cfg, err := LoadConfig(configPath)
31 if err != nil {
32 // Acceptable to error on missing include
33 assert.Error(t, err)
34 } else if cfg != nil {
35 // Or may skip the include silently - check behavior is consistent
36 assert.NotNil(t, cfg)
37 }
38 }
39
40 // TestIncludePermissionDenied tests behavior when included files are unreadable.
41 func TestIncludePermissionDenied(t *testing.T) {
42 t.Parallel()
43
44 if runtime.GOOS == "windows" {
45 t.Skip("Permission test not reliable on Windows")
46 }
47
48 td := t.TempDir()
49 configPath := filepath.Join(td, "config")
50 includePath := filepath.Join(td, "include.conf")
51
52 // Create include file
53 err := os.WriteFile(includePath, []byte("[section]\n\tkey = value"), 0o644)
54 require.NoError(t, err)
55
56 // Create main config with include
57 content := "[include]\n\tpath = " + includePath + "\n[user]\n\tname = Test"
58 err = os.WriteFile(configPath, []byte(content), 0o644)
59 require.NoError(t, err)
60
61 // Make include file unreadable
62 err = os.Chmod(includePath, 0o000)
63 require.NoError(t, err)
64
65 // Should error on permission
66 cfg, err := LoadConfig(configPath)
67
68 // Restore for cleanup
69 _ = os.Chmod(includePath, 0o644)
70
71 if err != nil {
72 // Acceptable to error
73 assert.Error(t, err)
74 } else if cfg != nil {
75 // Or may handle gracefully - verify some state
76 assert.NotNil(t, cfg)
77 }
78 }
79
80 // TestIncludeCircular tests behavior with circular include references.
81 func TestIncludeCircular(t *testing.T) {
82 t.Parallel()
83
84 td := t.TempDir()
85 configA := filepath.Join(td, "config-a")
86 configB := filepath.Join(td, "config-b")
87
88 // Create circular includes: A -> B -> A
89 contentA := "[include]\n\tpath = " + configB + "\n[section]\n\tkey = a"
90 contentB := "[include]\n\tpath = " + configA + "\n[section]\n\tkey = b"
91
92 err := os.WriteFile(configA, []byte(contentA), 0o644)
93 require.NoError(t, err)
94
95 err = os.WriteFile(configB, []byte(contentB), 0o644)
96 require.NoError(t, err)
97
98 // Behavior: either errors on circular include or handles gracefully
99 cfg, err := LoadConfig(configA)
100
101 if err != nil {
102 // Acceptable to detect and error
103 assert.Error(t, err)
104 } else {
105 // Or succeeds with some depth limit
106 assert.NotNil(t, cfg)
107 }
108 }
109
110 // TestIncludeRelativePath tests relative path resolution in includes.
111 func TestIncludeRelativePath(t *testing.T) {
112 t.Parallel()
113
114 td := t.TempDir()
115 subdir := filepath.Join(td, "configs")
116 err := os.MkdirAll(subdir, 0o755)
117 require.NoError(t, err)
118
119 // Create included config in subdirectory
120 includePath := filepath.Join(subdir, "included.conf")
121 err = os.WriteFile(includePath, []byte("[core]\n\teditor = vim"), 0o644)
122 require.NoError(t, err)
123
124 // Main config with relative path include
125 configPath := filepath.Join(td, "config")
126 // Relative paths are typically resolved from the directory of the config file
127 content := "[include]\n\tpath = configs/included.conf\n[user]\n\tname = Test"
128 err = os.WriteFile(configPath, []byte(content), 0o644)
129 require.NoError(t, err)
130
131 cfg, err := LoadConfig(configPath)
132 if err != nil {
133 // Relative path resolution might fail
134 return
135 }
136
137 require.NotNil(t, cfg)
138 // If successfully loaded, verify included value is present
139 editor, ok := cfg.Get("core.editor")
140 if ok {
141 assert.Equal(t, "vim", editor)
142 }
143 }
144
145 // TestIncludeAbsolutePath tests absolute path resolution in includes.
146 func TestIncludeAbsolutePath(t *testing.T) {
147 t.Parallel()
148
149 td := t.TempDir()
150 configPath := filepath.Join(td, "config")
151
152 // Create include file with absolute path
153 includePath := filepath.Join(td, "include.conf")
154 err := os.WriteFile(includePath, []byte("[core]\n\tpager = less"), 0o644)
155 require.NoError(t, err)
156
157 // Main config with absolute path include
158 content := "[include]\n\tpath = " + filepath.ToSlash(includePath) + "\n[user]\n\tname = Test"
159 err = os.WriteFile(configPath, []byte(content), 0o644)
160 require.NoError(t, err)
161
162 cfg, err := LoadConfig(configPath)
163 require.NoError(t, err)
164 require.NotNil(t, cfg)
165
166 // Verify included value is present
167 pager, ok := cfg.Get("core.pager")
168 assert.True(t, ok)
169 assert.Equal(t, "less", pager)
170
171 // Verify main config value is present
172 name, ok := cfg.Get("user.name")
173 assert.True(t, ok)
174 assert.Equal(t, "Test", name)
175 }
176
177 // TestIncludeMultipleFiles tests including multiple config files.
178 func TestIncludeMultipleFiles(t *testing.T) {
179 t.Parallel()
180
181 td := t.TempDir()
182 configPath := filepath.Join(td, "config")
183
184 // Create multiple include files
185 include1 := filepath.Join(td, "config1.conf")
186 include2 := filepath.Join(td, "config2.conf")
187
188 err := os.WriteFile(include1, []byte("[core]\n\teditor = vim"), 0o644)
189 require.NoError(t, err)
190
191 err = os.WriteFile(include2, []byte("[core]\n\tpager = less"), 0o644)
192 require.NoError(t, err)
193
194 // Main config including multiple files
195 content := "[include]\n\tpath = " + filepath.ToSlash(include1) + "\n[include]\n\tpath = " + filepath.ToSlash(include2) + "\n[user]\n\tname = Test"
196 err = os.WriteFile(configPath, []byte(content), 0o644)
197 require.NoError(t, err)
198
199 cfg, err := LoadConfig(configPath)
200 require.NoError(t, err)
201 require.NotNil(t, cfg)
202
203 // Verify all included values are present
204 editor, ok := cfg.Get("core.editor")
205 assert.True(t, ok)
206 assert.Equal(t, "vim", editor)
207
208 pager, ok := cfg.Get("core.pager")
209 assert.True(t, ok)
210 assert.Equal(t, "less", pager)
211
212 name, ok := cfg.Get("user.name")
213 assert.True(t, ok)
214 assert.Equal(t, "Test", name)
215 }
216
217 // TestIncludeOverride tests include file precedence and value merging.
218 func TestIncludeOverride(t *testing.T) {
219 t.Parallel()
220
221 td := t.TempDir()
222 configPath := filepath.Join(td, "config")
223
224 // Create include files with overlapping and unique keys
225 include1 := filepath.Join(td, "base.conf")
226 include2 := filepath.Join(td, "override.conf")
227
228 err := os.WriteFile(include1, []byte("[core]\n\teditor = vim\n\tpager = less"), 0o644)
229 require.NoError(t, err)
230
231 err = os.WriteFile(include2, []byte("[core]\n\teditor = nano"), 0o644)
232 require.NoError(t, err)
233
234 // Main config includes files in order
235 content := "[include]\n\tpath = " + filepath.ToSlash(include1) + "\n[include]\n\tpath = " + filepath.ToSlash(include2)
236 err = os.WriteFile(configPath, []byte(content), 0o644)
237 require.NoError(t, err)
238
239 cfg, err := LoadConfig(configPath)
240 require.NoError(t, err)
241 require.NotNil(t, cfg)
242
243 // Verify values from includes are loaded
244 editor, ok := cfg.Get("core.editor")
245 assert.True(t, ok)
246 // Value may depend on implementation; check it exists and is one of the included values
247 assert.Contains(t, []string{"vim", "nano"}, editor)
248
249 // Pager from first include should be present
250 pager, ok := cfg.Get("core.pager")
251 assert.True(t, ok)
252 assert.Equal(t, "less", pager)
253 }
254
255 // TestIncludeWithConditional tests conditional include directives.
256 func TestIncludeWithConditional(t *testing.T) {
257 t.Parallel()
258
259 td := t.TempDir()
260 configPath := filepath.Join(td, "config")
261 gitDir := filepath.Join(td, ".git")
262 err := os.MkdirAll(gitDir, 0o755)
263 require.NoError(t, err)
264
265 // Create work-specific config
266 workConfig := filepath.Join(td, "work.conf")
267 err = os.WriteFile(workConfig, []byte("[user]\n\temail = [email protected]"), 0o644)
268 require.NoError(t, err)
269
270 // Main config with conditional include
271 // Note: Conditional syntax might be [includeIf "gitdir:..."]
272 content := `[user]
273 email = [email protected]
274 [includeIf "gitdir:` + gitDir + `/"]
275 path = ` + workConfig
276 err = os.WriteFile(configPath, []byte(content), 0o644)
277 require.NoError(t, err)
278
279 cfg, err := LoadConfigWithWorkdir(configPath, td)
280 if err != nil {
281 // Not all implementations support conditional includes
282 t.Skip("Conditional includes not supported")
283 }
284
285 require.NotNil(t, cfg)
286 // Verify that the conditional include was applied
287 email, ok := cfg.Get("user.email")
288 assert.True(t, ok)
289 // Should have work email if gitdir condition matched
290 assert.NotEmpty(t, email)
291 }
292
293 // TestIncludeEmptyPath tests handling of includes with empty paths.
294 func TestIncludeEmptyPath(t *testing.T) {
295 t.Parallel()
296
297 td := t.TempDir()
298 configPath := filepath.Join(td, "config")
299
300 // Config with empty include path
301 content := `[include]
302 path =
303 [user]
304 name = Test`
305 err := os.WriteFile(configPath, []byte(content), 0o644)
306 require.NoError(t, err)
307
308 // This should either error or be ignored
309 cfg, err := LoadConfig(configPath)
310
311 if err != nil {
312 // Acceptable to reject invalid syntax
313 assert.Error(t, err)
314 } else if cfg != nil {
315 // Or silently ignore empty path
316 assert.NotNil(t, cfg)
317 }
318 }