Skip to content

Commit

Permalink
only match if output is "data"
Browse files Browse the repository at this point in the history
  • Loading branch information
joejstuart committed Nov 26, 2024
1 parent 9ec9ace commit 5464dbf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmd/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"runtime/trace"
"sort"
"strings"
Expand Down Expand Up @@ -411,7 +412,7 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
} else {
components = append(components, r.component)
// evaluator data is duplicated per component, so only collect it once.
if len(evaluatorData) == 0 {
if len(evaluatorData) == 0 && containsData(data.output) {
evaluatorData = append(evaluatorData, r.data)
}

Check warning on line 417 in cmd/validate/image.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/image.go#L416-L417

Added lines #L416 - L417 were not covered by tests
manyPolicyInput = append(manyPolicyInput, r.policyInput)
Expand Down Expand Up @@ -541,3 +542,17 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {

return cmd
}

// find if the slice contains "data" output
func containsData(data []string) bool {
// regex pattern to match "data" or "data=something"
pattern := `^data(=.*)?$`
re := regexp.MustCompile(pattern)

for _, item := range data {
if re.MatchString(item) {
return true
}
}
return false
}
24 changes: 24 additions & 0 deletions cmd/validate/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,3 +1302,27 @@ func TestValidateImageDefaultOutput(t *testing.T) {
assert.Equal(t, c.expected, out.String())
}
}

// TestContainsData validates containsData behavior
func TestContainsData(t *testing.T) {
tests := []struct {
input []string
expected bool
name string
}{
{[]string{"data"}, true, "Match single data"},
{[]string{"data=something"}, true, "Match data=something"},
{[]string{"text=data-file.txt"}, false, "Do not match text=data-file.txt"},
{[]string{"json", "data=custom-data.yaml"}, true, "Match data in slice with multiple values"},
{[]string{"data text"}, false, "Do not match data text"},
{[]string{"dat"}, false, "Do not match dat"},
{[]string{"data123"}, false, "Do not match data123"},
{[]string{"data="}, true, "Match data="},
{[]string{""}, false, "Do not match empty string"},
}

for _, test := range tests {
result := containsData(test.input)
assert.Equal(t, test.expected, result, test.name)
}
}

0 comments on commit 5464dbf

Please sign in to comment.