Skip to content

Commit 3b20437

Browse files
committed
TestRepo: set up a clean git environment when running git commands
1 parent b30ab78 commit 3b20437

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

git_sizer_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ func TestRefSelections(t *testing.T) {
126126

127127
// Create a test repo with one orphan commit per refname:
128128
repo := testutils.NewTestRepo(t, true, "ref-selection")
129-
130129
defer repo.Remove(t)
131130

132131
for _, p := range references {
@@ -241,7 +240,6 @@ func TestRefSelections(t *testing.T) {
241240
p.name,
242241
func(t *testing.T) {
243242
repo := repo.Clone(t, "ref-selection")
244-
245243
defer repo.Remove(t)
246244

247245
for _, e := range p.config {

internal/testutils/repoutils.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/exec"
1010
"path/filepath"
11+
"strings"
1112
"testing"
1213
"time"
1314

@@ -51,6 +52,7 @@ func (repo *TestRepo) Init(t *testing.T, bare bool) {
5152
} else {
5253
cmd = exec.Command("git", "init", repo.Path)
5354
}
55+
cmd.Env = CleanGitEnv()
5456
err := cmd.Run()
5557
require.NoError(t, err)
5658
}
@@ -90,14 +92,59 @@ func (repo *TestRepo) Repository(t *testing.T) *git.Repository {
9092
return r
9193
}
9294

95+
// localEnvVars is a list of the variable names that should be cleared
96+
// to give Git a clean environment.
97+
var localEnvVars = func() map[string]bool {
98+
m := map[string]bool{
99+
"HOME": true,
100+
"XDG_CONFIG_HOME": true,
101+
}
102+
out, err := exec.Command("git", "rev-parse", "--local-env-vars").Output()
103+
if err != nil {
104+
return m
105+
}
106+
for _, k := range strings.Fields(string(out)) {
107+
m[k] = true
108+
}
109+
return m
110+
}()
111+
112+
// GitEnv returns an appropriate environment for running `git`
113+
// commands without being confused by any existing environment or
114+
// gitconfig.
115+
func CleanGitEnv() []string {
116+
var env []string
117+
for _, e := range os.Environ() {
118+
i := strings.IndexByte(e, '=')
119+
if i == -1 {
120+
// This shouldn't happen, but if it does,
121+
// ignore it.
122+
continue
123+
}
124+
k := e[:i]
125+
if localEnvVars[k] {
126+
continue
127+
}
128+
env = append(env, e)
129+
}
130+
return append(
131+
env,
132+
fmt.Sprintf("HOME=%s", os.DevNull),
133+
fmt.Sprintf("XDG_CONFIG_HOME=%s", os.DevNull),
134+
"GIT_CONFIG_NOSYSTEM=1",
135+
)
136+
}
137+
93138
// GitCommand creates an `*exec.Cmd` for running `git` in `repo` with
94139
// the specified arguments.
95140
func (repo *TestRepo) GitCommand(t *testing.T, args ...string) *exec.Cmd {
96141
t.Helper()
97142

98143
gitArgs := []string{"-C", repo.Path}
99144
gitArgs = append(gitArgs, args...)
100-
return exec.Command("git", gitArgs...)
145+
cmd := exec.Command("git", gitArgs...)
146+
cmd.Env = CleanGitEnv()
147+
return cmd
101148
}
102149

103150
func (repo *TestRepo) UpdateRef(t *testing.T, refname string, oid git.OID) {

0 commit comments

Comments
 (0)