Skip to content

Commit a488509

Browse files
committed
test: Improve error comparison in resource validation tests
Replace manual error logging with cmp.Diff for more precise error comparisons, using cmpopts to ignore Origin field and support UniqueString comparison.
1 parent 02f7dc5 commit a488509

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

pkg/apis/resource/validation/validation_common_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ package validation
1919
import (
2020
"testing"
2121

22+
"github.com/google/go-cmp/cmp"
23+
"github.com/google/go-cmp/cmp/cmpopts"
2224
"github.com/stretchr/testify/assert"
2325

2426
"k8s.io/apimachinery/pkg/util/validation/field"
27+
"k8s.io/dynamic-resource-allocation/api"
2528
)
2629

2730
// assertFailures compares the expected against the actual errors.
@@ -31,22 +34,13 @@ import (
3134
// is informative.
3235
func assertFailures(tb testing.TB, want, got field.ErrorList) bool {
3336
tb.Helper()
34-
if !assert.Equal(tb, want, got) {
35-
logFailures(tb, "Wanted failures", want)
36-
logFailures(tb, "Got failures", got)
37+
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(field.Error{}, "Origin"), cmp.AllowUnexported(api.UniqueString{})); diff != "" {
38+
tb.Errorf("unexpected field errors (-want, +got):\n%s", diff)
3739
return false
3840
}
3941
return true
4042
}
4143

42-
func logFailures(tb testing.TB, header string, errs field.ErrorList) {
43-
tb.Helper()
44-
tb.Logf("%s:\n", header)
45-
for _, err := range errs {
46-
tb.Logf("- %s\n", err)
47-
}
48-
}
49-
5044
func TestTruncateIfTooLong(t *testing.T) {
5145
for name, tc := range map[string]struct {
5246
str string

0 commit comments

Comments
 (0)