Skip to content

Commit

Permalink
test: use T.Setenv to set env vars in tests (#6037)
Browse files Browse the repository at this point in the history
This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv
Signed-off-by: Eng Zer Jun <[email protected]>

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Aug 19, 2022
1 parent 79a57cd commit 53a733b
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 21 deletions.
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

0 comments on commit 53a733b

Please sign in to comment.