petergtz / pegomock

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

Allow creating mocks for *_test package interfaces #88

Open jpopadak opened 5 years ago

jpopadak commented 5 years ago

In my code base, I use many private interfaces to prevent package pollution.

I have an interface in my source code:

package auth

type tokenRetriever interface {
    Token() (string, error)
}

func SomeThing(retriever tokenRetriever) {
    // do stuff
}
package auth_test

type Cache interface {
    Token() (string, error)
}

// Test Cases Here

However, I cannot use pegomock to generate a mock for the given interface even though it is exported. I dont want to change the package my test file to be auth as it allows direct access to package members (we want to do API only testing and remove dependency on source code).

panic: Loading input failed: exit status 1 caused by:
prog.go:14:2: cannot find package "github.com/<dir>/<project>/internal/auth_test" in any of:
    /Users/james.popadak/go/src/github.com/<dir>/<project>/vendor/github.com/<dir>/<project>/internal/auth_test (vendor tree)
    /usr/local/Cellar/go/1.12.5/libexec/src/github.com/<dir>/<project>/internal/auth_test (from $GOROOT)
    /Users/james.popadak/go/src/github.com/<dir>/<project>/internal/auth_test (from $GOPATH)

goroutine 1 [running]:
github.com/petergtz/pegomock/pegomock/filehandling.GenerateMockSourceCode(0xc0000fc480, 0x2, 0x2, 0x0, 0x0, 0xc0001061a0, 0x9, 0x0, 0x0, 0xc000140000, ...)
    /Users/james.popadak/go/pkg/mod/github.com/petergtz/pegomock@v2.3.0+incompatible/pegomock/filehandling/filehandling.go:110 +0x614
github.com/petergtz/pegomock/pegomock/filehandling.GenerateMockFile(0xc0000fc480, 0x2, 0x2, 0xc000140070, 0x64, 0x0, 0x0, 0xc0001061a0, 0x9, 0x0, ...)
    /Users/james.popadak/go/pkg/mod/github.com/petergtz/pegomock@v2.3.0+incompatible/pegomock/filehandling/filehandling.go:64 +0xdd
github.com/petergtz/pegomock/pegomock/filehandling.GenerateMockFileInOutputDir(0xc0000fc480, 0x2, 0x2, 0xc000020004, 0x51, 0x0, 0x0, 0x0, 0x0, 0xc0001061a0, ...)
    /Users/james.popadak/go/pkg/mod/github.com/petergtz/pegomock@v2.3.0+incompatible/pegomock/filehandling/filehandling.go:40 +0x17f
main.Run(0xc000012100, 0x4, 0x4, 0x141d820, 0xc000010018, 0x141d800, 0xc000010010, 0xc0001320f0, 0xc000142000)
    /Users/james.popadak/go/pkg/mod/github.com/petergtz/pegomock@v2.3.0+incompatible/pegomock/main.go:116 +0x2359
main.main()
    /Users/james.popadak/go/pkg/mod/github.com/petergtz/pegomock@v2.3.0+incompatible/pegomock/main.go:37 +0xa8
auth_suite_test.go:9: running "pegomock": exit status 2

Process finished with exit code 1
petergtz commented 5 years ago

@jpopadak Trying to understand how you can call auth.SomeThing(...) from outside this package as it takes a private interface as parameter. Is this even possible? Even if it is, it seems strange.

If Something is part of your public API of that package, its parameter type TokenRetriever by definition should be public too.

All that being said, I guess the code could be changed, that it makes it possible to include _test packages too, but I'd first like to understand if this is indeed a realistic use case.

jpopadak commented 5 years ago

@petergtz This is possible. There are many locations in the Go code base that does this. You can pass in any object that satisfies the private interface. The idea is, we dont want anyone directly using that private interface directly in their code (no references) so we can rename or move it around to another package and refactor as needed.

jpopadak commented 5 years ago

I think this is a limitation of the language / reflection itself. Mainly because these are non-exported structures.

petergtz commented 4 years ago

Hey @jpopadak, sorry I never followed up on this. It was forever in my TODO box, but never got to it. My idea was not to close it until I have dived deeper into that part of the language.

Since you closed the issue, does it mean this is not a problem for you anymore or you worked around it?

jpopadak commented 4 years ago

I accidentally closed it. I must have clicked the button when reading back through it. 🤦‍♂ Sorry!