Dont write to ~/.gitconfig in the tests

Just in case the tests are run without setting HOME
This commit is contained in:
Kovid Goyal
2025-11-13 18:50:42 +05:30
parent 83f0d6bc1a
commit 9bc29a7fa6
2 changed files with 20 additions and 13 deletions

View File

@@ -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

View File

@@ -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)
}
}