| 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 |
// TestPlatformDefaultPaths tests that default config paths are platform-appropriate. |
| 14 |
func TestPlatformDefaultPaths(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
|
| 17 |
cfg := New() |
| 18 |
|
| 19 |
// System config should be set |
| 20 |
assert.NotEmpty(t, cfg.SystemConfig, "system config path should be set") |
| 21 |
|
| 22 |
// Local and worktree configs should be set |
| 23 |
assert.NotEmpty(t, cfg.LocalConfig, "local config should be set") |
| 24 |
assert.NotEmpty(t, cfg.WorktreeConfig, "worktree config should be set") |
| 25 |
|
| 26 |
// Platform-specific checks |
| 27 |
switch runtime.GOOS { |
| 28 |
case "windows": |
| 29 |
// Windows paths might contain backslashes or use ProgramData |
| 30 |
assert.Contains(t, []bool{ |
| 31 |
filepath.IsAbs(cfg.SystemConfig), |
| 32 |
len(cfg.SystemConfig) > 0, |
| 33 |
}, true, "Windows system config should be absolute or set") |
| 34 |
default: |
| 35 |
// Unix-like systems typically use /etc/gitconfig |
| 36 |
if cfg.SystemConfig != "" { |
| 37 |
assert.True(t, filepath.IsAbs(cfg.SystemConfig), "Unix system config should be absolute if set") |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// TestPlatformPathSeparators tests that path handling works correctly on the platform. |
| 43 |
func TestPlatformPathSeparators(t *testing.T) { |
| 44 |
t.Parallel() |
| 45 |
|
| 46 |
td := t.TempDir() |
| 47 |
subdir := filepath.Join(td, "configs") |
| 48 |
require.NoError(t, os.MkdirAll(subdir, 0o755)) |
| 49 |
|
| 50 |
configPath := filepath.Join(td, "config") |
| 51 |
includePath := filepath.Join(subdir, "included.conf") |
| 52 |
|
| 53 |
// Create included config |
| 54 |
err := os.WriteFile(includePath, []byte("[core]\n\teditor = vim"), 0o644) |
| 55 |
require.NoError(t, err) |
| 56 |
|
| 57 |
// Main config with platform-appropriate path |
| 58 |
content := "[include]\n\tpath = " + filepath.ToSlash(includePath) + "\n[user]\n\tname = Test" |
| 59 |
err = os.WriteFile(configPath, []byte(content), 0o644) |
| 60 |
require.NoError(t, err) |
| 61 |
|
| 62 |
cfg, err := LoadConfig(configPath) |
| 63 |
require.NoError(t, err) |
| 64 |
require.NotNil(t, cfg) |
| 65 |
|
| 66 |
// Verify included value is accessible regardless of platform path separators |
| 67 |
editor, ok := cfg.Get("core.editor") |
| 68 |
if ok { |
| 69 |
assert.Equal(t, "vim", editor) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// TestPlatformLineEndings tests handling of platform-specific line endings. |
| 74 |
func TestPlatformLineEndings(t *testing.T) { |
| 75 |
t.Parallel() |
| 76 |
|
| 77 |
testCases := []struct { |
| 78 |
name string |
| 79 |
content string |
| 80 |
lineEnding string |
| 81 |
skip bool |
| 82 |
}{ |
| 83 |
{ |
| 84 |
name: "Unix LF", |
| 85 |
lineEnding: "\n", |
| 86 |
skip: false, |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "Windows CRLF", |
| 90 |
lineEnding: "\r\n", |
| 91 |
skip: false, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "Old Mac CR", |
| 95 |
lineEnding: "\r", |
| 96 |
skip: true, // Git doesn't support CR-only line endings |
| 97 |
}, |
| 98 |
} |
| 99 |
|
| 100 |
for _, tc := range testCases { |
| 101 |
t.Run(tc.name, func(t *testing.T) { |
| 102 |
t.Parallel() |
| 103 |
|
| 104 |
if tc.skip { |
| 105 |
t.Skip("Skipping unsupported line ending format") |
| 106 |
} |
| 107 |
|
| 108 |
td := t.TempDir() |
| 109 |
configPath := filepath.Join(td, "config") |
| 110 |
|
| 111 |
// Build config with specific line ending |
| 112 |
lines := []string{ |
| 113 |
"[user]", |
| 114 |
"\tname = John Doe", |
| 115 |
"\temail = [email protected]", |
| 116 |
"[core]", |
| 117 |
"\teditor = vim", |
| 118 |
} |
| 119 |
content := "" |
| 120 |
for _, line := range lines { |
| 121 |
content += line + tc.lineEnding |
| 122 |
} |
| 123 |
|
| 124 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 125 |
require.NoError(t, err) |
| 126 |
|
| 127 |
cfg, err := LoadConfig(configPath) |
| 128 |
require.NoError(t, err) |
| 129 |
require.NotNil(t, cfg) |
| 130 |
|
| 131 |
// Values should be accessible regardless of line ending |
| 132 |
name, ok := cfg.Get("user.name") |
| 133 |
assert.True(t, ok) |
| 134 |
assert.Equal(t, "John Doe", name) |
| 135 |
|
| 136 |
email, ok := cfg.Get("user.email") |
| 137 |
assert.True(t, ok) |
| 138 |
assert.Equal(t, "[email protected]", email) |
| 139 |
|
| 140 |
editor, ok := cfg.Get("core.editor") |
| 141 |
assert.True(t, ok) |
| 142 |
assert.Equal(t, "vim", editor) |
| 143 |
}) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// TestPlatformFilePermissions tests file permission handling (Unix-specific behavior). |
| 148 |
func TestPlatformFilePermissions(t *testing.T) { |
| 149 |
if runtime.GOOS == "windows" { |
| 150 |
t.Skip("File permission test not applicable on Windows") |
| 151 |
} |
| 152 |
|
| 153 |
t.Parallel() |
| 154 |
|
| 155 |
td := t.TempDir() |
| 156 |
configPath := filepath.Join(td, "config") |
| 157 |
|
| 158 |
// Create config with specific permissions |
| 159 |
content := "[user]\n\tname = Test User" |
| 160 |
err := os.WriteFile(configPath, []byte(content), 0o600) |
| 161 |
require.NoError(t, err) |
| 162 |
|
| 163 |
cfg, err := LoadConfig(configPath) |
| 164 |
require.NoError(t, err) |
| 165 |
require.NotNil(t, cfg) |
| 166 |
|
| 167 |
name, ok := cfg.Get("user.name") |
| 168 |
assert.True(t, ok) |
| 169 |
assert.Equal(t, "Test User", name) |
| 170 |
|
| 171 |
// Verify file permissions are preserved after write (Set automatically writes) |
| 172 |
err = cfg.Set("user.email", "[email protected]") |
| 173 |
require.NoError(t, err) |
| 174 |
|
| 175 |
info, err := os.Stat(configPath) |
| 176 |
require.NoError(t, err) |
| 177 |
|
| 178 |
// Permission preservation may vary by implementation |
| 179 |
// Just verify file is still readable |
| 180 |
assert.NotNil(t, info) |
| 181 |
} |
| 182 |
|
| 183 |
// TestPlatformCaseSensitivity tests path handling based on filesystem case sensitivity. |
| 184 |
func TestPlatformCaseSensitivity(t *testing.T) { |
| 185 |
t.Parallel() |
| 186 |
|
| 187 |
td := t.TempDir() |
| 188 |
|
| 189 |
// Try to create files with different cases |
| 190 |
configLower := filepath.Join(td, "config") |
| 191 |
configUpper := filepath.Join(td, "CONFIG") |
| 192 |
|
| 193 |
err := os.WriteFile(configLower, []byte("[user]\n\tname = lowercase"), 0o644) |
| 194 |
require.NoError(t, err) |
| 195 |
|
| 196 |
// On case-sensitive filesystems, this creates a different file |
| 197 |
// On case-insensitive filesystems (Windows, macOS default), this overwrites |
| 198 |
err = os.WriteFile(configUpper, []byte("[user]\n\tname = uppercase"), 0o644) |
| 199 |
require.NoError(t, err) |
| 200 |
|
| 201 |
// Load the lowercase version |
| 202 |
cfg, err := LoadConfig(configLower) |
| 203 |
require.NoError(t, err) |
| 204 |
require.NotNil(t, cfg) |
| 205 |
|
| 206 |
name, ok := cfg.Get("user.name") |
| 207 |
assert.True(t, ok) |
| 208 |
|
| 209 |
// On case-insensitive FS, we get "uppercase" |
| 210 |
// On case-sensitive FS, we get "lowercase" |
| 211 |
// Both are valid platform behaviors |
| 212 |
assert.Contains(t, []string{"lowercase", "uppercase"}, name) |
| 213 |
} |
| 214 |
|
| 215 |
// TestPlatformUserHomeExpansion tests tilde expansion for home directory. |
| 216 |
func TestPlatformUserHomeExpansion(t *testing.T) { |
| 217 |
t.Parallel() |
| 218 |
|
| 219 |
td := t.TempDir() |
| 220 |
configPath := filepath.Join(td, "config") |
| 221 |
|
| 222 |
// Create a config that would normally use ~/ (though we test the loading, not path expansion) |
| 223 |
content := "[user]\n\tname = Test" |
| 224 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 225 |
require.NoError(t, err) |
| 226 |
|
| 227 |
cfg, err := LoadConfig(configPath) |
| 228 |
require.NoError(t, err) |
| 229 |
require.NotNil(t, cfg) |
| 230 |
|
| 231 |
// Verify basic loading works (actual ~ expansion tested elsewhere) |
| 232 |
name, ok := cfg.Get("user.name") |
| 233 |
assert.True(t, ok) |
| 234 |
assert.Equal(t, "Test", name) |
| 235 |
} |
| 236 |
|
| 237 |
// TestPlatformSymlinks tests symlink handling (Unix-specific). |
| 238 |
func TestPlatformSymlinks(t *testing.T) { |
| 239 |
if runtime.GOOS == "windows" { |
| 240 |
t.Skip("Symlink test not reliable on Windows without privileges") |
| 241 |
} |
| 242 |
|
| 243 |
t.Parallel() |
| 244 |
|
| 245 |
td := t.TempDir() |
| 246 |
realConfig := filepath.Join(td, "real-config") |
| 247 |
symlinkConfig := filepath.Join(td, "symlink-config") |
| 248 |
|
| 249 |
// Create real config |
| 250 |
content := "[user]\n\tname = Via Symlink" |
| 251 |
err := os.WriteFile(realConfig, []byte(content), 0o644) |
| 252 |
require.NoError(t, err) |
| 253 |
|
| 254 |
// Create symlink |
| 255 |
err = os.Symlink(realConfig, symlinkConfig) |
| 256 |
if err != nil { |
| 257 |
t.Skipf("Cannot create symlink: %v", err) |
| 258 |
} |
| 259 |
|
| 260 |
// Load via symlink |
| 261 |
cfg, err := LoadConfig(symlinkConfig) |
| 262 |
require.NoError(t, err) |
| 263 |
require.NotNil(t, cfg) |
| 264 |
|
| 265 |
name, ok := cfg.Get("user.name") |
| 266 |
assert.True(t, ok) |
| 267 |
assert.Equal(t, "Via Symlink", name) |
| 268 |
|
| 269 |
// Write via symlink should work (Set automatically writes) |
| 270 |
err = cfg.Set("user.email", "[email protected]") |
| 271 |
require.NoError(t, err) |
| 272 |
|
| 273 |
// Verify write went through |
| 274 |
cfg2, err := LoadConfig(realConfig) |
| 275 |
require.NoError(t, err) |
| 276 |
|
| 277 |
email, ok := cfg2.Get("user.email") |
| 278 |
assert.True(t, ok) |
| 279 |
assert.Equal(t, "[email protected]", email) |
| 280 |
} |
| 281 |
|
| 282 |
// TestPlatformLongPaths tests handling of long file paths. |
| 283 |
func TestPlatformLongPaths(t *testing.T) { |
| 284 |
t.Parallel() |
| 285 |
|
| 286 |
// Create a deep directory structure |
| 287 |
td := t.TempDir() |
| 288 |
deepPath := td |
| 289 |
|
| 290 |
// Create a reasonably deep path (not MAX_PATH to avoid platform issues) |
| 291 |
for range 10 { |
| 292 |
deepPath = filepath.Join(deepPath, "verylongdirectorynametotest") |
| 293 |
} |
| 294 |
|
| 295 |
err := os.MkdirAll(deepPath, 0o755) |
| 296 |
if err != nil { |
| 297 |
t.Skipf("Cannot create deep path on this platform: %v", err) |
| 298 |
} |
| 299 |
|
| 300 |
configPath := filepath.Join(deepPath, "config") |
| 301 |
content := "[user]\n\tname = Deep Path Test" |
| 302 |
err = os.WriteFile(configPath, []byte(content), 0o644) |
| 303 |
if err != nil { |
| 304 |
t.Skipf("Cannot write to deep path on this platform: %v", err) |
| 305 |
} |
| 306 |
|
| 307 |
cfg, err := LoadConfig(configPath) |
| 308 |
require.NoError(t, err) |
| 309 |
require.NotNil(t, cfg) |
| 310 |
|
| 311 |
name, ok := cfg.Get("user.name") |
| 312 |
assert.True(t, ok) |
| 313 |
assert.Equal(t, "Deep Path Test", name) |
| 314 |
} |
| 315 |
|
| 316 |
// TestPlatformRelativePaths tests relative path handling. |
| 317 |
func TestPlatformRelativePaths(t *testing.T) { |
| 318 |
td := t.TempDir() |
| 319 |
configPath := filepath.Join(td, "config") |
| 320 |
|
| 321 |
content := "[user]\n\tname = Relative Path Test" |
| 322 |
err := os.WriteFile(configPath, []byte(content), 0o644) |
| 323 |
require.NoError(t, err) |
| 324 |
|
| 325 |
// Change to temp directory for relative path resolution |
| 326 |
t.Chdir(td) |
| 327 |
|
| 328 |
// Load with relative path |
| 329 |
cfg, err := LoadConfig("config") |
| 330 |
require.NoError(t, err) |
| 331 |
require.NotNil(t, cfg) |
| 332 |
|
| 333 |
name, ok := cfg.Get("user.name") |
| 334 |
assert.True(t, ok) |
| 335 |
assert.Equal(t, "Relative Path Test", name) |
| 336 |
} |
| 337 |
|
| 338 |
// TestPlatformEnvironmentVariables tests environment variable handling across platforms. |
| 339 |
func TestPlatformEnvironmentVariables(t *testing.T) { |
| 340 |
// Set a test environment variable |
| 341 |
testKey := "GITCONFIG_TEST_COUNT" |
| 342 |
testKeyVar := "GITCONFIG_TEST_KEY_0" |
| 343 |
testValueVar := "GITCONFIG_TEST_VALUE_0" |
| 344 |
|
| 345 |
t.Setenv(testKey, "1") |
| 346 |
t.Setenv(testKeyVar, "user.name") |
| 347 |
t.Setenv(testValueVar, "From Env") |
| 348 |
|
| 349 |
cfg := LoadConfigFromEnv("GITCONFIG_TEST") |
| 350 |
require.NotNil(t, cfg) |
| 351 |
|
| 352 |
name, ok := cfg.Get("user.name") |
| 353 |
assert.True(t, ok) |
| 354 |
assert.Equal(t, "From Env", name) |
| 355 |
} |