Replace os.Setenv with testing.T.Setenv in tests

T.Setenv ensures that the environment is returned to its prior state
when the test ends. It also panics when called from a parallel test to
prevent racy test interdependencies.
This commit is contained in:
Chris Bandy
2023-04-15 10:09:47 -05:00
parent c07c739f04
commit 2e76ac31fd
4 changed files with 9 additions and 14 deletions

View File

@@ -32,7 +32,7 @@ var goBinary = flag.String("go", "", "path to a `go` binary")
func TestVerify(t *testing.T) {
// x/tools/packages is going to literally exec `go`, so it needs some
// setup.
setEnvVars()
setEnvVars(t)
tcs := []struct {
path string
@@ -57,17 +57,18 @@ func TestVerify(t *testing.T) {
}
}
func setEnvVars() {
func setEnvVars(t testing.TB) {
t.Helper()
if *goBinary != "" {
newPath := filepath.Dir(*goBinary)
curPath := os.Getenv("PATH")
if curPath != "" {
newPath = newPath + ":" + curPath
}
os.Setenv("PATH", newPath)
t.Setenv("PATH", newPath)
}
if os.Getenv("HOME") == "" {
os.Setenv("HOME", "/tmp")
t.Setenv("HOME", "/tmp")
}
}