mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 15:05:20 +00:00
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:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user