Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid code generation when interface method parameter's name is the same as interface name #648

Closed
1 of 5 tasks
kozmod opened this issue Jun 22, 2023 · 2 comments
Closed
1 of 5 tasks
Labels

Comments

@kozmod
Copy link
Contributor

kozmod commented Jun 22, 2023

Description

When generating mocks for an interface with the method's arg is other interface and args name is the same as args type:

//go:generate mockery --inpackage --all --outpkg service --testonly

type (
    interfaceA interface {
               // the name of the parameter like as interface name
        SomeMethod(interfaceB interfaceB) (int64, err error)
    }

    interfaceB interface {
        GetData() int
    }
)

the generated code:

// mockInterfaceA is an autogenerated mock type for the interfaceA type
type mockInterfaceA struct {
    mock.Mock
}

// SomeMethod provides a mock function with given fields: interfaceB
func (_m *mockInterfaceA) SomeMethod(interfaceB interfaceB) (error, error) {
    ret := _m.Called(interfaceB)

    var r0 error
    var r1 error
    if rf, ok := ret.Get(0).(func(interfaceB) (error, error)); ok { // 👈️ error
        return rf(interfaceB)
    }
    if rf, ok := ret.Get(0).(func(interfaceB) error); ok { // 👈️ error
        r0 = rf(interfaceB)
    } else {
        r0 = ret.Error(0)
    }

    if rf, ok := ret.Get(1).(func(interfaceB) error); ok { // 👈️ error
        r1 = rf(interfaceB)
    } else {
        r1 = ret.Error(1)
    }

    return r0, r1
}

Error:

./mock_interfaceA_test.go:18:32: interfaceB (variable of type interfaceB) is not a type

Mockery Version

v2.30.1

Golang Version

1.20

Installation Method

  • Binary Distribution
  • Docker
  • brew
  • go install
  • Other: [specify]

Expected Behavior

// SomeMethod provides a mock function with given fields: interfaceB
func (_m *mockInterfaceA) SomeMethod(interfaceB1 interfaceB) (error, error) {
    ret := _m.Called(interfaceB1)

    var r0 error
    var r1 error
    if rf, ok := ret.Get(0).(func(interfaceB) (error, error)); ok {
        return rf(interfaceB1)
    }
    if rf, ok := ret.Get(0).(func(interfaceB) error); ok {
        r0 = rf(interfaceB1)
    } else {
        r0 = ret.Error(0)
    }

    if rf, ok := ret.Get(1).(func(interfaceB) error); ok {
        r1 = rf(interfaceB1)
    } else {
        r1 = ret.Error(1)
    }

    return r0, r1
}

It swirl be nice to generate different the mock's arg method name when declared type name is the same.

@LandonTClipp
Copy link
Contributor

So this should be trivially easy to fix. You already know a workaround, just name your argument differently. I believe we already have logic for checking for conflicting import names that I think we could use here. PRs welcome!

kozmod added a commit to kozmod/mockery that referenced this issue Jun 25, 2023
kozmod added a commit to kozmod/mockery that referenced this issue Jun 25, 2023
kozmod added a commit to kozmod/mockery that referenced this issue Jun 25, 2023
kozmod added a commit to kozmod/mockery that referenced this issue Jun 25, 2023
kozmod added a commit to kozmod/mockery that referenced this issue Jun 27, 2023
kozmod added a commit to kozmod/mockery that referenced this issue Jun 27, 2023
@kozmod
Copy link
Contributor Author

kozmod commented Jun 27, 2023

@LandonTClipp please close the issue by yourself)

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

No branches or pull requests

2 participants