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
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
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) { 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") cfhome := os.Getenv("XDG_CONFIG_HOME")
if cfhome == "" { if cfhome == "" {
cfhome = utils.Expanduser("~/.config") 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 { if data, err := os.ReadFile(candidate); err == nil {
s := utils.NewLineScanner(utils.UnsafeBytesToString(data)) s := utils.NewLineScanner(utils.UnsafeBytesToString(data))
in_core := false in_core := false

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath"
"strings" "strings"
"testing" "testing"
@ -143,19 +144,21 @@ bar `: {
} }
} }
} }
os.WriteFile(utils.Expanduser("~/.gitconfig"), []byte(` tdir := t.TempDir()
[core] gc := filepath.Join(tdir, ".gitconfig")
something os.WriteFile(gc, []byte(`
[else] [core]
... something
[core] [else]
... ...
[core] [core]
excludesfile = one ...
[core] [core]
excludesfile = ~/global-gitignore excludesfile = one
[core]
excludesfile = ~/global-gitignore
`), 0600) `), 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) t.Fatalf("global gitconfig excludes file incorrect: %s != %s", utils.Expanduser("~/global-gitignore"), ef)
} }
} }