From a320fccdd279ed37b7bec7a065da2a9389ab59f6 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Mon, 16 May 2022 22:54:31 +0800 Subject: [PATCH] update unittest for execEnvs Signed-off-by: yxxhero --- pkg/tmpl/context_funcs_test.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/tmpl/context_funcs_test.go b/pkg/tmpl/context_funcs_test.go index 977692b3..ed1a2116 100644 --- a/pkg/tmpl/context_funcs_test.go +++ b/pkg/tmpl/context_funcs_test.go @@ -273,13 +273,29 @@ func TestExec(t *testing.T) { func TestExecEnvs(t *testing.T) { ctx := &Context{basePath: "."} - expected := "foo\n" + expected := "foo" + + testKey := "testkey" // test that the command is executed with environment variables - output, err := ctx.ExecEnvs(map[string]string{"testkey": "foo"}, "bash", []interface{}{"-c", "printenv testkey"}, "") + output, err := ctx.ExecEnvs(map[string]string{testKey: "foo"}, "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") require.Equalf(t, expected, output, "Expected %s to be returned when executing command with environment variables", expected) + // test that the command is executed with no environment variables + output, err = ctx.ExecEnvs(nil, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "") + require.Nilf(t, err, "Expected no error to be returned when executing command with no environment variables") + + 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) + output, err = ctx.ExecEnvs(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") + + require.Equalf(t, expected, output, "Expected %s to be returned when executing command with environment variables", expected) }