From 8b08c8e59ce18f6da2ed60e818a2759b02cea644 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sat, 2 Aug 2025 16:54:24 -0700 Subject: [PATCH] Remove ExpectRegexpsByPath() All use cases are converted. Co-Authored-by: Yongrui Lin --- .../optional/nonzero_defaults/doc_test.go | 15 ++--- .../tags/optional/zero_defaults/doc_test.go | 9 +-- .../validation-gen/testscheme/testscheme.go | 60 ------------------- 3 files changed, 13 insertions(+), 71 deletions(-) diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/nonzero_defaults/doc_test.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/nonzero_defaults/doc_test.go index 18841fe4f7f..798a24c3bf8 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/nonzero_defaults/doc_test.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/nonzero_defaults/doc_test.go @@ -19,6 +19,7 @@ package nonzerodefaults import ( "testing" + "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/utils/ptr" ) @@ -27,13 +28,13 @@ func Test(t *testing.T) { st.Value(&Struct{ // All zero-values. - }).ExpectRegexpsByPath(map[string][]string{ - "stringField": {"Required value"}, - "stringPtrField": {"Required value"}, - "intField": {"Required value"}, - "intPtrField": {"Required value"}, - "boolField": {"Required value"}, - "boolPtrField": {"Required value"}, + }).ExpectMatches(field.ErrorMatcher{}.ByType().ByField(), field.ErrorList{ + field.Required(field.NewPath("stringField"), ""), + field.Required(field.NewPath("stringPtrField"), ""), + field.Required(field.NewPath("intField"), ""), + field.Required(field.NewPath("intPtrField"), ""), + field.Required(field.NewPath("boolField"), ""), + field.Required(field.NewPath("boolPtrField"), ""), }) st.Value(&Struct{ diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/zero_defaults/doc_test.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/zero_defaults/doc_test.go index 1a09a9418e0..886b0829e50 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/zero_defaults/doc_test.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/zero_defaults/doc_test.go @@ -19,6 +19,7 @@ package zerodefaults import ( "testing" + "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/utils/ptr" ) @@ -27,13 +28,13 @@ func Test(t *testing.T) { st.Value(&Struct{ // All zero-values. - }).ExpectRegexpsByPath(map[string][]string{ + }).ExpectMatches(field.ErrorMatcher{}.ByType().ByField(), field.ErrorList{ // "stringField": optional value fields with zero defaults are just docs // "intField": optional value fields with zero defaults are just docs // "boolField": optional value fields with zero defaults are just docs - "stringPtrField": {"Required value"}, - "intPtrField": {"Required value"}, - "boolPtrField": {"Required value"}, + field.Required(field.NewPath("stringPtrField"), ""), + field.Required(field.NewPath("intPtrField"), ""), + field.Required(field.NewPath("boolPtrField"), ""), }) st.Value(&Struct{ diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/testscheme/testscheme.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/testscheme/testscheme.go index 07211c6e85d..2461a15dcdf 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/testscheme/testscheme.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/testscheme/testscheme.go @@ -28,7 +28,6 @@ import ( "os" "path" "reflect" - "regexp" "sort" "strings" "testing" @@ -358,65 +357,6 @@ func (v *ValidationTester) validateFalseArgsByPath() map[string][]string { return byPath } -func (v *ValidationTester) ExpectRegexpsByPath(regexpStringsByPath map[string][]string) *ValidationTester { - v.T.Helper() - - v.T.Run(fmt.Sprintf("%T", v.value), func(t *testing.T) { - t.Helper() - - errorsByPath := v.getErrorsByPath() - - // sanity check - if want, got := len(regexpStringsByPath), len(errorsByPath); got != want { - t.Fatalf("wrong number of error-fields: expected %d, got %d:\nwanted:\n%sgot:\n%s", - want, got, renderByPath(regexpStringsByPath), renderByPath(errorsByPath)) - } - - // compile regexps - regexpsByPath := map[string][]*regexp.Regexp{} - for field, strs := range regexpStringsByPath { - regexps := make([]*regexp.Regexp, 0, len(strs)) - for _, str := range strs { - regexps = append(regexps, regexp.MustCompile(str)) - } - regexpsByPath[field] = regexps - } - - for field := range errorsByPath { - errors := errorsByPath[field] - regexps := regexpsByPath[field] - - // sanity check - if want, got := len(regexps), len(errors); got != want { - t.Fatalf("field %q: wrong number of errors: expected %d, got %d:\nwanted:\n%sgot:\n%s", - field, want, got, renderList(regexpStringsByPath[field]), renderList(errors)) - } - - // build a set of errors and expectations, so we can track them, - expSet := sets.New(regexps...) - - for _, err := range errors { - var found *regexp.Regexp - for _, re := range regexps { - if re.MatchString(err) { - found = re - break // done with regexps - } - } - if found != nil { - expSet.Delete(found) - continue // next error - } - t.Errorf("field %q, error %q did not match any expectation", field, err) - } - if len(expSet) != 0 { - t.Errorf("field %q had unsatisfied expectations: %q", field, expSet.UnsortedList()) - } - } - }) - return v -} - func (v *ValidationTester) ExpectMatches(matcher field.ErrorMatcher, expected field.ErrorList) *ValidationTester { v.Helper()