diff --git a/internal/ci/checks/commit_test.go b/internal/ci/checks/commit_test.go index 1f6c584f9e2..ac09d8cbf29 100644 --- a/internal/ci/checks/commit_test.go +++ b/internal/ci/checks/commit_test.go @@ -21,6 +21,8 @@ import ( "github.com/go-quicktest/qt" "golang.org/x/tools/txtar" + + "cuelang.org/go/internal/vcs" ) func TestCommits(t *testing.T) { @@ -36,6 +38,7 @@ 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", "-c", "user.email=cueckoo@gmail.com", diff --git a/internal/vcs/vcs.go b/internal/vcs/vcs.go index faf40d3e216..95470f6d091 100644 --- a/internal/vcs/vcs.go +++ b/internal/vcs/vcs.go @@ -23,6 +23,9 @@ import ( "os" "os/exec" "path/filepath" + "runtime" + "strings" + "testing" "time" ) @@ -133,3 +136,38 @@ type vcsNotFoundError struct { func (e *vcsNotFoundError) Error() string { return fmt.Sprintf("%s VCS not found in any parent of %q", e.kind, e.dir) } + +func homeEnvName() string { + switch runtime.GOOS { + case "windows": + return "USERPROFILE" + case "plan9": + return "home" + default: + return "HOME" + } +} + +// InitTestEnv sets up the environment so that any executed VCS command +// 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) + } + 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) + } +} diff --git a/internal/vcs/vcs_test.go b/internal/vcs/vcs_test.go index 32e6c970a9e..459366be708 100644 --- a/internal/vcs/vcs_test.go +++ b/internal/vcs/vcs_test.go @@ -19,8 +19,6 @@ import ( "os" "os/exec" "path/filepath" - "runtime" - "strings" "testing" "time" @@ -53,7 +51,7 @@ 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) + InitTestEnv(t) mustRunCmd(t, dir, "git", "init") v, err := New("git", subdir) qt.Assert(t, qt.IsNil(err)) @@ -187,26 +185,6 @@ func TestGit(t *testing.T) { qt.Assert(t, qt.IsTrue(statusmissing.Uncommitted)) } -// initTestEnv sets up the environment so that -// any executed VCS command won't be affected -// by the outer level environment. -func initTestEnv(t *testing.T) { - 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) - } - 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) - } -} - func mustRunCmd(t *testing.T, dir string, exe string, args ...string) { c := exec.Command(exe, args...) c.Dir = dir @@ -219,14 +197,3 @@ func skipIfNoExecutable(t *testing.T, exeName string) { t.Skipf("cannot find %q executable: %v", exeName, err) } } - -func homeEnvName() string { - switch runtime.GOOS { - case "windows": - return "USERPROFILE" - case "plan9": - return "home" - default: - return "HOME" - } -}