this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/vcs: avoid using os.Environ in a test helper

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í <mvdan@mvdan.cc>
Change-Id: I9e8f87bbe60fda9b3d37817a4c24c8ace24a9018
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201442
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+18 -24
+5 -4
internal/ci/checks/commit_test.go
··· 38 38 t.Logf("commit:\n%s", commit) 39 39 40 40 dir := t.TempDir() 41 - vcs.InitTestEnv(t) 42 - mustRunCmd(t, dir, "git", "init") 43 - mustRunCmd(t, dir, "git", 41 + env := vcs.TestEnv() 42 + mustRunCmd(t, dir, env, "git", "init") 43 + mustRunCmd(t, dir, env, "git", 44 44 "-c", "user.email=cueckoo@gmail.com", 45 45 "-c", "user.name=cueckoo", 46 46 "commit", "--allow-empty", "-m", string(commit), ··· 71 71 } 72 72 } 73 73 74 - func mustRunCmd(t *testing.T, dir string, exe string, args ...string) { 74 + func mustRunCmd(t *testing.T, dir string, env []string, exe string, args ...string) { 75 75 cmd := exec.Command(exe, args...) 76 76 cmd.Dir = dir 77 + cmd.Env = env 77 78 data, err := cmd.CombinedOutput() 78 79 qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data)) 79 80 }
+7 -15
internal/vcs/vcs.go
··· 24 24 "os/exec" 25 25 "path/filepath" 26 26 "runtime" 27 - "strings" 28 - "testing" 29 27 "time" 30 28 ) 31 29 ··· 148 146 } 149 147 } 150 148 151 - // InitTestEnv sets up the environment so that any executed VCS command 149 + // TestEnv builds an environment so that any executed VCS command with it 152 150 // won't be affected by the outer level environment. 153 151 // 154 152 // Note that this function is exposed so we can reuse it from other test packages 155 153 // which also need to use Go tests with VCS systems. 156 154 // Exposing a test helper is fine for now, given this is an internal package. 157 - func InitTestEnv(t testing.TB) { 158 - t.Helper() 159 - path := os.Getenv("PATH") 160 - systemRoot := os.Getenv("SYSTEMROOT") 161 - // First unset all environment variables to make a pristine environment. 162 - for _, kv := range os.Environ() { 163 - key, _, _ := strings.Cut(kv, "=") 164 - t.Setenv(key, "") 165 - os.Unsetenv(key) 155 + func TestEnv() []string { 156 + env := []string{ 157 + "PATH=" + os.Getenv("PATH"), 158 + homeEnvName() + "=/no-home", 166 159 } 167 - os.Setenv("PATH", path) 168 - os.Setenv(homeEnvName(), "/no-home") 169 160 // Must preserve SYSTEMROOT on Windows: https://github.com/golang/go/issues/25513 et al 170 161 if runtime.GOOS == "windows" { 171 - os.Setenv("SYSTEMROOT", systemRoot) 162 + env = append(env, "SYSTEMROOT="+os.Getenv("SYSTEMROOT")) 172 163 } 164 + return env 173 165 }
+6 -5
internal/vcs/vcs_test.go
··· 51 51 _, err = New("git", subdir) 52 52 qt.Assert(t, qt.ErrorMatches(err, `git VCS not found in any parent of ".+"`)) 53 53 54 - InitTestEnv(t) 55 - mustRunCmd(t, dir, "git", "init") 54 + env := TestEnv() 55 + mustRunCmd(t, dir, env, "git", "init") 56 56 v, err := New("git", subdir) 57 57 qt.Assert(t, qt.IsNil(err)) 58 58 ··· 63 63 qt.Assert(t, qt.IsNil(err)) 64 64 qt.Assert(t, qt.IsTrue(statusuncommitted.Uncommitted)) 65 65 66 - mustRunCmd(t, dir, "git", "add", ".") 66 + mustRunCmd(t, dir, env, "git", "add", ".") 67 67 statusuncommitted, err = v.Status(ctx, subdir) 68 68 qt.Assert(t, qt.IsNil(err)) 69 69 qt.Assert(t, qt.IsTrue(statusuncommitted.Uncommitted)) 70 70 71 71 commitTime := time.Now().Truncate(time.Second) 72 - mustRunCmd(t, dir, "git", 72 + mustRunCmd(t, dir, env, "git", 73 73 "-c", "user.email=cueckoo@gmail.com", 74 74 "-c", "user.name=cueckoo", 75 75 "commit", "-m", "something", ··· 186 186 qt.Assert(t, qt.IsTrue(statusmissing.Uncommitted)) 187 187 } 188 188 189 - func mustRunCmd(t *testing.T, dir string, exe string, args ...string) { 189 + func mustRunCmd(t *testing.T, dir string, env []string, exe string, args ...string) { 190 190 c := exec.Command(exe, args...) 191 191 c.Dir = dir 192 + c.Env = env 192 193 data, err := c.CombinedOutput() 193 194 qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data)) 194 195 }