-
Notifications
You must be signed in to change notification settings - Fork 607
add default calling of ctrl.Finish() in go1.14+ #422
Conversation
In go1.14 the testing package added a new cleanup method that is called at the end of test runs. By taping into this gomock can remove the need for users to call ctrl.Finish() if they pass a *testing.T into gomock.NewController(...). Fixes #407
ctrl.Call(subject, "FooMethod", "argument") | ||
} | ||
|
||
func TestOrderedCallsInCorrect(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With a couple of these tests I had to get a little fancy. Their implementation relied on Finish not being called to verify things. Cleanup acts in a LIFO matter. By registering a cleanup before the one in NewController, I can assert things after Finish() has been called.
// provide a log message to guide users to remove `defer ctrl.Finish()` in Go 1.14+ | ||
tr := unwrapTestReporter(ctrl.T) | ||
if l, ok := tr.(interface{ Log(args ...interface{}) }); ok { | ||
l.Log("In Go 1.14+ you no longer need to `ctrl.Finish()` if a *testing.T is passed to `NewController(...)`") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't decide if this is a good or bad idea. Do we let users know about this new behavior? I think this only shows up with -v
. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a good idea. I think you're correct that it would only show up if -v
is passed
|
||
import "testing" | ||
|
||
func TestDuplicateFinishCallFails(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the previous behavior, lets keep things the same for versions less than 1.14
In go1.14 the testing package added a new cleanup method that is
called at the end of test runs. By taping into this gomock can
remove the need for users to call ctrl.Finish() if they pass a
*testing.T into gomock.NewController(...).
Fixes #407