Merge pull request #6760 from mxpv/env

Use t.Setenv instead of os.Setenv
This commit is contained in:
Maksym Pavlenko 2022-04-01 14:37:13 -07:00 committed by GitHub
commit 9766107a53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 13 deletions

View File

@ -12,6 +12,8 @@ linters:
- unused - unused
- misspell - misspell
- gosec - gosec
- exportloopref # Checks for pointers to enclosing loop variables
- tenv # Detects using os.Setenv instead of t.Setenv since Go 1.17
disable: disable:
- errcheck - errcheck
@ -38,6 +40,10 @@ run:
timeout: 8m timeout: 8m
skip-dirs: skip-dirs:
- api - api
- cluster
- design - design
- docs - docs
- docs/man - docs/man
- releases
- reports
- test # e2e scripts

View File

@ -135,9 +135,7 @@ func TestDetectPigz(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
oldPath := os.Getenv("PATH") t.Setenv("PATH", tempPath)
os.Setenv("PATH", tempPath)
defer os.Setenv("PATH", oldPath)
if pigzPath := detectPigz(); pigzPath == "" { if pigzPath := detectPigz(); pigzPath == "" {
t.Fatal("failed to detect pigz path") t.Fatal("failed to detect pigz path")
@ -145,8 +143,7 @@ func TestDetectPigz(t *testing.T) {
t.Fatalf("wrong pigz found: %s != %s", pigzPath, fullPath) t.Fatalf("wrong pigz found: %s != %s", pigzPath, fullPath)
} }
os.Setenv(disablePigzEnv, "1") t.Setenv(disablePigzEnv, "1")
defer os.Unsetenv(disablePigzEnv)
if pigzPath := detectPigz(); pigzPath != "" { if pigzPath := detectPigz(); pigzPath != "" {
t.Fatalf("disable via %s doesn't work", disablePigzEnv) t.Fatalf("disable via %s doesn't work", disablePigzEnv)

View File

@ -18,7 +18,6 @@ package namespaces
import ( import (
"context" "context"
"os"
"testing" "testing"
) )
@ -47,9 +46,6 @@ func TestContext(t *testing.T) {
} }
func TestNamespaceFromEnv(t *testing.T) { func TestNamespaceFromEnv(t *testing.T) {
oldenv := os.Getenv(NamespaceEnvVar)
defer os.Setenv(NamespaceEnvVar, oldenv) // restore old env var
ctx := context.Background() ctx := context.Background()
namespace, ok := Namespace(ctx) namespace, ok := Namespace(ctx)
if ok { if ok {
@ -61,7 +57,7 @@ func TestNamespaceFromEnv(t *testing.T) {
} }
expected := "test-namespace" expected := "test-namespace"
os.Setenv(NamespaceEnvVar, expected) t.Setenv(NamespaceEnvVar, expected)
nctx := NamespaceFromEnv(ctx) nctx := NamespaceFromEnv(ctx)
namespace, ok = Namespace(nctx) namespace, ok = Namespace(nctx)

View File

@ -18,7 +18,6 @@ package shim
import ( import (
"context" "context"
"os"
"runtime" "runtime"
"testing" "testing"
) )
@ -27,7 +26,7 @@ func TestRuntimeWithEmptyMaxEnvProcs(t *testing.T) {
var oldGoMaxProcs = runtime.GOMAXPROCS(0) var oldGoMaxProcs = runtime.GOMAXPROCS(0)
defer runtime.GOMAXPROCS(oldGoMaxProcs) defer runtime.GOMAXPROCS(oldGoMaxProcs)
os.Setenv("GOMAXPROCS", "") t.Setenv("GOMAXPROCS", "")
setRuntime() setRuntime()
var currentGoMaxProcs = runtime.GOMAXPROCS(0) var currentGoMaxProcs = runtime.GOMAXPROCS(0)
@ -37,7 +36,7 @@ func TestRuntimeWithEmptyMaxEnvProcs(t *testing.T) {
} }
func TestRuntimeWithNonEmptyMaxEnvProcs(t *testing.T) { func TestRuntimeWithNonEmptyMaxEnvProcs(t *testing.T) {
os.Setenv("GOMAXPROCS", "not_empty") t.Setenv("GOMAXPROCS", "not_empty")
setRuntime() setRuntime()
var oldGoMaxProcs2 = runtime.GOMAXPROCS(0) var oldGoMaxProcs2 = runtime.GOMAXPROCS(0)
if oldGoMaxProcs2 != runtime.NumCPU() { if oldGoMaxProcs2 != runtime.NumCPU() {