Skip to content

Commit

Permalink
fixed case sensitive issues with type flag and fix bug with duplicate…
Browse files Browse the repository at this point in the history
… types #2092 (#2097)
  • Loading branch information
cx-joao-reigota authored Feb 19, 2021
1 parent 738f83f commit 9e1817f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/engine/query/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *FilesystemSource) CheckType(queryPlatform interface{}) bool {
return true
}
if s.types[0] != "" {
return strings.Contains(strings.Join(s.types, ","), queryPlatform.(string))
return strings.Contains(strings.ToUpper(strings.Join(s.types, ",")), strings.ToUpper(queryPlatform.(string)))
}
return true
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (c *Parser) SupportedExtensions() model.Extensions {
}

func validateArguments(types, validArgs []string) error {
validArgs = removeDuplicateValues(validArgs)
if invalidType, ok, _ := contains(types, validArgs); !ok {
return fmt.Errorf(fmt.Sprintf("Unknown Argument: %s\nValid Arguments:\n %s\n", invalidType, strings.Join(validArgs, "\n ")))
}
Expand All @@ -103,13 +104,13 @@ func contains(types, supportedTypes []string) (invalidArgsRes []string, contRes,
}
set := make(map[string]struct{}, len(supportedTypes))
for _, s := range supportedTypes {
set[s] = struct{}{}
set[strings.ToUpper(s)] = struct{}{}
}
cont := true
supported := false
var invalidArgs []string
for _, item := range types {
_, ok := set[item]
_, ok := set[strings.ToUpper(item)]
if !ok {
cont = false
invalidArgs = append(invalidArgs, item)
Expand All @@ -119,3 +120,17 @@ func contains(types, supportedTypes []string) (invalidArgsRes []string, contRes,
}
return invalidArgs, cont, supported
}

// function to remove duplicate values in array
func removeDuplicateValues(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}

for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
61 changes: 61 additions & 0 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"reflect"
"testing"

"github.com/Checkmarx/kics/pkg/model"
Expand Down Expand Up @@ -111,6 +112,14 @@ func TestValidateArguments(t *testing.T) {
},
wantErr: false,
},
{
name: "validate_args_case_sensetive",
args: args{
types: []string{"kubernetes"},
validArgs: []string{"Dockerfile", "Ansible", "Terraform", "CloudFormation", "Kubernetes"},
},
wantErr: false,
},
}

for _, tt := range tests {
Expand All @@ -123,3 +132,55 @@ func TestValidateArguments(t *testing.T) {
})
}
}

func TestRemoveDuplicateValues(t *testing.T) {
type args struct {
stringSlice []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "remove_duplications",
args: args{
stringSlice: []string{
"test",
"test1",
"test",
"test2",
},
},
want: []string{
"test",
"test1",
"test2",
},
},
{
name: "no_duplicates",
args: args{
stringSlice: []string{
"test",
"test1",
"test2",
},
},
want: []string{
"test",
"test1",
"test2",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := removeDuplicateValues(tt.args.stringSlice)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("removeDuplicateValues() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 9e1817f

Please sign in to comment.