fix: isolate TestSetupGlobalGitIgnore_ReadOnly from user's git config (#1045)

The test was failing because it called 'git config --global core.excludesfile'
which returned the real user's gitignore path instead of using the test's
temp directory.

Fix: Set GIT_CONFIG_GLOBAL env var to an empty temp config file, ensuring
the test uses the temp directory's .config/git/ignore path as intended.

Also extracted the isolation logic into a reusable setupIsolatedGitConfig helper.

Co-authored-by: Ismar Iljazovic <ismar@gmail.com>
This commit is contained in:
Ismar
2026-01-13 01:42:30 +01:00
committed by GitHub
parent 75f03b782f
commit dc28efe9d1

View File

@@ -1257,11 +1257,24 @@ func TestInitReinitWithBranch(t *testing.T) {
} }
} }
// setupIsolatedGitConfig creates an empty git config in tmpDir and sets GIT_CONFIG_GLOBAL
// to prevent tests from using the real user's global git config.
func setupIsolatedGitConfig(t *testing.T, tmpDir string) {
t.Helper()
gitConfigPath := filepath.Join(tmpDir, ".gitconfig")
if err := os.WriteFile(gitConfigPath, []byte(""), 0644); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_CONFIG_GLOBAL", gitConfigPath)
}
// TestSetupGlobalGitIgnore_ReadOnly verifies graceful handling when the // TestSetupGlobalGitIgnore_ReadOnly verifies graceful handling when the
// gitignore file cannot be written (prints manual instructions instead of failing). // gitignore file cannot be written (prints manual instructions instead of failing).
func TestSetupGlobalGitIgnore_ReadOnly(t *testing.T) { func TestSetupGlobalGitIgnore_ReadOnly(t *testing.T) {
t.Run("read-only file", func(t *testing.T) { t.Run("read-only file", func(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
setupIsolatedGitConfig(t, tmpDir)
configDir := filepath.Join(tmpDir, ".config", "git") configDir := filepath.Join(tmpDir, ".config", "git")
if err := os.MkdirAll(configDir, 0755); err != nil { if err := os.MkdirAll(configDir, 0755); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -1290,6 +1303,7 @@ func TestSetupGlobalGitIgnore_ReadOnly(t *testing.T) {
t.Run("symlink to read-only file", func(t *testing.T) { t.Run("symlink to read-only file", func(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
setupIsolatedGitConfig(t, tmpDir)
// Target file in a separate location // Target file in a separate location
targetDir := filepath.Join(tmpDir, "target") targetDir := filepath.Join(tmpDir, "target")