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:
@@ -1188,8 +1188,8 @@ func TestInitBranchPersistsToConfigYaml(t *testing.T) {
|
|||||||
// The bug: sync-branch remains commented as "# sync-branch:" instead of "sync-branch:"
|
// The bug: sync-branch remains commented as "# sync-branch:" instead of "sync-branch:"
|
||||||
// This test should FAIL on the current codebase to prove the bug exists
|
// This test should FAIL on the current codebase to prove the bug exists
|
||||||
if strings.Contains(configStr, "# sync-branch:") && !strings.Contains(configStr, "\nsync-branch:") {
|
if strings.Contains(configStr, "# sync-branch:") && !strings.Contains(configStr, "\nsync-branch:") {
|
||||||
t.Errorf("BUG: --branch flag did not persist to config.yaml\n"+
|
t.Errorf("BUG: --branch flag did not persist to config.yaml\n" +
|
||||||
"Expected uncommented 'sync-branch: \"beads-sync\"'\n"+
|
"Expected uncommented 'sync-branch: \"beads-sync\"'\n" +
|
||||||
"Got commented '# sync-branch:' (only set in database, not config.yaml)")
|
"Got commented '# sync-branch:' (only set in database, not config.yaml)")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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")
|
||||||
|
|||||||
Reference in New Issue
Block a user