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

Fix #71 Do signature change error msg #395

Merged
merged 1 commit into from
Feb 28, 2020
Merged

Fix #71 Do signature change error msg #395

merged 1 commit into from
Feb 28, 2020

Conversation

cvgw
Copy link
Collaborator

@cvgw cvgw commented Feb 2, 2020

Fixes #71

Description

Update the error handling for Call.Do and Call.DoAndReturn in the case where
the argument passed does not match expectations.

  • panic if the argument is not a function
  • panic if the number of input arguments do not match those expected by Call
  • panic if the types of the input arguments do not match those expected
    by Call

Call.DoAndReturn has additional validations on the return signature

  • panic if the number of return arguments do not match those expected by
    Call
  • panic if the types of return arguments do not match those expected by
    Call

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

  • Includes tests

Reviewer Notes

  • The code flow looks good.
  • Tests added.

Release Notes

Describe any changes here so maintainer can include it in the release notes, or delete this block.

- Update validations for Do + DoAndReturn; now panics when argument passed to Do is not a function or does not match the signature of the mocked function

@cvgw cvgw force-pushed the u/cvgw/fix-issue-71 branch 5 times, most recently from 3c6dd7e to 4fc27f1 Compare February 2, 2020 21:24
@cvgw cvgw requested review from codyoss and minicuts February 2, 2020 21:28
@cvgw
Copy link
Collaborator Author

cvgw commented Feb 2, 2020

Open question about this PR:
Should we verify the return args? Should we verify them for both Do and DoAndReturn?

Note: This PR verifies the return args for both Do and DoAndReturn

@cvgw cvgw force-pushed the u/cvgw/fix-issue-71 branch from 4fc27f1 to 7d8f57b Compare February 2, 2020 22:15
@@ -1,6 +1,7 @@
package gomock

import (
"reflect"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the copyright to this file. Eventually we need to go and get all the files caught up.

@cvgw
Copy link
Collaborator Author

cvgw commented Feb 6, 2020

I did some brief testing and found that gomock currently allows arguments to Do which do not match the return signature of the mocked method. I don't think we should start validating that as it will be a break change so I'm going to revert that.

However, gomock raises an obscure error when arguments to DoAndReturn do not match the return signature of the mocked method. So I will keep the additional validation there.

@cvgw cvgw force-pushed the u/cvgw/fix-issue-71 branch 2 times, most recently from 0501097 to 5aae07b Compare February 6, 2020 17:14
@cvgw cvgw requested a review from minicuts February 6, 2020 17:17
@cvgw cvgw force-pushed the u/cvgw/fix-issue-71 branch 2 times, most recently from 67f48ca to dbb6401 Compare February 28, 2020 00:34
@cvgw cvgw requested a review from codyoss February 28, 2020 00:34
panic(fmt.Sprintf("DoAndReturn: %s", err))
}
default:
panic("DoAndReturn: argument must be a function")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are now explicitly panicing I think we should update the doc string.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}

func interfaceArg(actualArg, expectedArg reflect.Type) error {
if !actualArg.ConvertibleTo(expectedArg) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honest question: Does this cover the implements?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does, if expectedArg is something like Stringer and actualArg is an instance of some type Foo which implements Stringer this line will evaluate to true.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added another test case to make that more clear

Update the error handling for Call.Do and Call.DoAndReturn in the case where
the argument passed does not match expectations.

* panic if the argument is not a function
* panic if the number of input arguments do not match those expected by Call
* panic if the types of the input arguments do not match those expected
by Call

Call.DoAndReturn has additional validations on the return signature

* panic if the number of return arguments do not match those expected by
Call
* panic if the types of return arguments do not match those expected by
Call
@cvgw cvgw force-pushed the u/cvgw/fix-issue-71 branch from dbb6401 to e3d5d28 Compare February 28, 2020 17:05
@cvgw cvgw requested a review from codyoss February 28, 2020 17:09
@codyoss
Copy link
Member

codyoss commented Feb 28, 2020

I was just reading over this issue. Made me question if we should be using panics at all. #158 (comment)

This PR is just making panics in this case more explicit. I wonder in the future if we should recover all of them and t.Fatal with the TestReporter we should have access to in the controller.

@cvgw
Copy link
Collaborator Author

cvgw commented Feb 28, 2020

I was just reading over this issue. Made me question if we should be using panics at all. #158 (comment)

This PR is just making panics in this case more explicit. I wonder in the future if we should recover all of them and t.Fatal with the TestReporter we should have access to in the controller.

I think that is totally valid but brings up back to the question of what to do for code executing outside of the main goroutine where we can't call t.Fatal. We should brainstorm about it some more.

@cvgw cvgw merged commit f9b4ad1 into master Feb 28, 2020
@cvgw cvgw deleted the u/cvgw/fix-issue-71 branch February 28, 2020 23:08
@horejsek
Copy link

BTW this change was released as 1.4.2, changing only minor version. It breaks the current code. I think it should be released at least as 1.5.0.

@Linus-Boehm
Copy link

Linus-Boehm commented Mar 16, 2020

Can confirm that this is a breaking change. Gomock now interprets basic types wrong and complains that it wants an interface.

@codyoss
Copy link
Member

codyoss commented Mar 16, 2020

Thank you for bring this up, that behavior was not intentional. Can you please provide a small example of code this breaks so we can add a better test.

@horejsek
Copy link

mock.EXPECT().Foo(gomock.Any()).DoAndReturn(func(...interface{}) error {return nil})

We didn't care about parameters so we left ...interface{} there which was OK and now it's panicing. The change was easy thought, just use real type instead.

@codyoss
Copy link
Member

codyoss commented Mar 16, 2020

@Linus-Boehm was this a similar use case for you. I will be reverting for now and creating a new tag 1.4.3 until I have more time to evaluate how we want to move forward this this.

codyoss added a commit that referenced this pull request Mar 16, 2020
@codyoss
Copy link
Member

codyoss commented Mar 16, 2020

New release with the change reverted: v1.4.3

@Linus-Boehm
Copy link

mock.EXPECT().Foo("some-expected-string").DoAndReturn(func(name interface{}) error {return nil})

It panics becuase "some-expected-string" is not an interface, so yeah that should be the same reason.

@cvgw
Copy link
Collaborator Author

cvgw commented Mar 27, 2020

mock.EXPECT().Foo("some-expected-string").DoAndReturn(func(name interface{}) error {return nil})

It panics becuase "some-expected-string" is not an interface, so yeah that should be the same reason.

@Linus-Boehm for clarity, what is the signature of method Foo on the type being mocked?

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

Successfully merging this pull request may close these issues.

Hard to debug when a Do(fn) function no longer matches its mock signature
5 participants