Skip to content

Commit cd9df2f

Browse files
committed
chore: change error_matcher.go to use test interface instead of importing testing pkg
1 parent 8266787 commit cd9df2f

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

pkg/apis/core/validation/validation_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"k8s.io/apimachinery/pkg/util/sets"
3939
"k8s.io/apimachinery/pkg/util/validation"
4040
"k8s.io/apimachinery/pkg/util/validation/field"
41-
fldtest "k8s.io/apimachinery/pkg/util/validation/field/testing"
4241
"k8s.io/apimachinery/pkg/util/version"
4342
utilfeature "k8s.io/apiserver/pkg/util/feature"
4443
"k8s.io/component-base/featuregate"
@@ -16714,7 +16713,7 @@ func TestValidateReplicationControllerUpdate(t *testing.T) {
1671416713
tc.old.ObjectMeta.ResourceVersion = "1"
1671516714
tc.update.ObjectMeta.ResourceVersion = "1"
1671616715
errs := ValidateReplicationControllerUpdate(&tc.update, &tc.old, PodValidationOptions{})
16717-
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring()
16716+
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring()
1671816717
matcher.Test(t, tc.expectedErrs, errs)
1671916718
})
1672016719
}
@@ -16864,7 +16863,7 @@ func TestValidateReplicationController(t *testing.T) {
1686416863
for k, tc := range errorCases {
1686516864
t.Run(k, func(t *testing.T) {
1686616865
errs := ValidateReplicationController(&tc.input, PodValidationOptions{})
16867-
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring()
16866+
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring()
1686816867
matcher.Test(t, tc.expectedErrs, errs)
1686916868
})
1687016869
}
@@ -20770,7 +20769,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2077020769
t.Run(k, func(t *testing.T) {
2077120770
errs := ValidateEndpointsCreate(&v.endpoints)
2077220771
// TODO: set .RequireOriginWhenInvalid() once metadata is done
20773-
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin()
20772+
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
2077420773
matcher.Test(t, v.expectedErrs, errs)
2077520774
})
2077620775
}
@@ -22767,7 +22766,7 @@ func TestValidateTopologySpreadConstraints(t *testing.T) {
2276722766
for _, tc := range testCases {
2276822767
t.Run(tc.name, func(t *testing.T) {
2276922768
errs := validateTopologySpreadConstraints(tc.constraints, fieldPath, tc.opts)
22770-
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().RequireOriginWhenInvalid()
22769+
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().RequireOriginWhenInvalid()
2277122770
matcher.Test(t, tc.wantFieldErrors, errs)
2277222771
})
2277322772
}

staging/src/k8s.io/apimachinery/pkg/runtime/scheme_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3636
"k8s.io/apimachinery/pkg/util/sets"
3737
"k8s.io/apimachinery/pkg/util/validation/field"
38-
fieldtesting "k8s.io/apimachinery/pkg/util/validation/field/testing"
3938
)
4039

4140
type testConversions struct {
@@ -1089,7 +1088,7 @@ func TestRegisterValidate(t *testing.T) {
10891088
} else {
10901089
results = s.ValidateUpdate(ctx, tc.options, tc.object, tc.oldObject, tc.subresource...)
10911090
}
1092-
matcher := fieldtesting.ErrorMatcher{}.ByType().ByField().ByOrigin()
1091+
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
10931092
matcher.Test(t, tc.expected, results)
10941093
})
10951094
}
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package testing
17+
package field
1818

1919
import (
2020
"fmt"
2121
"reflect"
2222
"regexp"
2323
"strings"
24-
"testing"
25-
26-
field "k8s.io/apimachinery/pkg/util/validation/field"
2724
)
2825

29-
// ErrorMatcher is a helper for comparing field.Error objects.
26+
// ErrorMatcher is a helper for comparing Error objects.
3027
type ErrorMatcher struct {
3128
// TODO(thockin): consider whether type is ever NOT required, maybe just
3229
// assume it.
@@ -42,9 +39,9 @@ type ErrorMatcher struct {
4239
requireOriginWhenInvalid bool
4340
}
4441

45-
// Matches returns true if the two field.Error objects match according to the
42+
// Matches returns true if the two Error objects match according to the
4643
// configured criteria.
47-
func (m ErrorMatcher) Matches(want, got *field.Error) bool {
44+
func (m ErrorMatcher) Matches(want, got *Error) bool {
4845
if m.matchType && want.Type != got.Type {
4946
return false
5047
}
@@ -58,7 +55,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool {
5855
if want.Origin != got.Origin {
5956
return false
6057
}
61-
if m.requireOriginWhenInvalid && want.Type == field.ErrorTypeInvalid {
58+
if m.requireOriginWhenInvalid && want.Type == ErrorTypeInvalid {
6259
if want.Origin == "" || got.Origin == "" {
6360
return false
6461
}
@@ -72,7 +69,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool {
7269

7370
// Render returns a string representation of the specified Error object,
7471
// according to the criteria configured in the ErrorMatcher.
75-
func (m ErrorMatcher) Render(e *field.Error) string {
72+
func (m ErrorMatcher) Render(e *Error) string {
7673
buf := strings.Builder{}
7774

7875
comma := func() {
@@ -93,7 +90,7 @@ func (m ErrorMatcher) Render(e *field.Error) string {
9390
comma()
9491
buf.WriteString(fmt.Sprintf("Value=%v", e.BadValue))
9592
}
96-
if m.matchOrigin || m.requireOriginWhenInvalid && e.Type == field.ErrorTypeInvalid {
93+
if m.matchOrigin || m.requireOriginWhenInvalid && e.Type == ErrorTypeInvalid {
9794
comma()
9895
buf.WriteString(fmt.Sprintf("Origin=%q", e.Origin))
9996
}
@@ -170,17 +167,25 @@ func (m ErrorMatcher) ByDetailRegexp() ErrorMatcher {
170167
return m
171168
}
172169

170+
// TestIntf lets users pass a testing.T while not coupling this package to Go's
171+
// testing package.
172+
type TestIntf interface {
173+
Helper()
174+
Errorf(format string, args ...any)
175+
Logf(format string, args ...any)
176+
}
177+
173178
// Test compares two ErrorLists by the criteria configured in this matcher, and
174179
// fails the test if they don't match. If a given "want" error matches multiple
175180
// "got" errors, they will all be consumed. This might be OK (e.g. if there are
176181
// multiple errors on the same field from the same origin) or it might be an
177182
// insufficiently specific matcher, so these will be logged.
178-
func (m ErrorMatcher) Test(tb testing.TB, want, got field.ErrorList) {
183+
func (m ErrorMatcher) Test(tb TestIntf, want, got ErrorList) {
179184
tb.Helper()
180185

181186
remaining := got
182187
for _, w := range want {
183-
tmp := make(field.ErrorList, 0, len(remaining))
188+
tmp := make(ErrorList, 0, len(remaining))
184189
n := 0
185190
for _, g := range remaining {
186191
if m.Matches(w, g) {

staging/src/k8s.io/apiserver/pkg/registry/rest/validate_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"k8s.io/apimachinery/pkg/runtime/schema"
3030
"k8s.io/apimachinery/pkg/util/sets"
3131
"k8s.io/apimachinery/pkg/util/validation/field"
32-
fieldtesting "k8s.io/apimachinery/pkg/util/validation/field/testing"
3332
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
3433
)
3534

@@ -154,7 +153,7 @@ func TestValidateDeclaratively(t *testing.T) {
154153
} else {
155154
results = ValidateUpdateDeclaratively(ctx, tc.options, scheme, tc.object, tc.oldObject)
156155
}
157-
matcher := fieldtesting.ErrorMatcher{}.ByType().ByField().ByOrigin()
156+
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
158157
matcher.Test(t, tc.expected, results)
159158
})
160159
}

0 commit comments

Comments
 (0)