Skip to content

Commit

Permalink
gitindex: include environ for git tests (#760)
Browse files Browse the repository at this point in the history
gitindex: include environ for git tests

A recent change introduced GIT_CONFIG_GLOBAL and GIT_CONFIG_SYSTEM, but
ran into the footgun of unsetting the rest of the environ when cmd.Env
is non-empty. This for example broke tests for me since we didn't have
my custom $PATH set.

Instead we revert the change and make a much smaller change to just
always set the relevant environment variables.

This reverts commit ab1b8f0.

Test Plan: go test
  • Loading branch information
keegancsmith authored Apr 21, 2024
1 parent a0f051e commit 8619902
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions gitindex/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,13 @@ func TestIndexDeltaBasic(t *testing.T) {
repositoryDir := t.TempDir()

// setup: initialize the repository and all of its branches
runGitScript := func(t *testing.T, dir, script string) {
runScript(t, dir, script, "GIT_CONFIG_GLOBAL=", "GIT_CONFIG_SYSTEM=")
}
runGitScript(t, repositoryDir, "git init -b master")
runGitScript(t, repositoryDir, fmt.Sprintf("git config user.email %q", "[email protected]"))
runGitScript(t, repositoryDir, fmt.Sprintf("git config user.name %q", "Your Name"))
runScript(t, repositoryDir, "git init -b master")
runScript(t, repositoryDir, fmt.Sprintf("git config user.email %q", "[email protected]"))
runScript(t, repositoryDir, fmt.Sprintf("git config user.name %q", "Your Name"))

for _, b := range test.branches {
runGitScript(t, repositoryDir, fmt.Sprintf("git checkout -b %q", b))
runGitScript(t, repositoryDir, fmt.Sprintf("git commit --allow-empty -m %q", "empty commit"))
runScript(t, repositoryDir, fmt.Sprintf("git checkout -b %q", b))
runScript(t, repositoryDir, fmt.Sprintf("git commit --allow-empty -m %q", "empty commit"))
}

for _, step := range test.steps {
Expand All @@ -545,7 +542,7 @@ func TestIndexDeltaBasic(t *testing.T) {

hadChange := false

runGitScript(t, repositoryDir, fmt.Sprintf("git checkout %q", b))
runScript(t, repositoryDir, fmt.Sprintf("git checkout %q", b))

for _, d := range step.deletedDocuments[b] {
hadChange = true
Expand All @@ -557,7 +554,7 @@ func TestIndexDeltaBasic(t *testing.T) {
t.Fatalf("deleting file %q: %s", d.Name, err)
}

runGitScript(t, repositoryDir, fmt.Sprintf("git add %q", file))
runScript(t, repositoryDir, fmt.Sprintf("git add %q", file))
}

for _, d := range step.addedDocuments[b] {
Expand All @@ -575,14 +572,14 @@ func TestIndexDeltaBasic(t *testing.T) {
t.Fatalf("writing file %q: %s", d.Name, err)
}

runGitScript(t, repositoryDir, fmt.Sprintf("git add %q", file))
runScript(t, repositoryDir, fmt.Sprintf("git add %q", file))
}

if !hadChange {
continue
}

runGitScript(t, repositoryDir, fmt.Sprintf("git commit -m %q", step.name))
runScript(t, repositoryDir, fmt.Sprintf("git commit -m %q", step.name))
}

// setup: prepare indexOptions with given overrides
Expand Down Expand Up @@ -755,15 +752,17 @@ func TestRepoPathRanks(t *testing.T) {
}
}

func runScript(t *testing.T, cwd string, script string, env ...string) {
func runScript(t *testing.T, cwd string, script string) {
t.Helper()

err := os.MkdirAll(cwd, 0o755)
if err != nil {
t.Fatalf("ensuring path %q exists: %s", cwd, err)
}

cmd := exec.Command("sh", "-euxc", script)
cmd.Dir = cwd
cmd.Env = env
cmd.Env = append([]string{"GIT_CONFIG_GLOBAL=", "GIT_CONFIG_SYSTEM="}, os.Environ()...)

if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("execution error: %v, output %s", err, out)
Expand Down

0 comments on commit 8619902

Please sign in to comment.