Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating mocks for *_test package interfaces #88

Open
jpopadak opened this issue May 24, 2019 · 5 comments
Open

Allow creating mocks for *_test package interfaces #88

jpopadak opened this issue May 24, 2019 · 5 comments

Comments

@jpopadak
Copy link
Contributor

jpopadak commented May 24, 2019

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/[email protected]+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/[email protected]+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/[email protected]+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/[email protected]+incompatible/pegomock/main.go:116 +0x2359
main.main()
	/Users/james.popadak/go/pkg/mod/github.com/petergtz/[email protected]+incompatible/pegomock/main.go:37 +0xa8
auth_suite_test.go:9: running "pegomock": exit status 2

Process finished with exit code 1
@petergtz
Copy link
Owner

@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
Copy link
Contributor Author

jpopadak commented Jun 2, 2019

@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
Copy link
Contributor Author

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

@jpopadak jpopadak closed this as completed Nov 7, 2019
@petergtz
Copy link
Owner

petergtz commented Nov 7, 2019

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
Copy link
Contributor Author

jpopadak commented Nov 8, 2019

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

@jpopadak jpopadak reopened this Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants