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 27, 2024
1 parent 9ec9ace commit 0c29154
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
13 changes: 12 additions & 1 deletion cmd/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,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 416 in cmd/validate/image.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/image.go#L415-L416

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

return cmd
}

// find if the slice contains "data" output
func containsData(data []string) bool {
for _, item := range data {
newItem := strings.Split(item, "=")
if newItem[0] == "data" {
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)
}
}
2 changes: 1 addition & 1 deletion cmd/validate/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func validateInputCmd(validate InputValidationFunc) *cobra.Command {
} else {
inputs = append(inputs, r.input)
// 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 182 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L179-L182

Added lines #L179 - L182 were not covered by tests
manyPolicyInput = append(manyPolicyInput, r.policyInput)
Expand Down

0 comments on commit 0c29154

Please sign in to comment.