Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.Setenv to set env vars in tests #6037

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/envinfo/envinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestSetEnvInfo(t *testing.T) {
t.Fatal(err)
}
defer tempEnvFile.Close()
os.Setenv(envInfoEnvName, tempEnvFile.Name())
t.Setenv(envInfoEnvName, tempEnvFile.Name())
testDebugPort := 5005
invalidParam := "invalidParameter"

Expand Down Expand Up @@ -85,7 +85,7 @@ func TestUnsetEnvInfo(t *testing.T) {
t.Fatal(err)
}
defer tempEnvFile.Close()
os.Setenv(envInfoEnvName, tempEnvFile.Name())
t.Setenv(envInfoEnvName, tempEnvFile.Name())
testDebugPort := 15005
invalidParam := "invalidParameter"

Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/genericclioptions/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestNew(t *testing.T) {
os.RemoveAll(prefixDir)
// the second one to cleanup after execution
defer os.RemoveAll(prefixDir)
os.Setenv("KUBECONFIG", filepath.Join(prefixDir, ".kube", "config"))
t.Setenv("KUBECONFIG", filepath.Join(prefixDir, ".kube", "config"))
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand Down
14 changes: 7 additions & 7 deletions pkg/preference/implem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNew(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())

tests := []struct {
name string
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestGetPushTimeout(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())

nonzeroValue := 5 * time.Second

Expand Down Expand Up @@ -123,7 +123,7 @@ func TestGetTimeout(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
zeroValue := 0 * time.Second
nonzeroValue := 5 * time.Second
tests := []struct {
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestSetConfiguration(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false
minValue := minimumDurationValue
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestConsentTelemetry(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false

Expand Down Expand Up @@ -463,7 +463,7 @@ func TestGetupdateNotification(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false

Expand Down Expand Up @@ -692,7 +692,7 @@ func TestGetConsentTelemetry(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false

Expand Down
3 changes: 1 addition & 2 deletions pkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ OdoSettings:
t.Error(err)
}

os.Setenv(preference.GlobalConfigEnvName, tempConfigFile.Name())
defer os.Unsetenv(preference.GlobalConfigEnvName)
t.Setenv(preference.GlobalConfigEnvName, tempConfigFile.Name())

tests := []struct {
name string
Expand Down
3 changes: 1 addition & 2 deletions pkg/registry/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package registry

import (
"io/ioutil"
"os"
"reflect"
"testing"

Expand All @@ -20,7 +19,7 @@ func TestIsSecure(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
os.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())

tests := []struct {
name string
Expand Down
7 changes: 1 addition & 6 deletions pkg/segment/integrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/devfile/registry-support/registry-library/library"
Expand All @@ -18,11 +17,7 @@ func TestGetRegistryOptions(t *testing.T) {
}
defer tempConfigFile.Close()

err = os.Setenv(preference.GlobalConfigEnvName, tempConfigFile.Name())

if err != nil {
t.Fatal(err)
}
t.Setenv(preference.GlobalConfigEnvName, tempConfigFile.Name())

tests := []struct {
testName string
Expand Down
2 changes: 1 addition & 1 deletion pkg/segment/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestIsTelemetryEnabled(t *testing.T) {
},
}
for _, tt := range tests {
os.Setenv(DisableTelemetryEnv, tt.envVar)
t.Setenv(DisableTelemetryEnv, tt.envVar)
ctrl := gomock.NewController(t)
cfg := preference.NewMockClient(ctrl)
cfg.EXPECT().GetConsentTelemetry().Return(tt.preferenceValue).AnyTimes()
Expand Down