Skip to content

Commit

Permalink
internal/vcs: avoid using os.Environ in a test helper
Browse files Browse the repository at this point in the history
As reported by GODEBUG=gocachehash=1, this caused every single env var
set in the current environment to be part of the test cache key:

    $ grep testInput before | wc -l
    284
    $ grep testInput after | wc -l
    39

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I9e8f87bbe60fda9b3d37817a4c24c8ace24a9018
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201442
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mvdan committed Sep 18, 2024
1 parent bdd40af commit 7ec0e5b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
9 changes: 5 additions & 4 deletions internal/ci/checks/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]",
"-c", "user.name=cueckoo",
"commit", "--allow-empty", "-m", string(commit),
Expand Down Expand Up @@ -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))
}
22 changes: 7 additions & 15 deletions internal/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)

Expand Down Expand Up @@ -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
}
11 changes: 6 additions & 5 deletions internal/vcs/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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", "[email protected]",
"-c", "user.name=cueckoo",
"commit", "-m", "something",
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 7ec0e5b

Please sign in to comment.