mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-18 09:05:47 +02:00
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:
@@ -94,11 +94,9 @@ func TestGetTillerlessEnv(t *testing.T) {
|
||||
hc := &HelmContext{
|
||||
Tillerless: test.tillerless,
|
||||
}
|
||||
os.Setenv(kubeconfigEnv, test.kubeconfig)
|
||||
t.Setenv(kubeconfigEnv, test.kubeconfig)
|
||||
result := hc.getTillerlessEnv()
|
||||
require.Equalf(t, test.expected, result, "expected result %s, received result %s", test.expected, result)
|
||||
|
||||
}
|
||||
defer os.Unsetenv(kubeconfigEnv)
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user