[VOL-5486] Fix deprecated versions
Change-Id: Ia8cf5de26cc045c8519da848cd4314459a331e16
Signed-off-by: Abhay Kumar <abhay.kumar@radisys.com>
diff --git a/vendor/github.com/golang/mock/gomock/controller.go b/vendor/github.com/golang/mock/gomock/controller.go
index d7c3c65..f054200 100644
--- a/vendor/github.com/golang/mock/gomock/controller.go
+++ b/vendor/github.com/golang/mock/gomock/controller.go
@@ -50,9 +50,6 @@
// mockObj.EXPECT().SomeMethod(2, "second"),
// mockObj.EXPECT().SomeMethod(3, "third"),
// )
-//
-// TODO:
-// - Handle different argument/return types (e.g. ..., chan, map, interface).
package gomock
import (
@@ -77,6 +74,15 @@
Helper()
}
+// cleanuper is used to check if TestHelper also has the `Cleanup` method. A
+// common pattern is to pass in a `*testing.T` to
+// `NewController(t TestReporter)`. In Go 1.14+, `*testing.T` has a cleanup
+// method. This can be utilized to call `Finish()` so the caller of this library
+// does not have to.
+type cleanuper interface {
+ Cleanup(func())
+}
+
// A Controller represents the top-level control of a mock ecosystem. It
// defines the scope and lifetime of mock objects, as well as their
// expectations. It is safe to call Controller's methods from multiple
@@ -115,29 +121,43 @@
// NewController returns a new Controller. It is the preferred way to create a
// Controller.
+//
+// New in go1.14+, if you are passing a *testing.T into this function you no
+// longer need to call ctrl.Finish() in your test methods.
func NewController(t TestReporter) *Controller {
h, ok := t.(TestHelper)
if !ok {
- h = nopTestHelper{t}
+ h = &nopTestHelper{t}
}
-
- return &Controller{
+ ctrl := &Controller{
T: h,
expectedCalls: newCallSet(),
}
+ if c, ok := isCleanuper(ctrl.T); ok {
+ c.Cleanup(func() {
+ ctrl.T.Helper()
+ ctrl.finish(true, nil)
+ })
+ }
+
+ return ctrl
}
type cancelReporter struct {
- TestHelper
+ t TestHelper
cancel func()
}
func (r *cancelReporter) Errorf(format string, args ...interface{}) {
- r.TestHelper.Errorf(format, args...)
+ r.t.Errorf(format, args...)
}
func (r *cancelReporter) Fatalf(format string, args ...interface{}) {
defer r.cancel()
- r.TestHelper.Fatalf(format, args...)
+ r.t.Fatalf(format, args...)
+}
+
+func (r *cancelReporter) Helper() {
+ r.t.Helper()
}
// WithContext returns a new Controller and a Context, which is cancelled on any
@@ -145,15 +165,22 @@
func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context) {
h, ok := t.(TestHelper)
if !ok {
- h = nopTestHelper{t}
+ h = &nopTestHelper{t: t}
}
ctx, cancel := context.WithCancel(ctx)
- return NewController(&cancelReporter{h, cancel}), ctx
+ return NewController(&cancelReporter{t: h, cancel: cancel}), ctx
}
type nopTestHelper struct {
- TestReporter
+ t TestReporter
+}
+
+func (h *nopTestHelper) Errorf(format string, args ...interface{}) {
+ h.t.Errorf(format, args...)
+}
+func (h *nopTestHelper) Fatalf(format string, args ...interface{}) {
+ h.t.Fatalf(format, args...)
}
func (h nopTestHelper) Helper() {}
@@ -197,7 +224,10 @@
expected, err := ctrl.expectedCalls.FindMatch(receiver, method, args)
if err != nil {
- origin := callerInfo(2)
+ // callerInfo's skip should be updated if the number of calls between the user's test
+ // and this line changes, i.e. this code is wrapped in another anonymous function.
+ // 0 is us, 1 is controller.Call(), 2 is the generated mock, and 3 is the user's test.
+ origin := callerInfo(3)
ctrl.T.Fatalf("Unexpected call to %T.%v(%v) at %s because: %s", receiver, method, args, origin, err)
}
@@ -229,21 +259,33 @@
// Finish checks to see if all the methods that were expected to be called
// were called. It should be invoked for each Controller. It is not idempotent
// and therefore can only be invoked once.
+//
+// New in go1.14+, if you are passing a *testing.T into NewController function you no
+// longer need to call ctrl.Finish() in your test methods.
func (ctrl *Controller) Finish() {
+ // If we're currently panicking, probably because this is a deferred call.
+ // This must be recovered in the deferred function.
+ err := recover()
+ ctrl.finish(false, err)
+}
+
+func (ctrl *Controller) finish(cleanup bool, panicErr interface{}) {
ctrl.T.Helper()
ctrl.mu.Lock()
defer ctrl.mu.Unlock()
if ctrl.finished {
- ctrl.T.Fatalf("Controller.Finish was called more than once. It has to be called exactly once.")
+ if _, ok := isCleanuper(ctrl.T); !ok {
+ ctrl.T.Fatalf("Controller.Finish was called more than once. It has to be called exactly once.")
+ }
+ return
}
ctrl.finished = true
- // If we're currently panicking, probably because this is a deferred call,
- // pass through the panic.
- if err := recover(); err != nil {
- panic(err)
+ // Short-circuit, pass through the panic.
+ if panicErr != nil {
+ panic(panicErr)
}
// Check that all remaining expected calls are satisfied.
@@ -252,13 +294,43 @@
ctrl.T.Errorf("missing call(s) to %v", call)
}
if len(failures) != 0 {
- ctrl.T.Fatalf("aborting test due to missing call(s)")
+ if !cleanup {
+ ctrl.T.Fatalf("aborting test due to missing call(s)")
+ return
+ }
+ ctrl.T.Errorf("aborting test due to missing call(s)")
}
}
+// callerInfo returns the file:line of the call site. skip is the number
+// of stack frames to skip when reporting. 0 is callerInfo's call site.
func callerInfo(skip int) string {
if _, file, line, ok := runtime.Caller(skip + 1); ok {
return fmt.Sprintf("%s:%d", file, line)
}
return "unknown file"
}
+
+// isCleanuper checks it if t's base TestReporter has a Cleanup method.
+func isCleanuper(t TestReporter) (cleanuper, bool) {
+ tr := unwrapTestReporter(t)
+ c, ok := tr.(cleanuper)
+ return c, ok
+}
+
+// unwrapTestReporter unwraps TestReporter to the base implementation.
+func unwrapTestReporter(t TestReporter) TestReporter {
+ tr := t
+ switch nt := t.(type) {
+ case *cancelReporter:
+ tr = nt.t
+ if h, check := tr.(*nopTestHelper); check {
+ tr = h.t
+ }
+ case *nopTestHelper:
+ tr = nt.t
+ default:
+ // not wrapped
+ }
+ return tr
+}