Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

add default calling of ctrl.Finish() in go1.14+ #422

Merged
merged 2 commits into from
Apr 6, 2020
Merged

add default calling of ctrl.Finish() in go1.14+ #422

merged 2 commits into from
Apr 6, 2020

Conversation

codyoss
Copy link
Member

@codyoss codyoss commented Mar 30, 2020

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

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) {
Copy link
Member Author

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(...)`")
Copy link
Member Author

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?

Copy link
Collaborator

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) {
Copy link
Member Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use t.Cleanup (added in go1.14) to call Finish automatically
2 participants