From e4616a9ffcc98fc07b35c9ee28efc6afbbf15515 Mon Sep 17 00:00:00 2001 From: Finzi Date: Fri, 26 Feb 2021 07:25:25 -0300 Subject: [PATCH 01/16] Adding feature containsAny, containsAll, containsOnly and notContains on conditional options Signed-off-by: Finzi --- pkg/formula/input/flag/flag_test.go | 2 +- pkg/formula/input/input.go | 66 +++- pkg/formula/input/prompt/prompt_test.go | 388 +++++++++++++++++++++++- 3 files changed, 453 insertions(+), 3 deletions(-) diff --git a/pkg/formula/input/flag/flag_test.go b/pkg/formula/input/flag/flag_test.go index 9d2a514c5..35d8f1a69 100644 --- a/pkg/formula/input/flag/flag_test.go +++ b/pkg/formula/input/flag/flag_test.go @@ -82,7 +82,7 @@ func TestInputs(t *testing.T) { in: in{ operator: "eq", }, - want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=)"), + want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContains)"), }, { name: "mismatch error operator", diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 7de3d3d65..cb83d1e01 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -40,6 +40,62 @@ func HasRegex(input formula.Input) bool { return len(input.Pattern.Regex) > 0 } +func contains(s []string, str string) bool { + for _, v := range s { + if v == str { + return true + } + } + return false +} + +func valueContainsAny(value string, input string) bool { + splitValue := strings.Split(value, "|") + splitInput := strings.Split(input, "|") + for _, v := range splitInput { + if contains(splitValue, v) { + return true + } + } + return false +} + +func valueContainsAll(value string, input string) bool { + splitValue := strings.Split(value, "|") + splitInput := strings.Split(input, "|") + for _, v := range splitInput { + if !contains(splitValue, v) { + return false + } + } + return true +} + +func valueContainsOnly(value string, input string) bool { + splitValue := strings.Split(value, "|") + splitInput := strings.Split(input, "|") + if len(splitValue) != len(splitInput) { + return false + } + for _, v := range splitInput { + if !contains(splitValue, v) { + return false + } + } + return true +} + +func valueNotContains(value string, input string) bool { + splitValue := strings.Split(value, "|") + splitInput := strings.Split(input, "|") + for _, v := range splitInput { + if contains(splitValue, v) { + return false + } + } + return true +} + func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { if input.Condition.Variable == "" { return true, nil @@ -74,9 +130,17 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { return value < input.Condition.Value, nil case "<=": return value <= input.Condition.Value, nil + case "containsAny": + return valueContainsAny(value, input.Condition.Value), nil + case "containsAll": + return valueContainsAll(value, input.Condition.Value), nil + case "containsOnly": + return valueContainsOnly(value, input.Condition.Value), nil + case "notContains": + return valueNotContains(value, input.Condition.Value), nil default: return false, fmt.Errorf( - "config.json: conditional operator %s not valid. Use any of (==, !=, >, >=, <, <=)", + "config.json: conditional operator %s not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContains)", input.Condition.Operator, ) } diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 2339931fb..210e8f10c 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -341,7 +341,7 @@ func TestConditionalInputs(t *testing.T) { variable: "sample_list", operator: "eq", }, - want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=)"), + want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContains)"), }, { name: "non-existing variable conditional", @@ -689,6 +689,392 @@ func TestMultiselect(t *testing.T) { } } +func TestConditionalInputsWithMultiselect(t *testing.T) { + tests := []struct { + name string + inputJSON string + multiselectValue []string + want error + }{ + { + name: "success multiselect input test with conditional containsAny", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsAny", + "value": "item_2|item_3" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2", "item_3"}, + want: nil, + }, + { + name: "error multiselect input test with conditional containsAny", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsAny", + "value": "item_2|item_3" + } + } + ]`, + multiselectValue: []string{"item_1", "item_4"}, + want: nil, + }, + { + name: "success multiselect input test with conditional containsAll", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsAll", + "value": "item_2|item_4" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2", "item_3", "item_4"}, + want: nil, + }, + { + name: "error multiselect input test with conditional containsAll", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsAll", + "value": "item_2|item_4" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2", "item_3"}, + want: nil, + }, + { + name: "success multiselect input test with conditional containsOnly", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsOnly", + "value": "item_1|item_2" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2"}, + want: nil, + }, + { + name: "error multiselect input test with conditional containsOnly", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsOnly", + "value": "item_1|item_2" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2", "item_3"}, + want: nil, + }, + { + name: "error multiselect input test with conditional containsOnly", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsOnly", + "value": "item_1|item_2" + } + } + ]`, + multiselectValue: []string{"item_1", "item_3"}, + want: nil, + }, + { + name: "error multiselect input test with conditional containsOnly", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsOnly", + "value": "item_1|item_3" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2"}, + want: nil, + }, + { + name: "success multiselect input test with conditional notContains", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "notContains", + "value": "item_3|item_4" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2"}, + want: nil, + }, + { + name: "error multiselect input test with conditional notContains", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "notContains", + "value": "item_2|item_4" + } + } + ]`, + multiselectValue: []string{"item_3", "item_4"}, + want: nil, + }, + { + name: "error multiselect input test with conditional wrong separator", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "containsAny", + "value": "item_2,item_3" + } + } + ]`, + multiselectValue: []string{"item_1", "item_2", "item_3"}, + want: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var inputs []formula.Input + _ = json.Unmarshal([]byte(tt.inputJSON), &inputs) + + setup := formula.Setup{ + Config: formula.Config{ + Inputs: inputs, + }, + FormulaPath: os.TempDir(), + } + + iTextDefault := &mocks.InputDefaultTextMock{} + iTextDefault.On("Text", mock.Anything, mock.Anything, mock.Anything).Return("sample_text", nil) + iMultiselect := &mocks.InputMultiselectMock{} + iMultiselect.On("Multiselect", mock.Anything).Return(tt.multiselectValue, nil) + + inputManager := NewInputManager( + &mocks.CredResolverMock{}, + &mocks.InputListMock{}, + &mocks.InputTextMock{}, + &mocks.InputTextValidatorMock{}, + iTextDefault, + &mocks.InputBoolMock{}, + &mocks.InputPasswordMock{}, + iMultiselect, + ) + + cmd := &exec.Cmd{} + got := inputManager.Inputs(cmd, setup, nil) + + assert.Equal(t, tt.want, got) + }) + } +} + func TestDefaultFlag(t *testing.T) { inputJson := `[ { From 4b4e53f257e064535694c49f0798433749a9f5ba Mon Sep 17 00:00:00 2001 From: Finzi Date: Fri, 26 Feb 2021 08:44:19 -0300 Subject: [PATCH 02/16] Adding conditional feature notContainsAny and notContainsAll Signed-off-by: Finzi --- pkg/formula/input/flag/flag_test.go | 2 +- pkg/formula/input/input.go | 19 +--- pkg/formula/input/prompt/prompt_test.go | 134 +++++++++++++++++++++++- 3 files changed, 135 insertions(+), 20 deletions(-) diff --git a/pkg/formula/input/flag/flag_test.go b/pkg/formula/input/flag/flag_test.go index 35d8f1a69..2b4335227 100644 --- a/pkg/formula/input/flag/flag_test.go +++ b/pkg/formula/input/flag/flag_test.go @@ -82,7 +82,7 @@ func TestInputs(t *testing.T) { in: in{ operator: "eq", }, - want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContains)"), + want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContainsAny, notContainsAll)"), }, { name: "mismatch error operator", diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index cb83d1e01..602920f94 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -85,17 +85,6 @@ func valueContainsOnly(value string, input string) bool { return true } -func valueNotContains(value string, input string) bool { - splitValue := strings.Split(value, "|") - splitInput := strings.Split(input, "|") - for _, v := range splitInput { - if contains(splitValue, v) { - return false - } - } - return true -} - func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { if input.Condition.Variable == "" { return true, nil @@ -136,11 +125,13 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { return valueContainsAll(value, input.Condition.Value), nil case "containsOnly": return valueContainsOnly(value, input.Condition.Value), nil - case "notContains": - return valueNotContains(value, input.Condition.Value), nil + case "notContainsAny": + return !valueContainsAny(value, input.Condition.Value), nil + case "notContainsAll": + return !valueContainsAll(value, input.Condition.Value), nil default: return false, fmt.Errorf( - "config.json: conditional operator %s not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContains)", + "config.json: conditional operator %s not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContainsAny, notContainsAll)", input.Condition.Operator, ) } diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 210e8f10c..c2ac4f914 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -341,7 +341,7 @@ func TestConditionalInputs(t *testing.T) { variable: "sample_list", operator: "eq", }, - want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContains)"), + want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContainsAny, notContainsAll)"), }, { name: "non-existing variable conditional", @@ -945,7 +945,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { want: nil, }, { - name: "success multiselect input test with conditional notContains", + name: "success multiselect input test with conditional notContainsAny", inputJSON: `[ { "name": "sample_multiselect", @@ -967,7 +967,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { "default": "test", "condition": { "variable": "sample_multiselect", - "operator": "notContains", + "operator": "notContainsAny", "value": "item_3|item_4" } } @@ -976,7 +976,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { want: nil, }, { - name: "error multiselect input test with conditional notContains", + name: "error multiselect input test with conditional notContainsAny", inputJSON: `[ { "name": "sample_multiselect", @@ -998,7 +998,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { "default": "test", "condition": { "variable": "sample_multiselect", - "operator": "notContains", + "operator": "notContainsAny", "value": "item_2|item_4" } } @@ -1006,6 +1006,130 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { multiselectValue: []string{"item_3", "item_4"}, want: nil, }, + { + name: "success multiselect input test with conditional notContainsAll", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "notContainsAll", + "value": "item_1|item_4" + } + } + ]`, + multiselectValue: []string{"item_2", "item_3", "item_4"}, + want: nil, + }, + { + name: "success multiselect input test with conditional notContainsAll", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "notContainsAll", + "value": "item_1|item_4" + } + } + ]`, + multiselectValue: []string{"item_2", "item_3"}, + want: nil, + }, + { + name: "error multiselect input test with conditional notContainsAll", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "notContainsAll", + "value": "item_2|item_4" + } + } + ]`, + multiselectValue: []string{"item_2", "item_3", "item_4"}, + want: nil, + }, + { + name: "error multiselect input test with conditional notContainsAll", + inputJSON: `[ + { + "name": "sample_multiselect", + "type": "multiselect", + "items": [ + "item_1", + "item_2", + "item_3", + "item_4" + ], + "label": "Choose one or more items: ", + "required": false, + "tutorial": "Select one or more items for this field." + }, + { + "name": "sample_text", + "type": "text", + "label": "Type : ", + "default": "test", + "condition": { + "variable": "sample_multiselect", + "operator": "notContainsAll", + "value": "item_2|item_4" + } + } + ]`, + multiselectValue: []string{"item_2", "item_4"}, + want: nil, + }, { name: "error multiselect input test with conditional wrong separator", inputJSON: `[ From 6a1e630b12523667f38541586a4bf3d984994953 Mon Sep 17 00:00:00 2001 From: Finzi Date: Tue, 2 Mar 2021 09:35:43 -0300 Subject: [PATCH 03/16] Correcting conditional tests Signed-off-by: Finzi --- pkg/formula/input/prompt/prompt.go | 17 ++-- pkg/formula/input/prompt/prompt_test.go | 105 +++++++++--------------- pkg/formula/runner.go | 4 + pkg/formula/runner/docker/runner.go | 2 +- pkg/formula/runner/local/runner.go | 2 +- 5 files changed, 53 insertions(+), 77 deletions(-) diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index 7a77cafd7..b451547eb 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -65,7 +65,7 @@ func NewInputManager( inBool prompt.InputBool, inPass prompt.InputPassword, inMultiselect prompt.InputMultiselect, -) formula.InputRunner { +) formula.InputRunnerConditionals { return InputManager{ cred: cred, InputList: inList, @@ -78,20 +78,21 @@ func NewInputManager( } } -func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagSet) error { +func (in InputManager) InputsConditionals(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagSet) (bool, error) { config := setup.Config defaultFlag := false + var conditionPass bool if f != nil { defaultFlag, _ = f.GetBool("default") } for _, i := range config.Inputs { items, err := in.loadItems(i, setup.FormulaPath) if err != nil { - return err + return conditionPass, err } - conditionPass, err := input.VerifyConditional(cmd, i) + conditionPass, err = input.VerifyConditional(cmd, i) if err != nil { - return err + return conditionPass, err } if !conditionPass { continue @@ -102,7 +103,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagS if !defaultFlagSet { inputVal, err = in.inputTypeToPrompt(items, i) if err != nil { - return err + return conditionPass, err } } @@ -111,8 +112,10 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagS checkForSameEnv(i.Name) input.AddEnv(cmd, i.Name, inputVal) } + } - return nil + + return conditionPass, nil } func (in InputManager) inputTypeToPrompt(items []string, i formula.Input) (string, error) { diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index c2ac4f914..f803236c2 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -227,7 +227,7 @@ func TestInputManager(t *testing.T) { iMultiselect, ) cmd := &exec.Cmd{} - got := inputManager.Inputs(cmd, setup, nil) + _, got := inputManager.InputsConditionals(cmd, setup, nil) if got != nil { assert.EqualError(t, got, tt.expectedError) @@ -382,7 +382,7 @@ func TestConditionalInputs(t *testing.T) { ) cmd := &exec.Cmd{} - got := inputManager.Inputs(cmd, setup, nil) + _, got := inputManager.InputsConditionals(cmd, setup, nil) assert.Equal(t, tt.want, got) }) @@ -475,7 +475,7 @@ func TestRegexType(t *testing.T) { ) cmd := &exec.Cmd{} - got := inputManager.Inputs(cmd, setup, nil) + _, got := inputManager.InputsConditionals(cmd, setup, nil) assert.Equal(t, tt.want, got) }) @@ -581,7 +581,7 @@ func TestDynamicInputs(t *testing.T) { ) cmd := &exec.Cmd{} - got := inputManager.Inputs(cmd, setup, nil) + _, got := inputManager.InputsConditionals(cmd, setup, nil) if tt.want != nil { assert.NotNil(t, got) @@ -682,7 +682,7 @@ func TestMultiselect(t *testing.T) { ) cmd := &exec.Cmd{} - got := inputManager.Inputs(cmd, setup, nil) + _, got := inputManager.InputsConditionals(cmd, setup, nil) assert.Equal(t, tt.want, got) }) @@ -694,10 +694,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { name string inputJSON string multiselectValue []string - want error + want bool }{ { - name: "success multiselect input test with conditional containsAny", + name: "success: multiselect input test with conditional containsAny", inputJSON: `[ { "name": "sample_multiselect", @@ -725,10 +725,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: nil, + want: true, }, { - name: "error multiselect input test with conditional containsAny", + name: "fail: multiselect input test with conditional containsAny", inputJSON: `[ { "name": "sample_multiselect", @@ -756,10 +756,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_4"}, - want: nil, + want: false, }, { - name: "success multiselect input test with conditional containsAll", + name: "success: multiselect input test with conditional containsAll", inputJSON: `[ { "name": "sample_multiselect", @@ -787,10 +787,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3", "item_4"}, - want: nil, + want: true, }, { - name: "error multiselect input test with conditional containsAll", + name: "fail: multiselect input test with conditional containsAll", inputJSON: `[ { "name": "sample_multiselect", @@ -818,10 +818,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: nil, + want: false, }, { - name: "success multiselect input test with conditional containsOnly", + name: "success: multiselect input test with conditional containsOnly", inputJSON: `[ { "name": "sample_multiselect", @@ -849,10 +849,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: nil, + want: true, }, { - name: "error multiselect input test with conditional containsOnly", + name: "fail: multiselect input test with conditional containsOnly", inputJSON: `[ { "name": "sample_multiselect", @@ -880,10 +880,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: nil, + want: false, }, { - name: "error multiselect input test with conditional containsOnly", + name: "fail: multiselect input test with conditional containsOnly", inputJSON: `[ { "name": "sample_multiselect", @@ -911,10 +911,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_3"}, - want: nil, + want: false, }, { - name: "error multiselect input test with conditional containsOnly", + name: "fail: multiselect input test with conditional containsOnly", inputJSON: `[ { "name": "sample_multiselect", @@ -942,10 +942,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: nil, + want: false, }, { - name: "success multiselect input test with conditional notContainsAny", + name: "success: multiselect input test with conditional notContainsAny", inputJSON: `[ { "name": "sample_multiselect", @@ -973,10 +973,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: nil, + want: true, }, { - name: "error multiselect input test with conditional notContainsAny", + name: "fail: multiselect input test with conditional notContainsAny", inputJSON: `[ { "name": "sample_multiselect", @@ -1004,10 +1004,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_3", "item_4"}, - want: nil, + want: false, }, { - name: "success multiselect input test with conditional notContainsAll", + name: "success: multiselect input test with conditional notContainsAll", inputJSON: `[ { "name": "sample_multiselect", @@ -1035,10 +1035,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: nil, + want: true, }, { - name: "success multiselect input test with conditional notContainsAll", + name: "success: multiselect input test with conditional notContainsAll", inputJSON: `[ { "name": "sample_multiselect", @@ -1066,10 +1066,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3"}, - want: nil, + want: true, }, { - name: "error multiselect input test with conditional notContainsAll", + name: "fail: multiselect input test with conditional notContainsAll", inputJSON: `[ { "name": "sample_multiselect", @@ -1097,10 +1097,10 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: nil, + want: false, }, { - name: "error multiselect input test with conditional notContainsAll", + name: "fail: multiselect input test with conditional notContainsAll", inputJSON: `[ { "name": "sample_multiselect", @@ -1128,38 +1128,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_4"}, - want: nil, - }, - { - name: "error multiselect input test with conditional wrong separator", - inputJSON: `[ - { - "name": "sample_multiselect", - "type": "multiselect", - "items": [ - "item_1", - "item_2", - "item_3", - "item_4" - ], - "label": "Choose one or more items: ", - "required": false, - "tutorial": "Select one or more items for this field." - }, - { - "name": "sample_text", - "type": "text", - "label": "Type : ", - "default": "test", - "condition": { - "variable": "sample_multiselect", - "operator": "containsAny", - "value": "item_2,item_3" - } - } - ]`, - multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: nil, + want: false, }, } @@ -1192,7 +1161,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { ) cmd := &exec.Cmd{} - got := inputManager.Inputs(cmd, setup, nil) + got, _ := inputManager.InputsConditionals(cmd, setup, nil) assert.Equal(t, tt.want, got) }) @@ -1238,7 +1207,7 @@ func TestDefaultFlag(t *testing.T) { r, w, _ := os.Pipe() os.Stdout = w - err := inputManager.Inputs(cmd, setup, flags) + _, err := inputManager.InputsConditionals(cmd, setup, flags) _ = w.Close() out, _ := ioutil.ReadAll(r) @@ -1281,7 +1250,7 @@ func TestEmptyList(t *testing.T) { ) cmd := &exec.Cmd{} - got := inputManager.Inputs(cmd, setup, nil) + _, got := inputManager.InputsConditionals(cmd, setup, nil) assert.Equal(t, fmt.Errorf(EmptyItems, "sample_list"), got) }) diff --git a/pkg/formula/runner.go b/pkg/formula/runner.go index caf20a138..777a58925 100644 --- a/pkg/formula/runner.go +++ b/pkg/formula/runner.go @@ -54,6 +54,10 @@ type InputResolver interface { Resolve(inType api.TermInputType) (InputRunner, error) } +type InputRunnerConditionals interface { + InputsConditionals(cmd *exec.Cmd, setup Setup, flags *pflag.FlagSet) (bool, error) +} + type InputRunner interface { Inputs(cmd *exec.Cmd, setup Setup, flags *pflag.FlagSet) error } diff --git a/pkg/formula/runner/docker/runner.go b/pkg/formula/runner/docker/runner.go index 90c884e38..2f42249cf 100644 --- a/pkg/formula/runner/docker/runner.go +++ b/pkg/formula/runner/docker/runner.go @@ -138,7 +138,7 @@ func (ru RunManager) runDocker(setup formula.Setup, inputType api.TermInputType, return nil, err } - if err := inputRunner.Inputs(cmd, setup, flags); err != nil { + if _, err := inputRunner.Inputs(cmd, setup, flags); err != nil { return nil, err } diff --git a/pkg/formula/runner/local/runner.go b/pkg/formula/runner/local/runner.go index 197ebbbb8..2489227a5 100644 --- a/pkg/formula/runner/local/runner.go +++ b/pkg/formula/runner/local/runner.go @@ -91,7 +91,7 @@ func (ru RunManager) Run(def formula.Definition, inputType api.TermInputType, ve return err } - if err := inputRunner.Inputs(cmd, setup, flags); err != nil { + if _, err := inputRunner.Inputs(cmd, setup, flags); err != nil { return err } From 7c422d827530abcab6798d25ae8816a1595ad37b Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 4 Mar 2021 14:55:53 -0300 Subject: [PATCH 04/16] fix to handle string with conditional contains Signed-off-by: Finzi --- pkg/formula/input/input.go | 65 +++++++++++++++------ pkg/formula/input/prompt/prompt.go | 22 ++++--- pkg/formula/input/prompt/prompt_test.go | 77 ++++++++----------------- pkg/formula/runner.go | 4 -- pkg/formula/runner/docker/runner.go | 2 +- pkg/formula/runner/local/runner.go | 2 +- 6 files changed, 85 insertions(+), 87 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 602920f94..3d9028aaa 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -19,6 +19,7 @@ const ( PassType = "password" DynamicType = "dynamic" Multiselect = "multiselect" + TypeSuffix = "_type" ) // addEnv Add environment variable to run formulas. @@ -40,7 +41,7 @@ func HasRegex(input formula.Input) bool { return len(input.Pattern.Regex) > 0 } -func contains(s []string, str string) bool { +func containsArray(s []string, str string) bool { for _, v := range s { if v == str { return true @@ -49,36 +50,56 @@ func contains(s []string, str string) bool { return false } -func valueContainsAny(value string, input string) bool { - splitValue := strings.Split(value, "|") +func containsSubstring(s string, substr string) bool { + return strings.Contains(s, substr) +} + +func valueContainsAny(inputType string, value string, input string) bool { splitInput := strings.Split(input, "|") - for _, v := range splitInput { - if contains(splitValue, v) { - return true + if(inputType == Multiselect){ + splitValue := strings.Split(value, "|") + for _, i := range splitInput { + if containsArray(splitValue, i) { + return true + } + } + } else { + for _, i := range splitInput { + if containsSubstring(value, i) { + return true + } } } return false } -func valueContainsAll(value string, input string) bool { - splitValue := strings.Split(value, "|") +func valueContainsAll(inputType string, value string, input string) bool { splitInput := strings.Split(input, "|") - for _, v := range splitInput { - if !contains(splitValue, v) { - return false + if(inputType == Multiselect) { + splitValue := strings.Split(value, "|") + for _, v := range splitInput { + if !containsArray(splitValue, v) { + return false + } + } + } else { + for _, v := range splitInput { + if !containsSubstring(value, v) { + return false + } } } return true } -func valueContainsOnly(value string, input string) bool { +func valueContainsOnly(inputType string, value string, input string) bool { splitValue := strings.Split(value, "|") splitInput := strings.Split(input, "|") if len(splitValue) != len(splitInput) { return false } for _, v := range splitInput { - if !contains(splitValue, v) { + if !containsArray(splitValue, v) { return false } } @@ -91,18 +112,24 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { } var value string + var typeValue string variable := input.Condition.Variable for _, envVal := range cmd.Env { components := strings.Split(envVal, "=") if strings.ToLower(components[0]) == variable { value = components[1] - break + } else if strings.ToLower(components[0]) == (variable + TypeSuffix) { + typeValue = components[1] } } if value == "" { return false, fmt.Errorf("config.json: conditional variable %s not found", variable) } + if typeValue == "" { + return false, fmt.Errorf("config.json: conditional variable %s has no type", variable) + } + // Currently using case implementation to avoid adding a dependency module or exposing // the code to the risks of running an eval function on a user-defined variable // optimizations are welcome, being mindful of the points above @@ -120,15 +147,15 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { case "<=": return value <= input.Condition.Value, nil case "containsAny": - return valueContainsAny(value, input.Condition.Value), nil + return valueContainsAny(typeValue, value, input.Condition.Value), nil case "containsAll": - return valueContainsAll(value, input.Condition.Value), nil + return valueContainsAll(typeValue, value, input.Condition.Value), nil case "containsOnly": - return valueContainsOnly(value, input.Condition.Value), nil + return valueContainsOnly(typeValue, value, input.Condition.Value), nil case "notContainsAny": - return !valueContainsAny(value, input.Condition.Value), nil + return !valueContainsAny(typeValue, value, input.Condition.Value), nil case "notContainsAll": - return !valueContainsAll(value, input.Condition.Value), nil + return !valueContainsAll(typeValue, value, input.Condition.Value), nil default: return false, fmt.Errorf( "config.json: conditional operator %s not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContainsAny, notContainsAll)", diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index b451547eb..39e2fccf1 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -43,6 +43,7 @@ const ( DefaultCacheNewLabel = "Type new value?" DefaultCacheQty = 5 EmptyItems = "no items were provided. Please insert a list of items for the input %s in the config.json file of your formula" + TypeSuffix = "_type" ) type InputManager struct { @@ -65,7 +66,7 @@ func NewInputManager( inBool prompt.InputBool, inPass prompt.InputPassword, inMultiselect prompt.InputMultiselect, -) formula.InputRunnerConditionals { +) formula.InputRunner { return InputManager{ cred: cred, InputList: inList, @@ -78,21 +79,20 @@ func NewInputManager( } } -func (in InputManager) InputsConditionals(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagSet) (bool, error) { +func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagSet) error { config := setup.Config defaultFlag := false - var conditionPass bool if f != nil { defaultFlag, _ = f.GetBool("default") } for _, i := range config.Inputs { items, err := in.loadItems(i, setup.FormulaPath) if err != nil { - return conditionPass, err + return err } - conditionPass, err = input.VerifyConditional(cmd, i) + conditionPass, err := input.VerifyConditional(cmd, i) if err != nil { - return conditionPass, err + return err } if !conditionPass { continue @@ -103,7 +103,7 @@ func (in InputManager) InputsConditionals(cmd *exec.Cmd, setup formula.Setup, f if !defaultFlagSet { inputVal, err = in.inputTypeToPrompt(items, i) if err != nil { - return conditionPass, err + return err } } @@ -111,11 +111,13 @@ func (in InputManager) InputsConditionals(cmd *exec.Cmd, setup formula.Setup, f in.persistCache(setup.FormulaPath, inputVal, i, items) checkForSameEnv(i.Name) input.AddEnv(cmd, i.Name, inputVal) + checkForSameEnv(i.Name + TypeSuffix) + input.AddEnv(cmd, i.Name + TypeSuffix, i.Type) } } - return conditionPass, nil + return nil } func (in InputManager) inputTypeToPrompt(items []string, i formula.Input) (string, error) { @@ -168,6 +170,10 @@ func checkForSameEnv(envKey string) { } } +func addTypeToInputVal(inputVal string, inputType string) string { + return inputVal + ", TYPE=" + inputType +} + func (in InputManager) defaultFlag(input formula.Input, defaultFlag bool) (string, bool) { if defaultFlag && input.Default != "" { msg := fmt.Sprintf("Added %s by default: %s", input.Name, input.Default) diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index f803236c2..831d32239 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -227,7 +227,7 @@ func TestInputManager(t *testing.T) { iMultiselect, ) cmd := &exec.Cmd{} - _, got := inputManager.InputsConditionals(cmd, setup, nil) + got := inputManager.Inputs(cmd, setup, nil) if got != nil { assert.EqualError(t, got, tt.expectedError) @@ -382,7 +382,7 @@ func TestConditionalInputs(t *testing.T) { ) cmd := &exec.Cmd{} - _, got := inputManager.InputsConditionals(cmd, setup, nil) + got := inputManager.Inputs(cmd, setup, nil) assert.Equal(t, tt.want, got) }) @@ -475,7 +475,7 @@ func TestRegexType(t *testing.T) { ) cmd := &exec.Cmd{} - _, got := inputManager.InputsConditionals(cmd, setup, nil) + got := inputManager.Inputs(cmd, setup, nil) assert.Equal(t, tt.want, got) }) @@ -581,7 +581,7 @@ func TestDynamicInputs(t *testing.T) { ) cmd := &exec.Cmd{} - _, got := inputManager.InputsConditionals(cmd, setup, nil) + got := inputManager.Inputs(cmd, setup, nil) if tt.want != nil { assert.NotNil(t, got) @@ -682,19 +682,19 @@ func TestMultiselect(t *testing.T) { ) cmd := &exec.Cmd{} - _, got := inputManager.InputsConditionals(cmd, setup, nil) + got := inputManager.Inputs(cmd, setup, nil) assert.Equal(t, tt.want, got) }) } } -func TestConditionalInputsWithMultiselect(t *testing.T) { +func TestContainsConditionalInputs(t *testing.T) { tests := []struct { name string inputJSON string multiselectValue []string - want bool + want error }{ { name: "success: multiselect input test with conditional containsAny", @@ -725,7 +725,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: true, + want: nil, }, { name: "fail: multiselect input test with conditional containsAny", @@ -756,7 +756,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_4"}, - want: false, + want: nil, }, { name: "success: multiselect input test with conditional containsAll", @@ -787,7 +787,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3", "item_4"}, - want: true, + want: nil, }, { name: "fail: multiselect input test with conditional containsAll", @@ -818,7 +818,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: false, + want: nil, }, { name: "success: multiselect input test with conditional containsOnly", @@ -849,7 +849,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: true, + want: nil, }, { name: "fail: multiselect input test with conditional containsOnly", @@ -880,7 +880,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: false, + want: nil, }, { name: "fail: multiselect input test with conditional containsOnly", @@ -911,38 +911,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_3"}, - want: false, - }, - { - name: "fail: multiselect input test with conditional containsOnly", - inputJSON: `[ - { - "name": "sample_multiselect", - "type": "multiselect", - "items": [ - "item_1", - "item_2", - "item_3", - "item_4" - ], - "label": "Choose one or more items: ", - "required": false, - "tutorial": "Select one or more items for this field." - }, - { - "name": "sample_text", - "type": "text", - "label": "Type : ", - "default": "test", - "condition": { - "variable": "sample_multiselect", - "operator": "containsOnly", - "value": "item_1|item_3" - } - } - ]`, - multiselectValue: []string{"item_1", "item_2"}, - want: false, + want: nil, }, { name: "success: multiselect input test with conditional notContainsAny", @@ -973,7 +942,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: true, + want: nil, }, { name: "fail: multiselect input test with conditional notContainsAny", @@ -1004,7 +973,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_3", "item_4"}, - want: false, + want: nil, }, { name: "success: multiselect input test with conditional notContainsAll", @@ -1035,7 +1004,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: true, + want: nil, }, { name: "success: multiselect input test with conditional notContainsAll", @@ -1066,7 +1035,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3"}, - want: true, + want: nil, }, { name: "fail: multiselect input test with conditional notContainsAll", @@ -1097,7 +1066,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: false, + want: nil, }, { name: "fail: multiselect input test with conditional notContainsAll", @@ -1128,7 +1097,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_4"}, - want: false, + want: nil, }, } @@ -1161,7 +1130,7 @@ func TestConditionalInputsWithMultiselect(t *testing.T) { ) cmd := &exec.Cmd{} - got, _ := inputManager.InputsConditionals(cmd, setup, nil) + got := inputManager.Inputs(cmd, setup, nil) assert.Equal(t, tt.want, got) }) @@ -1207,7 +1176,7 @@ func TestDefaultFlag(t *testing.T) { r, w, _ := os.Pipe() os.Stdout = w - _, err := inputManager.InputsConditionals(cmd, setup, flags) + err := inputManager.Inputs(cmd, setup, flags) _ = w.Close() out, _ := ioutil.ReadAll(r) @@ -1250,7 +1219,7 @@ func TestEmptyList(t *testing.T) { ) cmd := &exec.Cmd{} - _, got := inputManager.InputsConditionals(cmd, setup, nil) + got := inputManager.Inputs(cmd, setup, nil) assert.Equal(t, fmt.Errorf(EmptyItems, "sample_list"), got) }) diff --git a/pkg/formula/runner.go b/pkg/formula/runner.go index 777a58925..caf20a138 100644 --- a/pkg/formula/runner.go +++ b/pkg/formula/runner.go @@ -54,10 +54,6 @@ type InputResolver interface { Resolve(inType api.TermInputType) (InputRunner, error) } -type InputRunnerConditionals interface { - InputsConditionals(cmd *exec.Cmd, setup Setup, flags *pflag.FlagSet) (bool, error) -} - type InputRunner interface { Inputs(cmd *exec.Cmd, setup Setup, flags *pflag.FlagSet) error } diff --git a/pkg/formula/runner/docker/runner.go b/pkg/formula/runner/docker/runner.go index 2f42249cf..90c884e38 100644 --- a/pkg/formula/runner/docker/runner.go +++ b/pkg/formula/runner/docker/runner.go @@ -138,7 +138,7 @@ func (ru RunManager) runDocker(setup formula.Setup, inputType api.TermInputType, return nil, err } - if _, err := inputRunner.Inputs(cmd, setup, flags); err != nil { + if err := inputRunner.Inputs(cmd, setup, flags); err != nil { return nil, err } diff --git a/pkg/formula/runner/local/runner.go b/pkg/formula/runner/local/runner.go index 2489227a5..197ebbbb8 100644 --- a/pkg/formula/runner/local/runner.go +++ b/pkg/formula/runner/local/runner.go @@ -91,7 +91,7 @@ func (ru RunManager) Run(def formula.Definition, inputType api.TermInputType, ve return err } - if _, err := inputRunner.Inputs(cmd, setup, flags); err != nil { + if err := inputRunner.Inputs(cmd, setup, flags); err != nil { return err } From 2256c90a960e23405a087b8c2e59f48c64d9c050 Mon Sep 17 00:00:00 2001 From: Finzi Date: Mon, 8 Mar 2021 08:09:10 -0300 Subject: [PATCH 05/16] adding tests to conditional input for strigs Signed-off-by: Finzi --- pkg/formula/input/input.go | 20 +- pkg/formula/input/prompt/prompt_test.go | 448 ++++++++++++++++++++++-- 2 files changed, 432 insertions(+), 36 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 3d9028aaa..920143b25 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -93,13 +93,19 @@ func valueContainsAll(inputType string, value string, input string) bool { } func valueContainsOnly(inputType string, value string, input string) bool { - splitValue := strings.Split(value, "|") - splitInput := strings.Split(input, "|") - if len(splitValue) != len(splitInput) { - return false - } - for _, v := range splitInput { - if !containsArray(splitValue, v) { + if (inputType == Multiselect) { + splitInput := strings.Split(input, "|") + splitValue := strings.Split(value, "|") + if (len(splitValue) != len(splitInput)) { + return false + } + for _, v := range splitInput { + if !containsArray(splitValue, v) { + return false + } + } + } else { + if !(strings.ToLower(value) == strings.ToLower(input)) { return false } } diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 831d32239..20cb54707 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -33,6 +33,7 @@ import ( "github.com/ZupIT/ritchie-cli/internal/mocks" "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/formula/input" ) func TestInputManager(t *testing.T) { @@ -689,15 +690,19 @@ func TestMultiselect(t *testing.T) { } } -func TestContainsConditionalInputs(t *testing.T) { +func TestContainsConditionalInputsMultiselect(t *testing.T) { + type TestResults struct { + error error + conditionalResult bool + } tests := []struct { name string inputJSON string multiselectValue []string - want error + want TestResults }{ { - name: "success: multiselect input test with conditional containsAny", + name: "[containsAny] SUCCESS: multiselect input contains the conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -725,10 +730,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: nil, + want: TestResults{nil,true}, }, { - name: "fail: multiselect input test with conditional containsAny", + name: "[containsAny] FAIL: multiselect input does not contain any the conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -756,10 +761,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_4"}, - want: nil, + want: TestResults{nil,false}, }, { - name: "success: multiselect input test with conditional containsAll", + name: "[containsAll] SUCCESS: multiselect input contains all conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -787,10 +792,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3", "item_4"}, - want: nil, + want: TestResults{nil,true}, }, { - name: "fail: multiselect input test with conditional containsAll", + name: "[containsAll] FAIL: multiselect input does not contain all conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -818,10 +823,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: nil, + want: TestResults{nil,false}, }, { - name: "success: multiselect input test with conditional containsOnly", + name: "[containsOnly] SUCCESS: multiselect input contains only conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -849,10 +854,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: nil, + want: TestResults{nil,true}, }, { - name: "fail: multiselect input test with conditional containsOnly", + name: "[containsOnly] FAIL: multiselect input does not contain only conditional values and they are not the same size", inputJSON: `[ { "name": "sample_multiselect", @@ -880,10 +885,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: nil, + want: TestResults{nil,false}, }, { - name: "fail: multiselect input test with conditional containsOnly", + name: "[containsOnly] FAIL: multiselect input does not contain only conditional values and they are the same size", inputJSON: `[ { "name": "sample_multiselect", @@ -911,10 +916,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_3"}, - want: nil, + want: TestResults{nil,false}, }, { - name: "success: multiselect input test with conditional notContainsAny", + name: "[notContainsAny] SUCCESS: multiselect input does not contain any of the conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -942,10 +947,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: nil, + want: TestResults{nil,true}, }, { - name: "fail: multiselect input test with conditional notContainsAny", + name: "[notContainsAny] FAIL: multiselect input contains any of the conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -973,10 +978,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_3", "item_4"}, - want: nil, + want: TestResults{nil,false}, }, { - name: "success: multiselect input test with conditional notContainsAll", + name: "[notContainsAll] SUCCESS: multiselect input only contains one of the conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -1004,10 +1009,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: nil, + want: TestResults{nil,true}, }, { - name: "success: multiselect input test with conditional notContainsAll", + name: "[notContainsAll] SUCCESS: multiselect input does not contain any of the conditional values", inputJSON: `[ { "name": "sample_multiselect", @@ -1035,10 +1040,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3"}, - want: nil, + want: TestResults{nil,true}, }, { - name: "fail: multiselect input test with conditional notContainsAll", + name: "[notContainsAll] FAIL: multiselect input contains all the conditional values and they are the same size", inputJSON: `[ { "name": "sample_multiselect", @@ -1066,10 +1071,10 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: nil, + want: TestResults{nil,false}, }, { - name: "fail: multiselect input test with conditional notContainsAll", + name: "[notContainsAll] FAIL: multiselect input contains all the conditional values and they are not the same size", inputJSON: `[ { "name": "sample_multiselect", @@ -1097,7 +1102,7 @@ func TestContainsConditionalInputs(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_4"}, - want: nil, + want: TestResults{nil,false}, }, } @@ -1130,9 +1135,394 @@ func TestContainsConditionalInputs(t *testing.T) { ) cmd := &exec.Cmd{} + got := inputManager.Inputs(cmd, setup, nil) + result, _ := input.VerifyConditional(cmd, inputs[1]) - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want.error, got) + assert.Equal(t, tt.want.conditionalResult, result) + }) + } +} + +func TestContainsConditionalInputsString(t *testing.T) { + type TestResults struct { + error error + conditionalResult bool + } + tests := []struct { + name string + inputJSON string + textValue string + want TestResults + }{ + { + name: "[containsAny] SUCCESS: text input contains the conditional substring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "containsAny", + "value": "input_2|input_3" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,true}, + textValue: "input_1,input_2,input_3", + }, + { + name: "[containsAny] FAIL: text input does not contain any the conditional substring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "containsAny", + "value": "input_2|input_3" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,false}, + textValue: "input_1,input_4", + }, + { + name: "[containsAll] SUCCESS: text input contains all conditional substrings values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "containsAll", + "value": "input_2|input_4" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,true}, + textValue: "input_1,input_2,input_3,input_4", + }, + { + name: "[containsAll] FAIL: text input does not contain all conditional sbustring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "containsAll", + "value": "input_2|input_4" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,false}, + textValue: "input_1,input_2,input_3", + }, + { + name: "[containsOnly] SUCCESS: text input contains only conditional substring value", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "containsOnly", + "value": "input_1" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,true}, + textValue: "input_1", + }, + { + name: "[containsOnly] FAIL: text input does not contain only conditional substring value", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "containsOnly", + "value": "input_1" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,false}, + textValue: "input_1,input_2", + }, + { + name: "[notContainsAny] SUCCESS: text input does not contain any of the conditional substring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "notContainsAny", + "value": "input_3|input_4" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,true}, + textValue: "input_1,input_2", + }, + { + name: "[notContainsAny] FAIL: text input contains any of the conditional substring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "notContainsAny", + "value": "input_2|input_4" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,false}, + textValue: "input_3,input_4", + }, + { + name: "[notContainsAll] SUCCESS: text input only contains one of the conditional substring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "notContainsAll", + "value": "input_1|input_4" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,true}, + textValue: "input_2,input_3,input_4", + }, + { + name: "[notContainsAll] SUCCESS: text input does not contain any of the conditional substring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "notContainsAll", + "value": "input_1|input_4" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,true}, + textValue: "input_2,input_3", + }, + { + name: "[notContainsAll] FAIL: text input contains all the conditional substring values", + inputJSON: `[ + { + "name": "sample_text1", + "type": "text", + "label": "Type: ", + "default": "text1" + }, + { + "name": "sample_list1", + "type": "list", + "default": "in1", + "items": [ + "in_list1", + "in_list2", + "in_list3", + "in_listN" + ], + "condition": { + "variable": "sample_text1", + "operator": "notContainsAll", + "value": "input_2|input_4" + }, + "label": "Pick your : " + } + ]`, + want: TestResults{nil,false}, + textValue: "input_2,input_3,input_4", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var inputs []formula.Input + _ = json.Unmarshal([]byte(tt.inputJSON), &inputs) + + setup := formula.Setup{ + Config: formula.Config{ + Inputs: inputs, + }, + FormulaPath: os.TempDir(), + } + + iTextDefault := &mocks.InputDefaultTextMock{} + iTextDefault.On("Text", mock.Anything, mock.Anything, mock.Anything).Return(tt.textValue, nil) + iList := &mocks.InputListMock{} + iList.On("List", mock.Anything, mock.Anything, mock.Anything).Return("list value", nil) + + inputManager := NewInputManager( + &mocks.CredResolverMock{}, + iList, + &mocks.InputTextMock{}, + &mocks.InputTextValidatorMock{}, + iTextDefault, + &mocks.InputBoolMock{}, + &mocks.InputPasswordMock{}, + &mocks.InputMultiselectMock{}, + ) + + cmd := &exec.Cmd{} + + got := inputManager.Inputs(cmd, setup, nil) + result, _ := input.VerifyConditional(cmd, inputs[1]) + + assert.Equal(t, tt.want.error, got) + assert.Equal(t, tt.want.conditionalResult, result) }) } } From 34f5d4953e0f306783965b698629737dbfaa623f Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 11 Mar 2021 10:47:07 -0300 Subject: [PATCH 06/16] adding type in envVal for input flag when using contains conditional Signed-off-by: Finzi --- pkg/formula/input/flag/flag.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/formula/input/flag/flag.go b/pkg/formula/input/flag/flag.go index 0b69c3836..2d50e511d 100644 --- a/pkg/formula/input/flag/flag.go +++ b/pkg/formula/input/flag/flag.go @@ -33,6 +33,7 @@ import ( const ( errInvalidInputItemsMsg = "only these input items [%s] are accepted in the %q flag" + TypeSuffix = "_type" ) type InputManager struct { @@ -83,6 +84,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, flags *pflag.F if len(inputVal) != 0 { input.AddEnv(cmd, i.Name, inputVal) + input.AddEnv(cmd, i.Name + TypeSuffix, i.Type) } else { emptyInputs = append(emptyInputs, i) } From 6997d1134a25e8d447c756e20a5d90615ea5a1b7 Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 11 Mar 2021 15:45:32 -0300 Subject: [PATCH 07/16] fixing lint errors Signed-off-by: Finzi --- pkg/formula/input/flag/flag.go | 4 +- pkg/formula/input/input.go | 10 +-- pkg/formula/input/prompt/prompt.go | 8 +-- pkg/formula/input/prompt/prompt_test.go | 86 ++++++++++++------------- 4 files changed, 52 insertions(+), 56 deletions(-) diff --git a/pkg/formula/input/flag/flag.go b/pkg/formula/input/flag/flag.go index 2d50e511d..66de1f98f 100644 --- a/pkg/formula/input/flag/flag.go +++ b/pkg/formula/input/flag/flag.go @@ -33,7 +33,7 @@ import ( const ( errInvalidInputItemsMsg = "only these input items [%s] are accepted in the %q flag" - TypeSuffix = "_type" + TypeSuffix = "_type" ) type InputManager struct { @@ -84,7 +84,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, flags *pflag.F if len(inputVal) != 0 { input.AddEnv(cmd, i.Name, inputVal) - input.AddEnv(cmd, i.Name + TypeSuffix, i.Type) + input.AddEnv(cmd, i.Name+TypeSuffix, i.Type) } else { emptyInputs = append(emptyInputs, i) } diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 920143b25..4e231ecc4 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -56,7 +56,7 @@ func containsSubstring(s string, substr string) bool { func valueContainsAny(inputType string, value string, input string) bool { splitInput := strings.Split(input, "|") - if(inputType == Multiselect){ + if inputType == Multiselect { splitValue := strings.Split(value, "|") for _, i := range splitInput { if containsArray(splitValue, i) { @@ -75,7 +75,7 @@ func valueContainsAny(inputType string, value string, input string) bool { func valueContainsAll(inputType string, value string, input string) bool { splitInput := strings.Split(input, "|") - if(inputType == Multiselect) { + if inputType == Multiselect { splitValue := strings.Split(value, "|") for _, v := range splitInput { if !containsArray(splitValue, v) { @@ -93,10 +93,10 @@ func valueContainsAll(inputType string, value string, input string) bool { } func valueContainsOnly(inputType string, value string, input string) bool { - if (inputType == Multiselect) { + if inputType == Multiselect { splitInput := strings.Split(input, "|") splitValue := strings.Split(value, "|") - if (len(splitValue) != len(splitInput)) { + if len(splitValue) != len(splitInput) { return false } for _, v := range splitInput { @@ -105,7 +105,7 @@ func valueContainsOnly(inputType string, value string, input string) bool { } } } else { - if !(strings.ToLower(value) == strings.ToLower(input)) { + if !(strings.EqualFold(strings.ToLower(value), strings.ToLower(input))) { return false } } diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index 39e2fccf1..2df961652 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -43,7 +43,7 @@ const ( DefaultCacheNewLabel = "Type new value?" DefaultCacheQty = 5 EmptyItems = "no items were provided. Please insert a list of items for the input %s in the config.json file of your formula" - TypeSuffix = "_type" + TypeSuffix = "_type" ) type InputManager struct { @@ -112,7 +112,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagS checkForSameEnv(i.Name) input.AddEnv(cmd, i.Name, inputVal) checkForSameEnv(i.Name + TypeSuffix) - input.AddEnv(cmd, i.Name + TypeSuffix, i.Type) + input.AddEnv(cmd, i.Name+TypeSuffix, i.Type) } } @@ -170,10 +170,6 @@ func checkForSameEnv(envKey string) { } } -func addTypeToInputVal(inputVal string, inputType string) string { - return inputVal + ", TYPE=" + inputType -} - func (in InputManager) defaultFlag(input formula.Input, defaultFlag bool) (string, bool) { if defaultFlag && input.Default != "" { msg := fmt.Sprintf("Added %s by default: %s", input.Name, input.Default) diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 20cb54707..fe8888cc9 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -692,8 +692,8 @@ func TestMultiselect(t *testing.T) { func TestContainsConditionalInputsMultiselect(t *testing.T) { type TestResults struct { - error error - conditionalResult bool + error error + conditionalResult bool } tests := []struct { name string @@ -730,7 +730,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: TestResults{nil,true}, + want: TestResults{nil, true}, }, { name: "[containsAny] FAIL: multiselect input does not contain any the conditional values", @@ -761,7 +761,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_4"}, - want: TestResults{nil,false}, + want: TestResults{nil, false}, }, { name: "[containsAll] SUCCESS: multiselect input contains all conditional values", @@ -792,7 +792,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3", "item_4"}, - want: TestResults{nil,true}, + want: TestResults{nil, true}, }, { name: "[containsAll] FAIL: multiselect input does not contain all conditional values", @@ -823,7 +823,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: TestResults{nil,false}, + want: TestResults{nil, false}, }, { name: "[containsOnly] SUCCESS: multiselect input contains only conditional values", @@ -854,7 +854,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: TestResults{nil,true}, + want: TestResults{nil, true}, }, { name: "[containsOnly] FAIL: multiselect input does not contain only conditional values and they are not the same size", @@ -885,7 +885,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: TestResults{nil,false}, + want: TestResults{nil, false}, }, { name: "[containsOnly] FAIL: multiselect input does not contain only conditional values and they are the same size", @@ -916,7 +916,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_3"}, - want: TestResults{nil,false}, + want: TestResults{nil, false}, }, { name: "[notContainsAny] SUCCESS: multiselect input does not contain any of the conditional values", @@ -947,7 +947,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: TestResults{nil,true}, + want: TestResults{nil, true}, }, { name: "[notContainsAny] FAIL: multiselect input contains any of the conditional values", @@ -978,7 +978,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_3", "item_4"}, - want: TestResults{nil,false}, + want: TestResults{nil, false}, }, { name: "[notContainsAll] SUCCESS: multiselect input only contains one of the conditional values", @@ -1009,7 +1009,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: TestResults{nil,true}, + want: TestResults{nil, true}, }, { name: "[notContainsAll] SUCCESS: multiselect input does not contain any of the conditional values", @@ -1040,7 +1040,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3"}, - want: TestResults{nil,true}, + want: TestResults{nil, true}, }, { name: "[notContainsAll] FAIL: multiselect input contains all the conditional values and they are the same size", @@ -1071,7 +1071,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: TestResults{nil,false}, + want: TestResults{nil, false}, }, { name: "[notContainsAll] FAIL: multiselect input contains all the conditional values and they are not the same size", @@ -1102,7 +1102,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_4"}, - want: TestResults{nil,false}, + want: TestResults{nil, false}, }, } @@ -1147,14 +1147,14 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { func TestContainsConditionalInputsString(t *testing.T) { type TestResults struct { - error error - conditionalResult bool + error error + conditionalResult bool } tests := []struct { - name string - inputJSON string - textValue string - want TestResults + name string + inputJSON string + textValue string + want TestResults }{ { name: "[containsAny] SUCCESS: text input contains the conditional substring values", @@ -1183,8 +1183,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,true}, - textValue: "input_1,input_2,input_3", + want: TestResults{nil, true}, + textValue: "input_1,input_2,input_3", }, { name: "[containsAny] FAIL: text input does not contain any the conditional substring values", @@ -1213,8 +1213,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,false}, - textValue: "input_1,input_4", + want: TestResults{nil, false}, + textValue: "input_1,input_4", }, { name: "[containsAll] SUCCESS: text input contains all conditional substrings values", @@ -1243,8 +1243,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,true}, - textValue: "input_1,input_2,input_3,input_4", + want: TestResults{nil, true}, + textValue: "input_1,input_2,input_3,input_4", }, { name: "[containsAll] FAIL: text input does not contain all conditional sbustring values", @@ -1273,8 +1273,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,false}, - textValue: "input_1,input_2,input_3", + want: TestResults{nil, false}, + textValue: "input_1,input_2,input_3", }, { name: "[containsOnly] SUCCESS: text input contains only conditional substring value", @@ -1303,8 +1303,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,true}, - textValue: "input_1", + want: TestResults{nil, true}, + textValue: "input_1", }, { name: "[containsOnly] FAIL: text input does not contain only conditional substring value", @@ -1333,8 +1333,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,false}, - textValue: "input_1,input_2", + want: TestResults{nil, false}, + textValue: "input_1,input_2", }, { name: "[notContainsAny] SUCCESS: text input does not contain any of the conditional substring values", @@ -1363,8 +1363,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,true}, - textValue: "input_1,input_2", + want: TestResults{nil, true}, + textValue: "input_1,input_2", }, { name: "[notContainsAny] FAIL: text input contains any of the conditional substring values", @@ -1393,8 +1393,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,false}, - textValue: "input_3,input_4", + want: TestResults{nil, false}, + textValue: "input_3,input_4", }, { name: "[notContainsAll] SUCCESS: text input only contains one of the conditional substring values", @@ -1423,8 +1423,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,true}, - textValue: "input_2,input_3,input_4", + want: TestResults{nil, true}, + textValue: "input_2,input_3,input_4", }, { name: "[notContainsAll] SUCCESS: text input does not contain any of the conditional substring values", @@ -1453,8 +1453,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,true}, - textValue: "input_2,input_3", + want: TestResults{nil, true}, + textValue: "input_2,input_3", }, { name: "[notContainsAll] FAIL: text input contains all the conditional substring values", @@ -1483,8 +1483,8 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil,false}, - textValue: "input_2,input_3,input_4", + want: TestResults{nil, false}, + textValue: "input_2,input_3,input_4", }, } From f8272f455c03d570595098f702335ffbb97f78ea Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 11 Mar 2021 16:05:34 -0300 Subject: [PATCH 08/16] fixing tests to contemplate type on envVal Signed-off-by: Finzi --- pkg/formula/input/prompt/prompt_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index fe8888cc9..dc4ecfdef 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -236,13 +236,21 @@ func TestInputManager(t *testing.T) { assert.Empty(t, tt.expectedError) expected := []string{ "SAMPLE_TEXT=default value", + "SAMPLE_TEXT_TYPE=text", "SAMPLE_TEXT=default value", + "SAMPLE_TEXT_TYPE=text", "SAMPLE_TEXT_2=default value", + "SAMPLE_TEXT_2_TYPE=text", "SAMPLE_LIST=list value", + "SAMPLE_LIST_TYPE=text", "SAMPLE_LIST2=list value", + "SAMPLE_LIST2_TYPE=list", "SAMPLE_BOOL=true", + "SAMPLE_BOOL_TYPE=bool", "SAMPLE_PASSWORD=pass value", + "SAMPLE_PASSWORD_TYPE=password", "TEST_RESOLVER=resolver value", + "TEST_RESOLVER_TYPE=CREDENTIAL_TEST", } assert.Equal(t, expected, cmd.Env) } From 25670c188c58bb0ddf63177272a4f144e0bdd06c Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 11 Mar 2021 16:26:56 -0300 Subject: [PATCH 09/16] fixing errors after resolve conflicts with master Signed-off-by: Finzi --- pkg/formula/input/input.go | 8 ++++---- pkg/formula/input/prompt/prompt_test.go | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 47a11a1c6..3d6595828 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -21,7 +21,7 @@ const ( DynamicType = "dynamic" MultiselectType = "multiselect" MultiselectSeparator = "|" - TypeSuffix = "_type" + TypeSuffix = "_type" ) // addEnv Add environment variable to run formulas. @@ -58,7 +58,7 @@ func containsSubstring(s string, substr string) bool { func valueContainsAny(inputType string, value string, input string) bool { splitInput := strings.Split(input, "|") - if inputType == Multiselect { + if inputType == MultiselectType { splitValue := strings.Split(value, "|") for _, i := range splitInput { if containsArray(splitValue, i) { @@ -77,7 +77,7 @@ func valueContainsAny(inputType string, value string, input string) bool { func valueContainsAll(inputType string, value string, input string) bool { splitInput := strings.Split(input, "|") - if inputType == Multiselect { + if inputType == MultiselectType { splitValue := strings.Split(value, "|") for _, v := range splitInput { if !containsArray(splitValue, v) { @@ -95,7 +95,7 @@ func valueContainsAll(inputType string, value string, input string) bool { } func valueContainsOnly(inputType string, value string, input string) bool { - if inputType == Multiselect { + if inputType == MultiselectType { splitInput := strings.Split(input, "|") splitValue := strings.Split(value, "|") if len(splitValue) != len(splitInput) { diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 102e33ee2..1e9cc2c13 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -1151,6 +1151,9 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { }, } + inPath := &mocks.InputPathMock{} + inPath.On("Read", "Type : ").Return("", nil) + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var inputs []formula.Input @@ -1177,6 +1180,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { &mocks.InputBoolMock{}, &mocks.InputPasswordMock{}, iMultiselect, + inPath, ) cmd := &exec.Cmd{} @@ -1533,6 +1537,9 @@ func TestContainsConditionalInputsString(t *testing.T) { }, } + inPath := &mocks.InputPathMock{} + inPath.On("Read", "Type : ").Return("", nil) + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var inputs []formula.Input @@ -1559,6 +1566,7 @@ func TestContainsConditionalInputsString(t *testing.T) { &mocks.InputBoolMock{}, &mocks.InputPasswordMock{}, &mocks.InputMultiselectMock{}, + inPath, ) cmd := &exec.Cmd{} From c072057e07cf0693368b904a1128308deeefdb29 Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 11 Mar 2021 16:33:28 -0300 Subject: [PATCH 10/16] fixing lint erros Signed-off-by: Finzi --- pkg/formula/input/flag/flag.go | 2 +- pkg/formula/input/input.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/formula/input/flag/flag.go b/pkg/formula/input/flag/flag.go index f694bade8..e22f42c97 100644 --- a/pkg/formula/input/flag/flag.go +++ b/pkg/formula/input/flag/flag.go @@ -33,7 +33,7 @@ import ( const ( errInvalidInputItemsMsg = "the value [%v] is not valid, only these input items [%s] are accepted in the %q flag" - TypeSuffix = "_type" + TypeSuffix = "_type" ) type InputManager struct { diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 3d6595828..dd357aa3c 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -21,7 +21,7 @@ const ( DynamicType = "dynamic" MultiselectType = "multiselect" MultiselectSeparator = "|" - TypeSuffix = "_type" + TypeSuffix = "_type" ) // addEnv Add environment variable to run formulas. From 2789d1c4697b50117e4d47130c43a52294b71051 Mon Sep 17 00:00:00 2001 From: Finzi Date: Fri, 12 Mar 2021 10:27:19 -0300 Subject: [PATCH 11/16] fixing errors after merge with master Signed-off-by: Finzi --- pkg/formula/input/input.go | 3 +-- pkg/formula/input/prompt/prompt_test.go | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index ef779a5ce..73775df72 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -123,7 +123,6 @@ func valueContainsOnly(inputType string, value string, input string) bool { return true } - func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inputs) (bool, error) { if input.Condition.Variable == "" { @@ -131,7 +130,7 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inp } var typeValue string - var value string + var value string variable := input.Condition.Variable diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 33a994042..8b5191b5e 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -1213,7 +1213,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { cmd := &exec.Cmd{} got := inputManager.Inputs(cmd, setup, nil) - result, _ := input.VerifyConditional(cmd, inputs[1]) + result, _ := input.VerifyConditional(cmd, inputs[1], inputs) assert.Equal(t, tt.want.error, got) assert.Equal(t, tt.want.conditionalResult, result) @@ -1599,7 +1599,7 @@ func TestContainsConditionalInputsString(t *testing.T) { cmd := &exec.Cmd{} got := inputManager.Inputs(cmd, setup, nil) - result, _ := input.VerifyConditional(cmd, inputs[1]) + result, _ := input.VerifyConditional(cmd, inputs[1], inputs) assert.Equal(t, tt.want.error, got) assert.Equal(t, tt.want.conditionalResult, result) From c043d7f7fdb8463ceb800593317daed71b3fbb1f Mon Sep 17 00:00:00 2001 From: Finzi Date: Mon, 15 Mar 2021 08:20:12 -0300 Subject: [PATCH 12/16] fixing string comparison Signed-off-by: Finzi --- pkg/formula/input/input.go | 6 +++--- pkg/formula/input/prompt/prompt.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 73775df72..cb1a7c6ba 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -116,7 +116,7 @@ func valueContainsOnly(inputType string, value string, input string) bool { } } } else { - if !(strings.EqualFold(strings.ToLower(value), strings.ToLower(input))) { + if !strings.EqualFold(value, input) { return false } } @@ -140,9 +140,9 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inp for _, envVal := range cmd.Env { components := strings.Split(envVal, "=") - if strings.ToLower(components[0]) == variable { + if strings.ToLower(components[0]) == strings.ToLower(variable) { value = components[1] - } else if strings.ToLower(components[0]) == (variable + TypeSuffix) { + } else if strings.ToLower(components[0]) == strings.ToLower(variable + TypeSuffix) { typeValue = components[1] } } diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index de001c7c1..f7ad68a90 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -115,7 +115,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagS checkForSameEnv(i.Name) input.AddEnv(cmd, i.Name, inputVal) checkForSameEnv(i.Name + TypeSuffix) - input.AddEnv(cmd, i.Name+TypeSuffix, i.Type) + input.AddEnv(cmd, i.Name + TypeSuffix, i.Type) } } From c73716afc1fb2489b6f6468538c8bd8860d168b2 Mon Sep 17 00:00:00 2001 From: Finzi Date: Mon, 15 Mar 2021 08:29:36 -0300 Subject: [PATCH 13/16] fix lint errors Signed-off-by: Finzi --- pkg/formula/input/input.go | 4 ++-- pkg/formula/input/prompt/prompt.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index cb1a7c6ba..4cb17e9ca 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -140,9 +140,9 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inp for _, envVal := range cmd.Env { components := strings.Split(envVal, "=") - if strings.ToLower(components[0]) == strings.ToLower(variable) { + if strings.EqualFold(components[0], variable) { value = components[1] - } else if strings.ToLower(components[0]) == strings.ToLower(variable + TypeSuffix) { + } else if strings.EqualFold(components[0], variable+TypeSuffix) { typeValue = components[1] } } diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index f7ad68a90..de001c7c1 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -115,7 +115,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagS checkForSameEnv(i.Name) input.AddEnv(cmd, i.Name, inputVal) checkForSameEnv(i.Name + TypeSuffix) - input.AddEnv(cmd, i.Name + TypeSuffix, i.Type) + input.AddEnv(cmd, i.Name+TypeSuffix, i.Type) } } From 0b29971f36241a2305255229bc33519766183500 Mon Sep 17 00:00:00 2001 From: Finzi Date: Mon, 15 Mar 2021 16:54:41 -0300 Subject: [PATCH 14/16] simplification suggested in code review Signed-off-by: Finzi --- pkg/formula/input/input.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 4cb17e9ca..f70e49250 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -116,9 +116,7 @@ func valueContainsOnly(inputType string, value string, input string) bool { } } } else { - if !strings.EqualFold(value, input) { - return false - } + return strings.EqualFold(value, input) } return true } From ddc1da361df8d9440ac38af093948aa28e4d2c8b Mon Sep 17 00:00:00 2001 From: Finzi Date: Wed, 17 Mar 2021 11:29:34 -0300 Subject: [PATCH 15/16] change _type to __type Signed-off-by: Finzi --- pkg/formula/input/flag/flag.go | 2 +- pkg/formula/input/input.go | 2 +- pkg/formula/input/prompt/prompt.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/formula/input/flag/flag.go b/pkg/formula/input/flag/flag.go index 65f037eca..e23c3e860 100644 --- a/pkg/formula/input/flag/flag.go +++ b/pkg/formula/input/flag/flag.go @@ -33,7 +33,7 @@ import ( const ( errInvalidInputItemsMsg = "the value [%v] is not valid, only these input items [%s] are accepted in the %q flag" - TypeSuffix = "_type" + TypeSuffix = "__type" ) type InputManager struct { diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index f70e49250..d16cf9715 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -21,7 +21,7 @@ const ( DynamicType = "dynamic" MultiselectType = "multiselect" MultiselectSeparator = "|" - TypeSuffix = "_type" + TypeSuffix = "__type" ) // addEnv Add environment variable to run formulas. diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index de001c7c1..011c8aeb2 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -43,7 +43,7 @@ const ( DefaultCacheNewLabel = "Type new value?" DefaultCacheQty = 5 EmptyItems = "no items were provided. Please insert a list of items for the input %s in the config.json file of your formula" - TypeSuffix = "_type" + TypeSuffix = "__type" ) type InputManager struct { From 39e83a39daa6f8c0e76ecd2dd23412112fba21ce Mon Sep 17 00:00:00 2001 From: Finzi Date: Wed, 17 Mar 2021 14:29:09 -0300 Subject: [PATCH 16/16] corretions after code review Signed-off-by: Finzi --- pkg/formula/input/input.go | 53 +++++++--------- pkg/formula/input/prompt/prompt_test.go | 84 +++++++++++-------------- 2 files changed, 61 insertions(+), 76 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index d16cf9715..50cb54187 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" ) type InputTextDefault interface { @@ -13,15 +14,16 @@ type InputTextDefault interface { } const ( - TextType = "text" - ListType = "list" - BoolType = "bool" - PassType = "password" - PathType = "path" - DynamicType = "dynamic" - MultiselectType = "multiselect" - MultiselectSeparator = "|" - TypeSuffix = "__type" + TextType = "text" + ListType = "list" + BoolType = "bool" + PassType = "password" + PathType = "path" + DynamicType = "dynamic" + MultiselectType = "multiselect" + MultiselectSeparator = "|" + InputConditionalSeparator = "|" + TypeSuffix = "__type" ) // addEnv Add environment variable to run formulas. @@ -43,15 +45,6 @@ func HasRegex(input formula.Input) bool { return len(input.Pattern.Regex) > 0 } -func containsArray(s []string, str string) bool { - for _, v := range s { - if v == str { - return true - } - } - return false -} - func inputConditionVariableExistsOnInputList(variable string, inputList formula.Inputs) bool { for _, inputListElement := range inputList { if inputListElement.Name == variable { @@ -66,11 +59,11 @@ func containsSubstring(s string, substr string) bool { } func valueContainsAny(inputType string, value string, input string) bool { - splitInput := strings.Split(input, "|") + splitInput := strings.Split(input, InputConditionalSeparator) if inputType == MultiselectType { - splitValue := strings.Split(value, "|") + splitValue := strings.Split(value, MultiselectSeparator) for _, i := range splitInput { - if containsArray(splitValue, i) { + if sliceutil.Contains(splitValue, i) { return true } } @@ -85,11 +78,11 @@ func valueContainsAny(inputType string, value string, input string) bool { } func valueContainsAll(inputType string, value string, input string) bool { - splitInput := strings.Split(input, "|") + splitInput := strings.Split(input, InputConditionalSeparator) if inputType == MultiselectType { - splitValue := strings.Split(value, "|") + splitValue := strings.Split(value, MultiselectSeparator) for _, v := range splitInput { - if !containsArray(splitValue, v) { + if !sliceutil.Contains(splitValue, v) { return false } } @@ -105,13 +98,13 @@ func valueContainsAll(inputType string, value string, input string) bool { func valueContainsOnly(inputType string, value string, input string) bool { if inputType == MultiselectType { - splitInput := strings.Split(input, "|") - splitValue := strings.Split(value, "|") + splitInput := strings.Split(input, InputConditionalSeparator) + splitValue := strings.Split(value, MultiselectSeparator) if len(splitValue) != len(splitInput) { return false } for _, v := range splitInput { - if !containsArray(splitValue, v) { + if !sliceutil.Contains(splitValue, v) { return false } } @@ -127,15 +120,15 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inp return true, nil } - var typeValue string - var value string - variable := input.Condition.Variable if !inputConditionVariableExistsOnInputList(variable, inputList) { return false, fmt.Errorf("config.json: conditional variable %s not found", variable) } + var typeValue string + var value string + for _, envVal := range cmd.Env { components := strings.Split(envVal, "=") if strings.EqualFold(components[0], variable) { diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 8b5191b5e..7e0f9b032 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -257,21 +257,21 @@ func TestInputManager(t *testing.T) { assert.Empty(t, tt.expectedError) expected := []string{ "SAMPLE_TEXT=default value", - "SAMPLE_TEXT_TYPE=text", + "SAMPLE_TEXT__TYPE=text", "SAMPLE_TEXT=default value", - "SAMPLE_TEXT_TYPE=text", + "SAMPLE_TEXT__TYPE=text", "SAMPLE_TEXT_2=default value", - "SAMPLE_TEXT_2_TYPE=text", + "SAMPLE_TEXT_2__TYPE=text", "SAMPLE_LIST=list value", - "SAMPLE_LIST_TYPE=text", + "SAMPLE_LIST__TYPE=text", "SAMPLE_LIST2=list value", - "SAMPLE_LIST2_TYPE=list", + "SAMPLE_LIST2__TYPE=list", "SAMPLE_BOOL=true", - "SAMPLE_BOOL_TYPE=bool", + "SAMPLE_BOOL__TYPE=bool", "SAMPLE_PASSWORD=pass value", - "SAMPLE_PASSWORD_TYPE=password", + "SAMPLE_PASSWORD__TYPE=password", "TEST_RESOLVER=resolver value", - "TEST_RESOLVER_TYPE=CREDENTIAL_TEST", + "TEST_RESOLVER__TYPE=CREDENTIAL_TEST", } assert.Equal(t, expected, cmd.Env) } @@ -763,15 +763,11 @@ func TestMultiselect(t *testing.T) { } func TestContainsConditionalInputsMultiselect(t *testing.T) { - type TestResults struct { - error error - conditionalResult bool - } tests := []struct { name string inputJSON string multiselectValue []string - want TestResults + want bool }{ { name: "[containsAny] SUCCESS: multiselect input contains the conditional values", @@ -802,7 +798,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: TestResults{nil, true}, + want: true, }, { name: "[containsAny] FAIL: multiselect input does not contain any the conditional values", @@ -833,7 +829,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_4"}, - want: TestResults{nil, false}, + want: false, }, { name: "[containsAll] SUCCESS: multiselect input contains all conditional values", @@ -864,7 +860,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3", "item_4"}, - want: TestResults{nil, true}, + want: true, }, { name: "[containsAll] FAIL: multiselect input does not contain all conditional values", @@ -895,7 +891,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: TestResults{nil, false}, + want: false, }, { name: "[containsOnly] SUCCESS: multiselect input contains only conditional values", @@ -926,7 +922,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: TestResults{nil, true}, + want: true, }, { name: "[containsOnly] FAIL: multiselect input does not contain only conditional values and they are not the same size", @@ -957,7 +953,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2", "item_3"}, - want: TestResults{nil, false}, + want: false, }, { name: "[containsOnly] FAIL: multiselect input does not contain only conditional values and they are the same size", @@ -988,7 +984,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_3"}, - want: TestResults{nil, false}, + want: false, }, { name: "[notContainsAny] SUCCESS: multiselect input does not contain any of the conditional values", @@ -1019,7 +1015,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_1", "item_2"}, - want: TestResults{nil, true}, + want: true, }, { name: "[notContainsAny] FAIL: multiselect input contains any of the conditional values", @@ -1050,7 +1046,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_3", "item_4"}, - want: TestResults{nil, false}, + want: false, }, { name: "[notContainsAll] SUCCESS: multiselect input only contains one of the conditional values", @@ -1081,7 +1077,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: TestResults{nil, true}, + want: true, }, { name: "[notContainsAll] SUCCESS: multiselect input does not contain any of the conditional values", @@ -1112,7 +1108,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3"}, - want: TestResults{nil, true}, + want: true, }, { name: "[notContainsAll] FAIL: multiselect input contains all the conditional values and they are the same size", @@ -1143,7 +1139,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_3", "item_4"}, - want: TestResults{nil, false}, + want: false, }, { name: "[notContainsAll] FAIL: multiselect input contains all the conditional values and they are not the same size", @@ -1174,7 +1170,7 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { } ]`, multiselectValue: []string{"item_2", "item_4"}, - want: TestResults{nil, false}, + want: false, }, } @@ -1215,22 +1211,18 @@ func TestContainsConditionalInputsMultiselect(t *testing.T) { got := inputManager.Inputs(cmd, setup, nil) result, _ := input.VerifyConditional(cmd, inputs[1], inputs) - assert.Equal(t, tt.want.error, got) - assert.Equal(t, tt.want.conditionalResult, result) + assert.Equal(t, nil, got) + assert.Equal(t, tt.want, result) }) } } func TestContainsConditionalInputsString(t *testing.T) { - type TestResults struct { - error error - conditionalResult bool - } tests := []struct { name string inputJSON string textValue string - want TestResults + want bool }{ { name: "[containsAny] SUCCESS: text input contains the conditional substring values", @@ -1259,7 +1251,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, true}, + want: true, textValue: "input_1,input_2,input_3", }, { @@ -1289,7 +1281,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, false}, + want: false, textValue: "input_1,input_4", }, { @@ -1319,7 +1311,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, true}, + want: true, textValue: "input_1,input_2,input_3,input_4", }, { @@ -1349,7 +1341,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, false}, + want: false, textValue: "input_1,input_2,input_3", }, { @@ -1379,7 +1371,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, true}, + want: true, textValue: "input_1", }, { @@ -1409,7 +1401,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, false}, + want: false, textValue: "input_1,input_2", }, { @@ -1439,7 +1431,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, true}, + want: true, textValue: "input_1,input_2", }, { @@ -1469,7 +1461,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, false}, + want: false, textValue: "input_3,input_4", }, { @@ -1499,7 +1491,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, true}, + want: true, textValue: "input_2,input_3,input_4", }, { @@ -1529,7 +1521,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, true}, + want: true, textValue: "input_2,input_3", }, { @@ -1559,7 +1551,7 @@ func TestContainsConditionalInputsString(t *testing.T) { "label": "Pick your : " } ]`, - want: TestResults{nil, false}, + want: false, textValue: "input_2,input_3,input_4", }, } @@ -1601,8 +1593,8 @@ func TestContainsConditionalInputsString(t *testing.T) { got := inputManager.Inputs(cmd, setup, nil) result, _ := input.VerifyConditional(cmd, inputs[1], inputs) - assert.Equal(t, tt.want.error, got) - assert.Equal(t, tt.want.conditionalResult, result) + assert.Equal(t, nil, got) + assert.Equal(t, tt.want, result) }) } }