From d7ec4e801357fa5ccfab53669a42f78fc1a69d39 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Thu, 1 Aug 2019 23:26:43 -0700 Subject: [PATCH] add env var tests --- context_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/context_test.go b/context_test.go index f2fc250..47313e1 100644 --- a/context_test.go +++ b/context_test.go @@ -407,6 +407,7 @@ func TestCheckRequiredFlags(t *testing.T) { tdata := []struct { testCase string parseInput []string + envVarInput [2]string flags []Flag expectedAnError bool expectedErrorContents []string @@ -435,6 +436,13 @@ func TestCheckRequiredFlags(t *testing.T) { }, parseInput: []string{"--requiredFlag", "myinput"}, }, + { + testCase: "required_and_present_via_env_var", + flags: []Flag{ + StringFlag{Name: "requiredFlag", Required: true, EnvVar: "REQUIRED_FLAG"}, + }, + envVarInput: [2]string{"REQUIRED_FLAG", "true"}, + }, { testCase: "required_and_optional", flags: []Flag{ @@ -452,6 +460,15 @@ func TestCheckRequiredFlags(t *testing.T) { parseInput: []string{"--optionalFlag", "myinput"}, expectedAnError: true, }, + { + testCase: "required_and_optional_and_optional_present_via_env_var", + flags: []Flag{ + StringFlag{Name: "requiredFlag", Required: true}, + StringFlag{Name: "optionalFlag", EnvVar: "OPTIONAL_FLAG"}, + }, + envVarInput: [2]string{"OPTIONAL_FLAG", "true"}, + expectedAnError: true, + }, { testCase: "required_and_optional_and_required_present", flags: []Flag{ @@ -495,8 +512,13 @@ func TestCheckRequiredFlags(t *testing.T) { flags.Apply(set) } set.Parse(test.parseInput) + if test.envVarInput[0] != "" { + os.Clearenv() + os.Setenv(test.envVarInput[0], test.envVarInput[1]) + } ctx := &Context{} context := NewContext(ctx.App, set, ctx) + context.Command.Flags = test.flags // logic under test err := checkRequiredFlags(test.flags, context)