petergtz / pegomock

Pegomock is a powerful, yet simple mocking framework for the Go programming language
Apache License 2.0
252 stars 28 forks source link

Incorrect type name generated in filemode with multiple interfaces #103

Closed anthonyraymond closed 4 years ago

anthonyraymond commented 4 years ago

This bug happens when:

What happens:

The generated mocks are duplicated, and all points to the first interface type.

Example

bugmultiname.go

package pkg

type MyFirstInterface interface {
    doSomethingCool()
}

type MySecondInterface interface {
    FirstStoleMyName()
}

bugmultiname_test.go

package pkg

import "testing"

//go:generate pegomock generate --use-experimental-model-gen bugmultiname.go

func TestWhatever(t *testing.T) {

}

when generating the mocks (no matter if use-experimental-model-gen or not) this is the result:

// Code generated by pegomock. DO NOT EDIT.
// Source: bugmultiname.go

package pkg_test

import (
    pegomock "github.com/petergtz/pegomock"
    "reflect"
    "time"
)

type MockMyFirstInterface struct {
    fail func(message string, callerSkip ...int)
}

func NewMockMyFirstInterface(options ...pegomock.Option) *MockMyFirstInterface {
    mock := &MockMyFirstInterface{}
    for _, option := range options {
        option.Apply(mock)
    }
    return mock
}

func (mock *MockMyFirstInterface) SetFailHandler(fh pegomock.FailHandler) { mock.fail = fh }
func (mock *MockMyFirstInterface) FailHandler() pegomock.FailHandler      { return mock.fail }

func (mock *MockMyFirstInterface) doSomethingCool() {
    if mock == nil {
        panic("mock must not be nil. Use myMock := NewMockMyFirstInterface().")
    }
    params := []pegomock.Param{}
    pegomock.GetGenericMockFrom(mock).Invoke("doSomethingCool", params, []reflect.Type{})
}

...

type MockMyFirstInterface struct {
    fail func(message string, callerSkip ...int)
}

func NewMockMyFirstInterface(options ...pegomock.Option) *MockMyFirstInterface {
    mock := &MockMyFirstInterface{}
    for _, option := range options {
        option.Apply(mock)
    }
    return mock
}

func (mock *MockMyFirstInterface) SetFailHandler(fh pegomock.FailHandler) { mock.fail = fh }
func (mock *MockMyFirstInterface) FailHandler() pegomock.FailHandler      { return mock.fail }

func (mock *MockMyFirstInterface) FirstStoleMyName() {
    if mock == nil {
        panic("mock must not be nil. Use myMock := NewMockMyFirstInterface().")
    }
    params := []pegomock.Param{}
    pegomock.GetGenericMockFrom(mock).Invoke("FirstStoleMyName", params, []reflect.Type{})
}

...

As you can see there are not two distinct interfaces but only one MyFirstInterface. If you look closely however you'll see that the first generated mock has the MyFirstInterface methods and the second has the MySecondInterface methods. The only wrong thing is actually the name of the type.



Go: 1.14.1

anthonyraymond commented 4 years ago

I'e dig a bit in the source code and the problem comes from mockgen\mockgen.go => opening a PR