Skip to content

Commit 88c9a4b

Browse files
authored
Use t.Setenv function to set env in unit tests (#76)
Signed-off-by: Lam Tran <[email protected]>
1 parent cd4f865 commit 88c9a4b

File tree

5 files changed

+10
-27
lines changed

5 files changed

+10
-27
lines changed

cmd/istioctl_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ func TestIstioctl_istioctlArgChecks(t *testing.T) {
6060
_, err = f.Write(raw)
6161
require.NoError(t, err)
6262

63-
require.NoError(t, os.Setenv("GETMESH_TEST_MANIFEST_PATH", f.Name()))
64-
defer func() {
65-
require.NoError(t, os.Setenv("GETMESH_TEST_MANIFEST_PATH", ""))
66-
}()
63+
t.Setenv("GETMESH_TEST_MANIFEST_PATH", f.Name())
6764

6865
t.Run("ok", func(t *testing.T) {
6966
out, err := istioctlArgChecks([]string{"analyze"}, nil, "")

cmd/switch_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ func Test_switchParse(t *testing.T) {
6161
_, err = f.Write(raw)
6262
require.NoError(t, err)
6363

64-
require.NoError(t, os.Setenv("GETMESH_TEST_MANIFEST_PATH", f.Name()))
65-
defer func() {
66-
require.NoError(t, os.Setenv("GETMESH_TEST_MANIFEST_PATH", ""))
67-
}()
64+
t.Setenv("GETMESH_TEST_MANIFEST_PATH", f.Name())
6865

6966
// set up active distro
7067
d := &manifest.IstioDistribution{

e2e/e2e_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ func TestFetch(t *testing.T) {
7474
home, err := ioutil.TempDir("", "")
7575
require.NoError(t, err)
7676
defer os.RemoveAll(home)
77-
require.NoError(t, os.Setenv("GETMESH_HOME", home))
78-
defer os.Setenv("GETMESH_HOME", "")
77+
t.Setenv("GETMESH_HOME", home)
7978

8079
cmd := exec.Command("./getmesh", "fetch", "--version=1.8.6", "--flavor=tetrate", "--flavor-version=0")
8180
buf := new(bytes.Buffer)
@@ -134,8 +133,7 @@ func TestPrune(t *testing.T) {
134133
home, err := ioutil.TempDir("", "")
135134
require.NoError(t, err)
136135
defer os.RemoveAll(home)
137-
require.NoError(t, os.Setenv("GETMESH_HOME", home))
138-
defer os.Setenv("GETMESH_HOME", "")
136+
t.Setenv("GETMESH_HOME", home)
139137

140138
t.Run("specific", func(t *testing.T) {
141139
var version = "1.7.8"
@@ -205,8 +203,7 @@ func TestShow(t *testing.T) {
205203
home, err := ioutil.TempDir("", "")
206204
require.NoError(t, err)
207205
defer os.RemoveAll(home)
208-
require.NoError(t, os.Setenv("GETMESH_HOME", home))
209-
defer os.Setenv("GETMESH_HOME", "")
206+
t.Setenv("GETMESH_HOME", home)
210207

211208
distros := []struct{ version, flavor, flavorVersion string }{
212209
{
@@ -245,8 +242,7 @@ func TestSwitch(t *testing.T) {
245242
home, err := ioutil.TempDir("", "")
246243
require.NoError(t, err)
247244
defer os.RemoveAll(home)
248-
require.NoError(t, os.Setenv("GETMESH_HOME", home))
249-
defer os.Setenv("GETMESH_HOME", "")
245+
t.Setenv("GETMESH_HOME", home)
250246

251247
distros := []struct{ version, flavor, flavorVersion string }{
252248
{

internal/manifestchecker/endoflife_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func Test_endOfLifeChecker(t *testing.T) {
5858

5959
_, err = f.Write(raw)
6060
require.NoError(t, err)
61-
require.NoError(t, os.Setenv("GETMESH_TEST_MANIFEST_PATH", f.Name()))
61+
t.Setenv("GETMESH_TEST_MANIFEST_PATH", f.Name())
6262

6363
t.Run("ok version", func(t *testing.T) {
6464
require.NoError(t, getmesh.SetIstioVersion(home, &manifest.IstioDistribution{Version: "1.8.1"}))

internal/util/kube_test.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package util
1616

1717
import (
18-
"os"
1918
"testing"
2019

2120
"github.com/stretchr/testify/require"
@@ -24,28 +23,22 @@ import (
2423

2524
func TestGetKubeConfigLocation(t *testing.T) {
2625
t.Run("default", func(t *testing.T) {
27-
original := os.Getenv("KUBECONFIG")
28-
os.Setenv("KUBECONFIG", "")
29-
defer os.Setenv("KUBECONFIG", original)
26+
t.Setenv("KUBECONFIG", "")
3027

3128
config := GetKubeConfigLocation()
3229
require.Equal(t, config, clientcmd.RecommendedHomeFile)
3330
})
3431

3532
t.Run("KUBECONFIG", func(t *testing.T) {
3633
actual := "./testconfig"
37-
original := os.Getenv("KUBECONFIG")
38-
os.Setenv("KUBECONFIG", actual)
39-
defer os.Setenv("KUBECONFIG", original)
34+
t.Setenv("KUBECONFIG", actual)
4035

4136
config := GetKubeConfigLocation()
4237
require.Equal(t, config, actual)
4338
})
4439
t.Run("local", func(t *testing.T) {
4540
actual := "./testconfig"
46-
original := os.Getenv("KUBECONFIG")
47-
os.Setenv("KUBECONFIG", "")
48-
defer os.Setenv("KUBECONFIG", original)
41+
t.Setenv("KUBECONFIG", "")
4942

5043
KubeConfig = actual
5144
config := GetKubeConfigLocation()

0 commit comments

Comments
 (0)