diff --git a/internal/ci/checks/commit_test.go b/internal/ci/checks/commit_test.go index ac09d8cbf29..27781bd4e85 100644 --- a/internal/ci/checks/commit_test.go +++ b/internal/ci/checks/commit_test.go @@ -38,9 +38,9 @@ func TestCommits(t *testing.T) { t.Logf("commit:\n%s", commit) dir := t.TempDir() - vcs.InitTestEnv(t) - mustRunCmd(t, dir, "git", "init") - mustRunCmd(t, dir, "git", + env := vcs.TestEnv() + mustRunCmd(t, dir, env, "git", "init") + mustRunCmd(t, dir, env, "git", "-c", "user.email=cueckoo@gmail.com", "-c", "user.name=cueckoo", "commit", "--allow-empty", "-m", string(commit), @@ -71,9 +71,10 @@ func TestCommits(t *testing.T) { } } -func mustRunCmd(t *testing.T, dir string, exe string, args ...string) { +func mustRunCmd(t *testing.T, dir string, env []string, exe string, args ...string) { cmd := exec.Command(exe, args...) cmd.Dir = dir + cmd.Env = env data, err := cmd.CombinedOutput() qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data)) } diff --git a/internal/vcs/vcs.go b/internal/vcs/vcs.go index 95470f6d091..00ae8baee1b 100644 --- a/internal/vcs/vcs.go +++ b/internal/vcs/vcs.go @@ -24,8 +24,6 @@ import ( "os/exec" "path/filepath" "runtime" - "strings" - "testing" "time" ) @@ -148,26 +146,20 @@ func homeEnvName() string { } } -// InitTestEnv sets up the environment so that any executed VCS command +// TestEnv builds an environment so that any executed VCS command with it // won't be affected by the outer level environment. // // Note that this function is exposed so we can reuse it from other test packages // which also need to use Go tests with VCS systems. // Exposing a test helper is fine for now, given this is an internal package. -func InitTestEnv(t testing.TB) { - t.Helper() - path := os.Getenv("PATH") - systemRoot := os.Getenv("SYSTEMROOT") - // First unset all environment variables to make a pristine environment. - for _, kv := range os.Environ() { - key, _, _ := strings.Cut(kv, "=") - t.Setenv(key, "") - os.Unsetenv(key) +func TestEnv() []string { + env := []string{ + "PATH=" + os.Getenv("PATH"), + homeEnvName() + "=/no-home", } - os.Setenv("PATH", path) - os.Setenv(homeEnvName(), "/no-home") // Must preserve SYSTEMROOT on Windows: https://github.com/golang/go/issues/25513 et al if runtime.GOOS == "windows" { - os.Setenv("SYSTEMROOT", systemRoot) + env = append(env, "SYSTEMROOT="+os.Getenv("SYSTEMROOT")) } + return env } diff --git a/internal/vcs/vcs_test.go b/internal/vcs/vcs_test.go index 947e9a111e6..5fa2ce42f0b 100644 --- a/internal/vcs/vcs_test.go +++ b/internal/vcs/vcs_test.go @@ -51,8 +51,8 @@ func TestGit(t *testing.T) { _, err = New("git", subdir) qt.Assert(t, qt.ErrorMatches(err, `git VCS not found in any parent of ".+"`)) - InitTestEnv(t) - mustRunCmd(t, dir, "git", "init") + env := TestEnv() + mustRunCmd(t, dir, env, "git", "init") v, err := New("git", subdir) qt.Assert(t, qt.IsNil(err)) @@ -63,13 +63,13 @@ func TestGit(t *testing.T) { qt.Assert(t, qt.IsNil(err)) qt.Assert(t, qt.IsTrue(statusuncommitted.Uncommitted)) - mustRunCmd(t, dir, "git", "add", ".") + mustRunCmd(t, dir, env, "git", "add", ".") statusuncommitted, err = v.Status(ctx, subdir) qt.Assert(t, qt.IsNil(err)) qt.Assert(t, qt.IsTrue(statusuncommitted.Uncommitted)) commitTime := time.Now().Truncate(time.Second) - mustRunCmd(t, dir, "git", + mustRunCmd(t, dir, env, "git", "-c", "user.email=cueckoo@gmail.com", "-c", "user.name=cueckoo", "commit", "-m", "something", @@ -186,9 +186,10 @@ func TestGit(t *testing.T) { qt.Assert(t, qt.IsTrue(statusmissing.Uncommitted)) } -func mustRunCmd(t *testing.T, dir string, exe string, args ...string) { +func mustRunCmd(t *testing.T, dir string, env []string, exe string, args ...string) { c := exec.Command(exe, args...) c.Dir = dir + c.Env = env data, err := c.CombinedOutput() qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data)) }