mirror of
https://github.com/helmfile/helmfile.git
synced 2026-09-30 08:32:48 +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:
@@ -3,7 +3,6 @@ package tmpl
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -286,7 +285,7 @@ func TestRequiredEnv(t *testing.T) {
|
||||
require.Emptyf(t, envVal, "Expected empty string to be returned when environment variable %s is not set", envKey)
|
||||
|
||||
// test that the environment variable is set to an empty string
|
||||
os.Setenv(envKey, "")
|
||||
t.Setenv(envKey, "")
|
||||
envVal, err = RequiredEnv(envKey)
|
||||
|
||||
require.NotNilf(t, err, "Expected error to be returned when environment variable %s is set to an empty string", envKey)
|
||||
@@ -294,10 +293,8 @@ func TestRequiredEnv(t *testing.T) {
|
||||
|
||||
// test that the environment variable is set to a non-empty string
|
||||
expected := "helmfile"
|
||||
os.Setenv(envKey, expected)
|
||||
t.Setenv(envKey, expected)
|
||||
|
||||
// Unset the environment variable
|
||||
defer os.Unsetenv(envKey)
|
||||
envVal, err = RequiredEnv(envKey)
|
||||
require.Nilf(t, err, "Expected no error to be returned when environment variable %s is set to a non-empty string", envKey)
|
||||
require.Equalf(t, expected, envVal, "Expected %s to be returned when environment variable %s is set to a non-empty string", expected, envKey)
|
||||
@@ -348,8 +345,7 @@ func TestEnvExec(t *testing.T) {
|
||||
require.Emptyf(t, output, "Expected empty string to be returned when executing command with no environment variables")
|
||||
|
||||
// test that the command is executed with os environment variables
|
||||
os.Setenv(testKey, "foo")
|
||||
defer os.Unsetenv(testKey)
|
||||
t.Setenv(testKey, "foo")
|
||||
output, err = ctx.EnvExec(nil, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "")
|
||||
|
||||
require.Nilf(t, err, "Expected no error to be returned when executing command with environment variables")
|
||||
|
||||
@@ -2,7 +2,6 @@ package tmpl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@@ -234,10 +233,7 @@ func Test_renderTemplateToString(t *testing.T) {
|
||||
tt := tests[i]
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
for k, v := range tt.args.envs {
|
||||
err := os.Setenv(k, v)
|
||||
if err != nil {
|
||||
t.Error("renderTemplateToString() could not set env var for testing")
|
||||
}
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
got, err := renderTemplateToString(tt.args.s, tt.args.data)
|
||||
if (err != nil) != tt.wantErr {
|
||||
|
||||
Reference in New Issue
Block a user