golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.33k stars 607 forks source link

[BUG] Generated mock imports its own package #274

Closed si-m closed 5 years ago

si-m commented 5 years ago

This started happening yesterday in the CI which installs a fresh version of mockgen. Then I ran go get -u github.com/golang/mock/mockgen and i could reproduced it locally.

The issue is that if i have two interfaces and one has a method that returns the other one, the generated mock file imports its own package, I doesn't realize that its in that same package.

The issue happens in source mode and reflect mode.

How to reproduce the issue

example package

package example

import "fmt"

type Foo interface {
    Foo()
}

type Bar interface {
    Bar() Foo
}

type a struct{}

type b struct{}

func (*a) Foo() {
    fmt.Println("foo")
}

func (*b) Bar() Foo {
    return &a{}
}

Create mocks for this package:

$GOPATH/bin/mockgen -package example -source=example/example.go -mock_names Foo=MockFoo,Bar=MockBar > example/example_mocks.go
koenpunt commented 5 years ago

You should use the -destination argument, instead of redirecting the output, then generating works fine:

$GOPATH/bin/mockgen -package example -source=example/example.go \
  -mock_names Foo=MockFoo,Bar=MockBar -destination=example/example_mocks.go
si-m commented 5 years ago

Okay the -destination flag fixes my problem, but it was working fine a few days ago. It would be nice to have versions.

balshetzer commented 5 years ago

We do use versions. We haven't yet released a new version. You must be following head.

On Thu, Mar 14, 2019, 12:51 PM Santiago notifications@github.com wrote:

Okay the -destination flag fixes my problem, but it was working fine a few days ago. It would be nice to have versions.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/golang/mock/issues/274#issuecomment-472959939, or mute the thread https://github.com/notifications/unsubscribe-auth/ABwtxlBaQfiSgXN0tdYnhvTgUAbnW6gvks5vWn4hgaJpZM4bs7JO .