add unittest for RequiredEnv func

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2022-04-05 18:03:44 +08:00
parent 4a52ce0ac2
commit 5cb82ec3d1
1 changed files with 31 additions and 0 deletions

View File

@ -3,11 +3,13 @@ package tmpl
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)
func TestReadFile(t *testing.T) {
@ -221,3 +223,32 @@ func TestRequired(t *testing.T) {
})
}
}
// TestRequiredEnv tests that RequiredEnv returns an error if the environment variable is not set.
func TestRequiredEnv(t *testing.T) {
// test that the environment variable is not set
envKey := "HelmFile"
envVal, err := RequiredEnv(envKey)
require.NotNilf(t, err, "Expected error to be returned when environment variable %s is not set", envKey)
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, "")
envVal, err = RequiredEnv(envKey)
require.NotNilf(t, err, "Expected error to be returned when environment variable %s is set to an empty string", envKey)
require.Emptyf(t, envVal, "Expected empty string to be returned when environment variable %s is set to an empty string", envKey)
// test that the environment variable is set to a non-empty string
expected := "helmfile"
os.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)
}