From f2d8accbbb13bfe9fc81c4ae68a1d0576a66856e Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 10 Mar 2026 11:12:52 -0700 Subject: [PATCH] Fix maxItems test to match minItems with ErrorMatcher --- .../pkg/api/validate/limits_test.go | 42 +++++++------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/api/validate/limits_test.go b/staging/src/k8s.io/apimachinery/pkg/api/validate/limits_test.go index 6dee7b395fc..ff1cc60f270 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/validate/limits_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/validate/limits_test.go @@ -19,7 +19,6 @@ package validate import ( "context" "fmt" - "regexp" "testing" "k8s.io/apimachinery/pkg/api/operation" @@ -166,10 +165,10 @@ func TestMinItems(t *testing.T) { func TestMaxItems(t *testing.T) { cases := []struct { - name string - items int - max int - err string // regex + name string + items int + max int + wantErrs field.ErrorList }{{ name: "0 items, max 0", items: 0, @@ -178,7 +177,9 @@ func TestMaxItems(t *testing.T) { name: "1 item, max 0", items: 1, max: 0, - err: "fldpath: Too many.*must have at most", + wantErrs: field.ErrorList{ + field.TooMany(field.NewPath("fldpath"), 1, 0).WithOrigin("maxItems"), + }, }, { name: "1 item, max 1", items: 1, @@ -187,35 +188,24 @@ func TestMaxItems(t *testing.T) { name: "2 items, max 1", items: 2, max: 1, - err: "fldpath: Too many.*must have at most", + wantErrs: field.ErrorList{ + field.TooMany(field.NewPath("fldpath"), 2, 1).WithOrigin("maxItems"), + }, }, { name: "0 items, max -1", items: 0, max: -1, - err: "fldpath: Too many.*too many items", + wantErrs: field.ErrorList{ + field.TooMany(field.NewPath("fldpath"), 0, -1).WithOrigin("maxItems"), + }, }} + matcher := field.ErrorMatcher{}.ByOrigin().ByDetailSubstring().ByField().ByType() for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { value := make([]bool, tc.items) - result := MaxItems(context.Background(), operation.Operation{}, field.NewPath("fldpath"), value, nil, tc.max) - if len(result) > 0 && tc.err == "" { - t.Errorf("unexpected failure: %v", fmtErrs(result)) - return - } - if len(result) == 0 && tc.err != "" { - t.Errorf("unexpected success: expected %q", tc.err) - return - } - if len(result) > 0 { - if len(result) > 1 { - t.Errorf("unexepected multi-error: %v", fmtErrs(result)) - return - } - if re := regexp.MustCompile(tc.err); !re.MatchString(result[0].Error()) { - t.Errorf("wrong error\nexpected: %q\n got: %v", tc.err, fmtErrs(result)) - } - } + gotErrs := MaxItems(context.Background(), operation.Operation{}, field.NewPath("fldpath"), value, nil, tc.max) + matcher.Test(t, tc.wantErrs, gotErrs) }) } }