golang / mock

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

Mockgen emits bad code on empty interface (imported and not used: "reflect") #102

Closed SpikesDivZero closed 6 years ago

SpikesDivZero commented 7 years ago

When running against an empty interface, mockgen is writing out Go files that import the reflect library but don't actually use it causing an unexpected error during test execution:

imported and not used: "reflect"

A rough example input file is:

package tmp

//go:generate mockgen -pkg mocks -destination=../mockgen_tmp.go PACKAGE/tmp EmptyInterfacer

type EmptyInterfacer interface {
    // TODO
}

And all matching instances of reflect in the generated file:

mocks$ grep -C2 reflect mockgen_tmp.go
import (
    gomock "github.com/golang/mock/gomock"
    reflect "reflect"
)

mocks$ 

Adding any placeholder method to this interface makes the error go away.

And thanks for your work on this. It's a nice tool ^_^

bamdadd commented 6 years ago

I'm having this issue as well. If this gets fixed it helps with TDD process so that I can start with an empty interface and then add methods to the interface when needed.