test: use T.Setenv to set env vars in tests (#189)

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 <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-06-30 09:15:34 +09:00
committed by GitHub
parent a5b7c2b08d
commit ce3b81e2a1
8 changed files with 20 additions and 52 deletions
+2 -4
View File
@@ -736,14 +736,13 @@ func Test_getTillerlessEnv(t *testing.T) {
t.Errorf("getTillerlessEnv() KUBECONFIG\nactual = %s\nexpect = nil", val)
}
os.Setenv("KUBECONFIG", "toto")
t.Setenv("KUBECONFIG", "toto")
actual = context.getTillerlessEnv()
cwd, _ := os.Getwd()
expected := path.Join(cwd, "toto")
if val, found := actual["KUBECONFIG"]; !found || val != expected {
t.Errorf("getTillerlessEnv() KUBECONFIG\nactual = %s\nexpect = %s", val, expected)
}
os.Unsetenv("KUBECONFIG")
}
func Test_mergeEnv(t *testing.T) {
@@ -795,13 +794,12 @@ func Test_IsHelm3(t *testing.T) {
t.Error("helmexec.IsHelm3() - Failed to detect Helm 3")
}
os.Setenv(envvar.Helm3, "1")
t.Setenv(envvar.Helm3, "1")
helm2Runner = mockRunner{output: []byte("Client: v2.16.0+ge13bc94\n")}
helm = New("helm", NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
if !helm.IsHelm3() {
t.Errorf("helmexec.IsHelm3() - Helm3 not detected when %s is set", envvar.Helm3)
}
os.Setenv(envvar.Helm3, "")
}
func Test_GetVersion(t *testing.T) {