From cd9df2f115a95835e07cddf740861dbd8f6f3988 Mon Sep 17 00:00:00 2001 From: Aaron Prindle Date: Tue, 11 Mar 2025 05:24:07 +0000 Subject: [PATCH] chore: change error_matcher.go to use test interface instead of importing testing pkg --- pkg/apis/core/validation/validation_test.go | 9 +++--- .../apimachinery/pkg/runtime/scheme_test.go | 3 +- .../field/{testing => }/error_matcher.go | 29 +++++++++++-------- .../pkg/registry/rest/validate_test.go | 3 +- 4 files changed, 23 insertions(+), 21 deletions(-) rename staging/src/k8s.io/apimachinery/pkg/util/validation/field/{testing => }/error_matcher.go (88%) diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index aced1df257e..9c9fc32229a 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -38,7 +38,6 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation/field" - fldtest "k8s.io/apimachinery/pkg/util/validation/field/testing" "k8s.io/apimachinery/pkg/util/version" utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/component-base/featuregate" @@ -16714,7 +16713,7 @@ func TestValidateReplicationControllerUpdate(t *testing.T) { tc.old.ObjectMeta.ResourceVersion = "1" tc.update.ObjectMeta.ResourceVersion = "1" errs := ValidateReplicationControllerUpdate(&tc.update, &tc.old, PodValidationOptions{}) - matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring() + matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring() matcher.Test(t, tc.expectedErrs, errs) }) } @@ -16864,7 +16863,7 @@ func TestValidateReplicationController(t *testing.T) { for k, tc := range errorCases { t.Run(k, func(t *testing.T) { errs := ValidateReplicationController(&tc.input, PodValidationOptions{}) - matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring() + matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring() matcher.Test(t, tc.expectedErrs, errs) }) } @@ -20770,7 +20769,7 @@ func TestValidateEndpointsCreate(t *testing.T) { t.Run(k, func(t *testing.T) { errs := ValidateEndpointsCreate(&v.endpoints) // TODO: set .RequireOriginWhenInvalid() once metadata is done - matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin() + matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin() matcher.Test(t, v.expectedErrs, errs) }) } @@ -22767,7 +22766,7 @@ func TestValidateTopologySpreadConstraints(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { errs := validateTopologySpreadConstraints(tc.constraints, fieldPath, tc.opts) - matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().RequireOriginWhenInvalid() + matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().RequireOriginWhenInvalid() matcher.Test(t, tc.wantFieldErrors, errs) }) } diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/scheme_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/scheme_test.go index fd87634d60a..b2712c107d7 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/scheme_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/scheme_test.go @@ -35,7 +35,6 @@ import ( utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/validation/field" - fieldtesting "k8s.io/apimachinery/pkg/util/validation/field/testing" ) type testConversions struct { @@ -1089,7 +1088,7 @@ func TestRegisterValidate(t *testing.T) { } else { results = s.ValidateUpdate(ctx, tc.options, tc.object, tc.oldObject, tc.subresource...) } - matcher := fieldtesting.ErrorMatcher{}.ByType().ByField().ByOrigin() + matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin() matcher.Test(t, tc.expected, results) }) } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/testing/error_matcher.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/error_matcher.go similarity index 88% rename from staging/src/k8s.io/apimachinery/pkg/util/validation/field/testing/error_matcher.go rename to staging/src/k8s.io/apimachinery/pkg/util/validation/field/error_matcher.go index 40530035e57..1d15deae868 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/testing/error_matcher.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/error_matcher.go @@ -14,19 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -package testing +package field import ( "fmt" "reflect" "regexp" "strings" - "testing" - - field "k8s.io/apimachinery/pkg/util/validation/field" ) -// ErrorMatcher is a helper for comparing field.Error objects. +// ErrorMatcher is a helper for comparing Error objects. type ErrorMatcher struct { // TODO(thockin): consider whether type is ever NOT required, maybe just // assume it. @@ -42,9 +39,9 @@ type ErrorMatcher struct { requireOriginWhenInvalid bool } -// Matches returns true if the two field.Error objects match according to the +// Matches returns true if the two Error objects match according to the // configured criteria. -func (m ErrorMatcher) Matches(want, got *field.Error) bool { +func (m ErrorMatcher) Matches(want, got *Error) bool { if m.matchType && want.Type != got.Type { return false } @@ -58,7 +55,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool { if want.Origin != got.Origin { return false } - if m.requireOriginWhenInvalid && want.Type == field.ErrorTypeInvalid { + if m.requireOriginWhenInvalid && want.Type == ErrorTypeInvalid { if want.Origin == "" || got.Origin == "" { return false } @@ -72,7 +69,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool { // Render returns a string representation of the specified Error object, // according to the criteria configured in the ErrorMatcher. -func (m ErrorMatcher) Render(e *field.Error) string { +func (m ErrorMatcher) Render(e *Error) string { buf := strings.Builder{} comma := func() { @@ -93,7 +90,7 @@ func (m ErrorMatcher) Render(e *field.Error) string { comma() buf.WriteString(fmt.Sprintf("Value=%v", e.BadValue)) } - if m.matchOrigin || m.requireOriginWhenInvalid && e.Type == field.ErrorTypeInvalid { + if m.matchOrigin || m.requireOriginWhenInvalid && e.Type == ErrorTypeInvalid { comma() buf.WriteString(fmt.Sprintf("Origin=%q", e.Origin)) } @@ -170,17 +167,25 @@ func (m ErrorMatcher) ByDetailRegexp() ErrorMatcher { return m } +// TestIntf lets users pass a testing.T while not coupling this package to Go's +// testing package. +type TestIntf interface { + Helper() + Errorf(format string, args ...any) + Logf(format string, args ...any) +} + // Test compares two ErrorLists by the criteria configured in this matcher, and // fails the test if they don't match. If a given "want" error matches multiple // "got" errors, they will all be consumed. This might be OK (e.g. if there are // multiple errors on the same field from the same origin) or it might be an // insufficiently specific matcher, so these will be logged. -func (m ErrorMatcher) Test(tb testing.TB, want, got field.ErrorList) { +func (m ErrorMatcher) Test(tb TestIntf, want, got ErrorList) { tb.Helper() remaining := got for _, w := range want { - tmp := make(field.ErrorList, 0, len(remaining)) + tmp := make(ErrorList, 0, len(remaining)) n := 0 for _, g := range remaining { if m.Matches(w, g) { diff --git a/staging/src/k8s.io/apiserver/pkg/registry/rest/validate_test.go b/staging/src/k8s.io/apiserver/pkg/registry/rest/validate_test.go index 8ed9ed9adee..afe59e7c5ce 100644 --- a/staging/src/k8s.io/apiserver/pkg/registry/rest/validate_test.go +++ b/staging/src/k8s.io/apiserver/pkg/registry/rest/validate_test.go @@ -29,7 +29,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/validation/field" - fieldtesting "k8s.io/apimachinery/pkg/util/validation/field/testing" genericapirequest "k8s.io/apiserver/pkg/endpoints/request" ) @@ -154,7 +153,7 @@ func TestValidateDeclaratively(t *testing.T) { } else { results = ValidateUpdateDeclaratively(ctx, tc.options, scheme, tc.object, tc.oldObject) } - matcher := fieldtesting.ErrorMatcher{}.ByType().ByField().ByOrigin() + matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin() matcher.Test(t, tc.expected, results) }) }