Skip to content

Commit

Permalink
Update ignored fields matching logic (#29)
Browse files Browse the repository at this point in the history
* Update ignored fields matching logic to be agnostic of colons contained in field names

* Update ignored fields matching logic to only match (and ignore) errors where the ignored field appears at the end of the error string

* Use Compile instead of MustCompile when testing the ignored fields regexp to avoid panic and instead return error to the user
  • Loading branch information
kkajla12 authored May 19, 2023
1 parent 368b36e commit 336160f
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,20 +362,19 @@ func (suite TestSuite) compareObjects(obj map[string]interface{}, expectedObj ma
// github.com/go-test/deep package's Equal method
// continues to return errors in the expected format.
deepLibDiffs := deep.Equal(obj, expectedObj)
ignoredFieldsMatchExpr := fmt.Sprintf(`\[%s\]$`, strings.Join(suite.spec.IgnoredFields, `\]|\[`))
ignoredFieldsMatchRegExp, err := regexp.Compile(fmt.Sprintf(`\[%s\]$`, strings.Join(suite.spec.IgnoredFields, `\]$|\[`)))
if err != nil {
return diffs, errors.Wrap(err, "invalid ignored fields regexp")
}

for _, diff := range deepLibDiffs {
field, _, found := strings.Cut(diff, ":")
field, _, found := strings.Cut(diff, ": ")
if !found {
return diffs, fmt.Errorf("invalid diff %s returned by deep.Equal", diff)
}

// ignore errors that match an ignored field
matched, err := regexp.MatchString(ignoredFieldsMatchExpr, field)
if err != nil {
return diffs, err
return diffs, fmt.Errorf("unexpectedly formatted diff %s returned by deep.Equal", diff)
}

if !matched {
// Only register errors that don't match an ignored field
if !ignoredFieldsMatchRegExp.Match([]byte(field)) {
diffs = append(diffs, diff)
}
}
Expand Down

0 comments on commit 336160f

Please sign in to comment.