From 9bc29a7fa63250486c5f554e14be34ac300b93f8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 13 Nov 2025 18:50:42 +0530 Subject: [PATCH] Dont write to ~/.gitconfig in the tests Just in case the tests are run without setting HOME --- tools/ignorefiles/gitignore.go | 6 +++++- tools/ignorefiles/gitignore_test.go | 27 +++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/tools/ignorefiles/gitignore.go b/tools/ignorefiles/gitignore.go index 5377429b9..6b88b0ecd 100644 --- a/tools/ignorefiles/gitignore.go +++ b/tools/ignorefiles/gitignore.go @@ -261,11 +261,15 @@ func CompileGitIgnoreLine(line string) (ans GitPattern, skipped_line bool) { } func get_global_gitconfig_excludesfile() (ans string) { + return _get_global_gitconfig_excludesfile(utils.Expanduser("~/.gitconfig")) +} + +func _get_global_gitconfig_excludesfile(home_gitconfig string) (ans string) { cfhome := os.Getenv("XDG_CONFIG_HOME") if cfhome == "" { cfhome = utils.Expanduser("~/.config") } - for _, candidate := range []string{"/etc/gitconfig", filepath.Join(cfhome, "git", "config"), utils.Expanduser("~/.gitconfig")} { + for _, candidate := range []string{"/etc/gitconfig", filepath.Join(cfhome, "git", "config"), home_gitconfig} { if data, err := os.ReadFile(candidate); err == nil { s := utils.NewLineScanner(utils.UnsafeBytesToString(data)) in_core := false diff --git a/tools/ignorefiles/gitignore_test.go b/tools/ignorefiles/gitignore_test.go index 0b4dc1065..a8bf49e8d 100644 --- a/tools/ignorefiles/gitignore_test.go +++ b/tools/ignorefiles/gitignore_test.go @@ -4,6 +4,7 @@ import ( "fmt" "io/fs" "os" + "path/filepath" "strings" "testing" @@ -143,19 +144,21 @@ bar `: { } } } - os.WriteFile(utils.Expanduser("~/.gitconfig"), []byte(` - [core] - something - [else] - ... - [core] - ... - [core] - excludesfile = one - [core] - excludesfile = ~/global-gitignore + tdir := t.TempDir() + gc := filepath.Join(tdir, ".gitconfig") + os.WriteFile(gc, []byte(` +[core] +something +[else] +... +[core] +... +[core] +excludesfile = one +[core] +excludesfile = ~/global-gitignore `), 0600) - if ef := get_global_gitconfig_excludesfile(); ef != utils.Expanduser("~/global-gitignore") { + if ef := _get_global_gitconfig_excludesfile(gc); ef != utils.Expanduser("~/global-gitignore") { t.Fatalf("global gitconfig excludes file incorrect: %s != %s", utils.Expanduser("~/global-gitignore"), ef) } }