vektra / mockery

A mock code autogenerator for Go
https://vektra.github.io/mockery/
BSD 3-Clause "New" or "Revised" License
5.8k stars 395 forks source link

Variadic function issues. #766

Closed shortsyoungster closed 2 months ago

shortsyoungster commented 3 months ago

PLEASE READ

DO NOT submit tickets without first using the latest version of Golang, clearing your local golang package cache, and re-building mockery using the latest Golang version and the latest version of mockery. Please provide evidence this has been done in your issue. Failure to provide this evidence will likely result in your issue being closed.

Description

I am getting this error from mockery for my interface that uses variadic functions. I get it even after setting the flag --unroll-variadic=False. ERR Interface conversion: types.Type is *types.Named, not *types.Slice dry-run=false interface=Name qualified-name=FilePath version=v2.42.0. Deleting the interface methods that use the variadic input fixes the issue.

Mockery Version

2.42.0 Golang Version

1.22

NOTE: Please upgrade to the latest golang version before submitting tickets!

Installation Method

Steps to Reproduce

  1. [First Step]
  2. [Second Step]
  3. [etc]

Expected Behavior

Mock is generated.

Actual Behavior

Blank mock, and the following error: ERR Interface conversion: types.Type is *types.Named, not *types.Slice dry-run=false interface=Name qualified-name=FilePath version=v2.42.0

LandonTClipp commented 3 months ago

Hello, please provide a reproducer. Without this, I cannot provide any help.

shortsyoungster commented 3 months ago
type MyNewInterface interface {
    FetchData(
        ctx context.Context,
        dataType reflect.Type,
        fetchFunc func(xxx ...yyy.ZZ) ([]aa.BB, error),
    ) ([]aa.BB, error)
}

This worked on Mockery v2.20.2

shortsyoungster commented 3 months ago

I would presume that it has to do with this commit https://github.com/vektra/mockery/commit/d119c66e267c739ff3dc9b0d1d55e5b8090f4e9c

shortsyoungster commented 3 months ago

I have tested on v2.40.0, and everything works, this is the version right before https://github.com/vektra/mockery/commit/d119c66e267c739ff3dc9b0d1d55e5b8090f4e9c so I am fairly confident that it is the cause.

LandonTClipp commented 3 months ago

I think the thing to do here is add your example interface as a fixture and have mockery generate it within this project (mainly for testing/backwards compat reasons), then figure out precisely why this fails. Variadic argument have been the bane of my existence in this project, so I'm guessing we'll need yet another special exception for whatever is going wrong here.

gurudesu commented 2 months ago

I'm experiencing the same issues here - especially with trying to mock the AWS SDK Go V2 interfaces. As an example, see this struct.

Not a bash on this project (thank you for supporting this) but for the time being we've been pushing folks to use gomock instead since they seem to handle variadic arguments well.

LandonTClipp commented 2 months ago

I'll do what I can to fix this, if anyone would be able to submit a PR that would be great, I probably can't get to it for the next few weeks due to being busy at work.

I hope to not drive people away to the main competition for this project, that irks me :)

LandonTClipp commented 2 months ago

@shortsyoungster @gurudesu I found the issue and submitted a PR to fix it. If the tests pass I will merge and tag the fix.

Sorry for the delay in getting to this. @gurudesu after you told me you were telling people to use gomock I knew I wouldn't be able to sleep until my competition is crushed, so I hope this fixes your issue 😅

This bug actually had nothing to do with the variadic-ness of the function (although I did find a separate bug that I don't think was part of the original complaint here). It was failing on the return value of the function argument. Details in the linked PR.

PS I guess a way to get me to do stuff is to just say gomock does something better. That should never be the case in my opinion!

gurudesu commented 2 months ago

Thanks @LandonTClipp! Will try this out and recommend folks in my org to just upgrade 👍

shortsyoungster commented 2 months ago

This worked for me @LandonTClipp. Thank you so much!