Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
test: use T.Setenv to set env vars in tests (#8366)
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]>
  • Loading branch information
Juneezee authored Jul 11, 2022
1 parent 1aeba59 commit 3d8f8bd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
5 changes: 2 additions & 3 deletions pkg/helm/generated_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package helm
import (
"errors"
"log"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -133,8 +132,8 @@ func Test_getVirtualServicePublicHost(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("HOSTNAME_TEMPLATE", tt.hostnameTemplate)
os.Setenv("INGRESS_HOSTNAME_SUFFIX", tt.hostnameSuffix)
t.Setenv("HOSTNAME_TEMPLATE", tt.hostnameTemplate)
t.Setenv("INGRESS_HOSTNAME_SUFFIX", tt.hostnameSuffix)
got, err := getVirtualServicePublicHost(tt.args.svc, tt.args.project, tt.args.stageName)
if (err != nil) != tt.wantErr {
t.Errorf("getVirtualServicePublicHost() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
12 changes: 6 additions & 6 deletions pkg/mesh/ingress_config_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package mesh

import (
"github.com/keptn/go-utils/pkg/lib/v0_2_0"
"os"
"reflect"
"testing"

"github.com/keptn/go-utils/pkg/lib/v0_2_0"
)

func TestGetPublicDeploymentURI(t *testing.T) {
Expand Down Expand Up @@ -64,10 +64,10 @@ func TestGetPublicDeploymentURI(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("INGRESS_HOSTNAME_SUFFIX", tt.ingressHostNameSuffix)
os.Setenv("INGRESS_PORT", tt.ingressPort)
os.Setenv("INGRESS_PROTOCOL", tt.ingressProtocol)
os.Setenv("HOSTNAME_TEMPLATE", tt.hostNameTemplate)
t.Setenv("INGRESS_HOSTNAME_SUFFIX", tt.ingressHostNameSuffix)
t.Setenv("INGRESS_PORT", tt.ingressPort)
t.Setenv("INGRESS_PROTOCOL", tt.ingressProtocol)
t.Setenv("HOSTNAME_TEMPLATE", tt.hostNameTemplate)
if got := GetPublicDeploymentURI(tt.args.event); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetPublicDeploymentURI() = %v, want %v", got, tt.want)
}
Expand Down

0 comments on commit 3d8f8bd

Please sign in to comment.