counterfeiter icon indicating copy to clipboard operation
counterfeiter copied to clipboard

Interface fakes generated in the same package as the interface cause an import cycle

Open alexkohler opened this issue 6 years ago • 5 comments

Suppose you have the following interface:

package abc
//go:generate $GOPATH/src/github.com/maxbrunsfeld/counterfeiter/counterfeiter -o FakeFoo.go . Foo

type Foo interface {
	Bar() error
}

After generating fakes, the output fake includes the following line to ensure FakeFoo is meeting the abc.Foo interface:

var _ abc.Foo = new(FakeFoo)

Note that we output this fake to the abc package, so the abc. identifier in front of Foo is not necessary, and actually causes an import cycle.

alexkohler avatar Dec 05 '18 19:12 alexkohler

I guess this is a duplicate of https://github.com/maxbrunsfeld/counterfeiter/issues/68.

alexkohler avatar Dec 06 '18 11:12 alexkohler

The problem is not limited to just the interface check. If the interface uses any types defined in the same package for args or returns those types will cause the same problem.

I think the arguments against this in #68 are questionable. White box testing can be just as important as black box testing. In fact this project modifies unexported fields in its tests in many places.

@alexkohler did you find a way to work around the problem? Would you be interested in re-opening this issue or should I open a new one.

dnephin avatar Dec 26 '18 19:12 dnephin

@dnephin I did not find a workaround. I was planning on forking and patching, but I'll gladly reopen this for further discussion.

alexkohler avatar Dec 27 '18 01:12 alexkohler

Up! Anything being done in this respect ?

LasTshaMAN avatar Jun 09 '19 07:06 LasTshaMAN

This basically makes this tool unusable.. quite sad, i liked the style

jgroeneveld avatar Jan 25 '20 12:01 jgroeneveld