this repo has no description
0
fork

Configure Feed

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

internal/vcs: use limited environment when executing git in tests

We don't want git environment variables like `$GIT_DIR`
to cause the test to fail. This change was prompted
by the fact that running `git rebase --exec` causes
the test to fail because it sets `$DIR_DIR` which
overrides the current directory, causing `git init`
to use the wrong directory to create the `.git` directory in.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ia17380a313a88fb525933c7ae6ef389f1042d67a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193380
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+35
+35
internal/vcs/vcs_test.go
··· 16 16 17 17 import ( 18 18 "context" 19 + "os" 19 20 "os/exec" 20 21 "path/filepath" 22 + "runtime" 23 + "strings" 21 24 "testing" 22 25 "time" 23 26 ··· 44 47 _, err = New("git", filepath.Join(dir, "subdir")) 45 48 qt.Assert(t, qt.ErrorMatches(err, `git VCS not found in any parent of ".+"`)) 46 49 50 + initTestEnv(t) 47 51 mustRunCmd(t, dir, "git", "init") 48 52 v, err := New("git", filepath.Join(dir, "subdir")) 49 53 qt.Assert(t, qt.IsNil(err)) ··· 81 85 })) 82 86 } 83 87 88 + // initTestEnv sets up the environment so that 89 + // any executed VCS command won't be affected 90 + // by the outer level environment. 91 + func initTestEnv(t *testing.T) { 92 + path := os.Getenv("PATH") 93 + systemRoot := os.Getenv("SYSTEMROOT") 94 + // First unset all environment variables to make a pristine environment. 95 + for _, kv := range os.Environ() { 96 + key, _, _ := strings.Cut(kv, "=") 97 + t.Setenv(key, "") 98 + os.Unsetenv(key) 99 + } 100 + os.Setenv("PATH", path) 101 + os.Setenv(homeEnvName(), "/no-home") 102 + // Must preserve SYSTEMROOT on Windows: https://github.com/golang/go/issues/25513 et al 103 + if runtime.GOOS == "windows" { 104 + os.Setenv("SYSTEMROOT", systemRoot) 105 + } 106 + } 107 + 84 108 func mustRunCmd(t *testing.T, dir string, exe string, args ...string) { 85 109 c := exec.Command(exe, args...) 86 110 c.Dir = dir ··· 93 117 t.Skipf("cannot find %q executable: %v", exeName, err) 94 118 } 95 119 } 120 + 121 + func homeEnvName() string { 122 + switch runtime.GOOS { 123 + case "windows": 124 + return "USERPROFILE" 125 + case "plan9": 126 + return "home" 127 + default: 128 + return "HOME" 129 + } 130 + }