| 1 |
package gitconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"runtime" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
) |
| 15 |
|
| 16 |
// TestConfigParseErrors tests error handling during config file parsing. |
| 17 |
func TestConfigParseErrors(t *testing.T) { |
| 18 |
t.Parallel() |
| 19 |
|
| 20 |
testCases := []struct { |
| 21 |
name string |
| 22 |
content string |
| 23 |
}{ |
| 24 |
{ |
| 25 |
name: "valid config", |
| 26 |
content: "[user]\n\tname = Test", |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "empty file", |
| 30 |
content: "", |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "only comments", |
| 34 |
content: "; This is a comment\n# Another comment", |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "whitespace only", |
| 38 |
content: " \n\t\n ", |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "nested sections", |
| 42 |
content: "[user]\n\tname = Test\n[core]\n\teditor = vim", |
| 43 |
}, |
| 44 |
} |
| 45 |
|
| 46 |
for _, tc := range testCases { |
| 47 |
t.Run(tc.name, func(t *testing.T) { |
| 48 |
t.Parallel() |
| 49 |
|
| 50 |
td := t.TempDir() |
| 51 |
configPath := filepath.Join(td, "config") |
| 52 |
|
| 53 |
err := os.WriteFile(configPath, []byte(tc.content), 0o644) |
| 54 |
require.NoError(t, err) |
| 55 |
|
| 56 |
cfg, err := LoadConfig(configPath) |
| 57 |
// Most valid formats should load successfully |
| 58 |
if err != nil { |
| 59 |
return // Skip validation if load failed |
| 60 |
} |
| 61 |
assert.NotNil(t, cfg) |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// TestConfigFileNotFound tests behavior when config file doesn't exist. |
| 67 |
func TestConfigFileNotFound(t *testing.T) { |
| 68 |
t.Parallel() |
| 69 |
|
| 70 |
nonExistentPath := "/nonexistent/path/.git/config" |
| 71 |
cfg, err := LoadConfig(nonExistentPath) |
| 72 |
|
| 73 |
require.Error(t, err) |
| 74 |
assert.Nil(t, cfg) |
| 75 |
|
| 76 |
// Error should indicate file not found |
| 77 |
assert.True(t, errors.Is(err, os.ErrNotExist) || |
| 78 |
strings.Contains(err.Error(), "no such file") || |
| 79 |
strings.Contains(err.Error(), "cannot find")) |
| 80 |
} |
| 81 |
|
| 82 |
// TestConfigPermissionDenied tests behavior when config file is not readable. |
| 83 |
func TestConfigPermissionDenied(t *testing.T) { |
| 84 |
t.Parallel() |
| 85 |
|
| 86 |
if runtime.GOOS == "windows" { |
| 87 |
t.Skip("Permission test not reliable on Windows") |
| 88 |
} |
| 89 |
|
| 90 |
td := t.TempDir() |
| 91 |
configPath := filepath.Join(td, "config") |
| 92 |
|
| 93 |
// Create a readable config |
| 94 |
err := os.WriteFile(configPath, []byte("[user]\n\tname = Test"), 0o644) |
| 95 |
require.NoError(t, err) |
| 96 |
|
| 97 |
// Make it unreadable |
| 98 |
err = os.Chmod(configPath, 0o000) |
| 99 |
require.NoError(t, err) |
| 100 |
|
| 101 |
// Should get permission error |
| 102 |
cfg, err := LoadConfig(configPath) |
| 103 |
|
| 104 |
// Restore permissions for cleanup |
| 105 |
_ = os.Chmod(configPath, 0o644) |
| 106 |
|
| 107 |
require.Error(t, err) |
| 108 |
assert.Nil(t, cfg) |
| 109 |
assert.True(t, errors.Is(err, os.ErrPermission) || |
| 110 |
strings.Contains(err.Error(), "permission")) |
| 111 |
} |
| 112 |
|
| 113 |
// TestConfigFlushRawErrors tests error handling during write operations. |
| 114 |
func TestConfigFlushRawErrors(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
|
| 117 |
t.Run("flush to read-only file", func(t *testing.T) { |
| 118 |
t.Parallel() |
| 119 |
|
| 120 |
if runtime.GOOS == "windows" { |
| 121 |
t.Skip("Read-only file test not reliable on Windows") |
| 122 |
} |
| 123 |
|
| 124 |
td := t.TempDir() |
| 125 |
configPath := filepath.Join(td, "config") |
| 126 |
|
| 127 |
// Create initial config |
| 128 |
content := "[user]\n\tname = Test" |
| 129 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 130 |
require.NoError(t, err) |
| 131 |
|
| 132 |
cfg, err := LoadConfig(configPath) |
| 133 |
require.NoError(t, err) |
| 134 |
|
| 135 |
// Make file read-only |
| 136 |
err = os.Chmod(configPath, 0o444) |
| 137 |
require.NoError(t, err) |
| 138 |
|
| 139 |
// Try to write |
| 140 |
_ = cfg.Set("user.email", "[email protected]") |
| 141 |
err = cfg.flushRaw() |
| 142 |
|
| 143 |
// Restore permissions |
| 144 |
_ = os.Chmod(configPath, 0o644) |
| 145 |
|
| 146 |
// Should get an error |
| 147 |
assert.Error(t, err) |
| 148 |
}) |
| 149 |
|
| 150 |
t.Run("flush to directory without permissions", func(t *testing.T) { |
| 151 |
t.Parallel() |
| 152 |
|
| 153 |
if runtime.GOOS == "windows" { |
| 154 |
t.Skip("Directory permission test not reliable on Windows") |
| 155 |
} |
| 156 |
|
| 157 |
td := t.TempDir() |
| 158 |
configPath := filepath.Join(td, "config") |
| 159 |
|
| 160 |
// Create initial config |
| 161 |
content := "[user]\n\tname = Test" |
| 162 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 163 |
require.NoError(t, err) |
| 164 |
|
| 165 |
cfg, err := LoadConfig(configPath) |
| 166 |
require.NoError(t, err) |
| 167 |
|
| 168 |
// Remove write permission from directory |
| 169 |
err = os.Chmod(td, 0o555) |
| 170 |
require.NoError(t, err) |
| 171 |
|
| 172 |
// Try to write |
| 173 |
_ = cfg.Set("user.email", "[email protected]") |
| 174 |
err = cfg.flushRaw() |
| 175 |
|
| 176 |
// Restore permissions |
| 177 |
_ = os.Chmod(td, 0o755) |
| 178 |
|
| 179 |
// Should get an error or succeed (depending on implementation) |
| 180 |
// The important thing is that the directory permissions are restored |
| 181 |
_ = err |
| 182 |
}) |
| 183 |
} |
| 184 |
|
| 185 |
// TestSetGetErrors tests error handling for Set/Get operations. |
| 186 |
func TestSetGetErrors(t *testing.T) { |
| 187 |
t.Parallel() |
| 188 |
|
| 189 |
t.Run("get invalid key format", func(t *testing.T) { |
| 190 |
t.Parallel() |
| 191 |
|
| 192 |
td := t.TempDir() |
| 193 |
configPath := filepath.Join(td, "config") |
| 194 |
|
| 195 |
err := os.WriteFile(configPath, []byte("[user]\n\tname = Test"), 0o644) |
| 196 |
require.NoError(t, err) |
| 197 |
|
| 198 |
cfg, err := LoadConfig(configPath) |
| 199 |
require.NoError(t, err) |
| 200 |
|
| 201 |
// Try to get with invalid key format (no dot separator) |
| 202 |
value, ok := cfg.Get("invalid") |
| 203 |
assert.False(t, ok) |
| 204 |
assert.Empty(t, value) |
| 205 |
}) |
| 206 |
|
| 207 |
t.Run("set with special characters", func(t *testing.T) { |
| 208 |
t.Parallel() |
| 209 |
|
| 210 |
td := t.TempDir() |
| 211 |
configPath := filepath.Join(td, "config") |
| 212 |
|
| 213 |
err := os.WriteFile(configPath, []byte(""), 0o644) |
| 214 |
require.NoError(t, err) |
| 215 |
|
| 216 |
cfg, err := LoadConfig(configPath) |
| 217 |
require.NoError(t, err) |
| 218 |
|
| 219 |
// Set with special characters |
| 220 |
_ = cfg.Set("user.name", "Test User") |
| 221 |
_ = cfg.Set("user.email", "[email protected]") |
| 222 |
|
| 223 |
// Verify values are preserved |
| 224 |
name, ok := cfg.Get("user.name") |
| 225 |
assert.True(t, ok) |
| 226 |
assert.Equal(t, "Test User", name) |
| 227 |
|
| 228 |
email, ok := cfg.Get("user.email") |
| 229 |
assert.True(t, ok) |
| 230 |
assert.Equal(t, "[email protected]", email) |
| 231 |
}) |
| 232 |
|
| 233 |
t.Run("multivalue handling", func(t *testing.T) { |
| 234 |
t.Parallel() |
| 235 |
|
| 236 |
td := t.TempDir() |
| 237 |
configPath := filepath.Join(td, "config") |
| 238 |
|
| 239 |
content := `[user] |
| 240 |
name = Test |
| 241 |
multivalue = first |
| 242 |
multivalue = second |
| 243 |
` |
| 244 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 245 |
require.NoError(t, err) |
| 246 |
|
| 247 |
cfg, err := LoadConfig(configPath) |
| 248 |
require.NoError(t, err) |
| 249 |
|
| 250 |
// Get single value (should return first) |
| 251 |
value, ok := cfg.Get("user.multivalue") |
| 252 |
assert.True(t, ok) |
| 253 |
assert.Equal(t, "first", value) |
| 254 |
|
| 255 |
// GetAll should return both |
| 256 |
values, ok := cfg.GetAll("user.multivalue") |
| 257 |
assert.True(t, ok) |
| 258 |
assert.Equal(t, []string{"first", "second"}, values) |
| 259 |
}) |
| 260 |
|
| 261 |
t.Run("invalid key errors", func(t *testing.T) { |
| 262 |
t.Parallel() |
| 263 |
|
| 264 |
td := t.TempDir() |
| 265 |
configPath := filepath.Join(td, "config") |
| 266 |
|
| 267 |
err := os.WriteFile(configPath, []byte(""), 0o644) |
| 268 |
require.NoError(t, err) |
| 269 |
|
| 270 |
cfg, err := LoadConfig(configPath) |
| 271 |
require.NoError(t, err) |
| 272 |
|
| 273 |
err = cfg.Set("invalid", "value") |
| 274 |
require.Error(t, err) |
| 275 |
require.ErrorIs(t, err, ErrInvalidKey) |
| 276 |
|
| 277 |
err = cfg.Unset("invalid") |
| 278 |
require.Error(t, err) |
| 279 |
require.ErrorIs(t, err, ErrInvalidKey) |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
func TestConfigsWorkdirErrors(t *testing.T) { |
| 284 |
t.Parallel() |
| 285 |
|
| 286 |
cs := New() |
| 287 |
err := cs.SetLocal("core.editor", "vim") |
| 288 |
require.Error(t, err) |
| 289 |
require.ErrorIs(t, err, ErrWorkdirNotSet) |
| 290 |
} |
| 291 |
|
| 292 |
// TestConfigUnsetErrors tests error handling for Unset operations. |
| 293 |
func TestConfigUnsetErrors(t *testing.T) { |
| 294 |
t.Parallel() |
| 295 |
|
| 296 |
t.Run("unset then get returns not found", func(t *testing.T) { |
| 297 |
t.Parallel() |
| 298 |
|
| 299 |
td := t.TempDir() |
| 300 |
configPath := filepath.Join(td, "config") |
| 301 |
|
| 302 |
content := "[user]\n\tname = Test" |
| 303 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 304 |
require.NoError(t, err) |
| 305 |
|
| 306 |
cfg, err := LoadConfig(configPath) |
| 307 |
require.NoError(t, err) |
| 308 |
|
| 309 |
// Verify key exists |
| 310 |
_, ok := cfg.Get("user.name") |
| 311 |
require.True(t, ok) |
| 312 |
|
| 313 |
// Unset the key |
| 314 |
err = cfg.Unset("user.name") |
| 315 |
require.NoError(t, err) |
| 316 |
|
| 317 |
// Verify key is gone |
| 318 |
_, ok = cfg.Get("user.name") |
| 319 |
assert.False(t, ok) |
| 320 |
}) |
| 321 |
} |
| 322 |
|
| 323 |
// TestEmptyConfigurationPersistence tests loading and setting on initially empty config. |
| 324 |
func TestEmptyConfigurationPersistence(t *testing.T) { |
| 325 |
t.Parallel() |
| 326 |
|
| 327 |
td := t.TempDir() |
| 328 |
configPath := filepath.Join(td, "config") |
| 329 |
|
| 330 |
// Create empty config file |
| 331 |
err := os.WriteFile(configPath, []byte("[user]\n\tname = Initial"), 0o644) |
| 332 |
require.NoError(t, err) |
| 333 |
|
| 334 |
cfg, err := LoadConfig(configPath) |
| 335 |
require.NoError(t, err) |
| 336 |
require.NotNil(t, cfg) |
| 337 |
|
| 338 |
// Verify initial value |
| 339 |
initial, ok := cfg.Get("user.name") |
| 340 |
require.True(t, ok) |
| 341 |
assert.Equal(t, "Initial", initial) |
| 342 |
|
| 343 |
// Modify and persist |
| 344 |
err = cfg.Set("user.name", "Modified") |
| 345 |
require.NoError(t, err) |
| 346 |
|
| 347 |
err = cfg.flushRaw() |
| 348 |
require.NoError(t, err) |
| 349 |
|
| 350 |
// Reload and verify |
| 351 |
cfg2, err := LoadConfig(configPath) |
| 352 |
require.NoError(t, err) |
| 353 |
|
| 354 |
value, ok := cfg2.Get("user.name") |
| 355 |
assert.True(t, ok) |
| 356 |
assert.Equal(t, "Modified", value) |
| 357 |
} |
| 358 |
|
| 359 |
// TestParseConfigFromReader tests parsing from io.Reader. |
| 360 |
func TestParseConfigFromReader(t *testing.T) { |
| 361 |
t.Parallel() |
| 362 |
|
| 363 |
t.Run("parse valid config", func(t *testing.T) { |
| 364 |
t.Parallel() |
| 365 |
|
| 366 |
content := `[user] |
| 367 |
name = Test User |
| 368 |
email = [email protected] |
| 369 |
[core] |
| 370 |
editor = vim |
| 371 |
` |
| 372 |
cfg := ParseConfig(strings.NewReader(content)) |
| 373 |
require.NotNil(t, cfg) |
| 374 |
|
| 375 |
name, ok := cfg.Get("user.name") |
| 376 |
assert.True(t, ok) |
| 377 |
assert.Equal(t, "Test User", name) |
| 378 |
|
| 379 |
editor, ok := cfg.Get("core.editor") |
| 380 |
assert.True(t, ok) |
| 381 |
assert.Equal(t, "vim", editor) |
| 382 |
}) |
| 383 |
|
| 384 |
t.Run("parse config with comments", func(t *testing.T) { |
| 385 |
t.Parallel() |
| 386 |
|
| 387 |
content := ` |
| 388 |
# Global git configuration |
| 389 |
[user] |
| 390 |
# My name |
| 391 |
name = Test User |
| 392 |
; Email address |
| 393 |
email = [email protected] |
| 394 |
` |
| 395 |
cfg := ParseConfig(strings.NewReader(content)) |
| 396 |
require.NotNil(t, cfg) |
| 397 |
|
| 398 |
// Comments should be preserved in raw |
| 399 |
rawContent := cfg.raw.String() |
| 400 |
assert.Contains(t, rawContent, "Test User") |
| 401 |
}) |
| 402 |
|
| 403 |
t.Run("parse empty reader", func(t *testing.T) { |
| 404 |
t.Parallel() |
| 405 |
|
| 406 |
cfg := ParseConfig(bytes.NewReader([]byte(""))) |
| 407 |
if cfg == nil { |
| 408 |
t.Skip("ParseConfig returns nil for empty input") |
| 409 |
} |
| 410 |
// Note: ParseConfig may not mark an empty input as IsEmpty |
| 411 |
// because it initializes the raw buffer |
| 412 |
assert.NotNil(t, cfg) |
| 413 |
}) |
| 414 |
} |
| 415 |
|
| 416 |
// TestLoadConfigWithWorkdir tests loading config with workdir context. |
| 417 |
func TestLoadConfigWithWorkdir(t *testing.T) { |
| 418 |
t.Parallel() |
| 419 |
|
| 420 |
td := t.TempDir() |
| 421 |
gitDir := filepath.Join(td, ".git") |
| 422 |
err := os.MkdirAll(gitDir, 0o755) |
| 423 |
require.NoError(t, err) |
| 424 |
|
| 425 |
configPath := filepath.Join(gitDir, "config") |
| 426 |
content := "[user]\n\tname = Test" |
| 427 |
err = os.WriteFile(configPath, []byte(content), 0o644) |
| 428 |
require.NoError(t, err) |
| 429 |
|
| 430 |
// LoadConfigWithWorkdir can resolve includes relative to workdir |
| 431 |
cfg, err := LoadConfigWithWorkdir(configPath, td) |
| 432 |
require.NoError(t, err) |
| 433 |
require.NotNil(t, cfg) |
| 434 |
|
| 435 |
name, ok := cfg.Get("user.name") |
| 436 |
assert.True(t, ok) |
| 437 |
assert.Equal(t, "Test", name) |
| 438 |
} |
| 439 |
|
| 440 |
// TestConfigWithNoWrites tests noWrites flag. |
| 441 |
func TestConfigWithNoWrites(t *testing.T) { |
| 442 |
t.Parallel() |
| 443 |
|
| 444 |
td := t.TempDir() |
| 445 |
configPath := filepath.Join(td, "config") |
| 446 |
|
| 447 |
content := "[user]\n\tname = Original" |
| 448 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 449 |
require.NoError(t, err) |
| 450 |
|
| 451 |
cfg, err := LoadConfig(configPath) |
| 452 |
require.NoError(t, err) |
| 453 |
|
| 454 |
// Set noWrites flag |
| 455 |
cfg.noWrites = true |
| 456 |
|
| 457 |
// Try to set and flush |
| 458 |
_ = cfg.Set("user.name", "Modified") |
| 459 |
_ = cfg.flushRaw() |
| 460 |
|
| 461 |
// With noWrites, flushRaw should silently skip the write |
| 462 |
// File should still have original content |
| 463 |
fileContent, err := os.ReadFile(configPath) |
| 464 |
require.NoError(t, err) |
| 465 |
assert.Contains(t, string(fileContent), "Original") |
| 466 |
} |