vektra / mockery

A mock code autogenerator for Go
https://vektra.github.io/mockery/
BSD 3-Clause "New" or "Revised" License
6.09k stars 409 forks source link

Generating all mocks into one file #564

Closed yurii-vyrovyi closed 1 year ago

yurii-vyrovyi commented 1 year ago

Description

I want to generate all mocks into one file to avoid garbaging a folder. I have a lot of interfaces in my package and I have a unit tests in the same package. So, if I generate mocks into sub-package I get a cyclic dependency as mypackage refers to mocks while mocks imports mypackage due to data types.

├── mypackage
│   ├── worker.go
│   ├── worker_test.go
│   ├── mocks
│   │   ├── mock_repository_one.go
│   │   ├── mock_repository_two.go
│   │   └── mock_repository_three.go

worker.go

package mypackage
import .../mypackage/mocks

mock_repository_one.go

package mocks
import .../mypackage

Of course I can move all interfaces definitions to some other folder but I prefer not to change a project structure only because of mocks. Another solution is to place mocks into mypackage. Then I have

├── mypackage
│   ├── worker.go
│   ├── worker_test.go
│   ├── mock_repository_one.go
│   ├── mock_repository_two.go
│   └── mock_repository_three.go

That's better from imports prospective but the more interfaces I have the more garbaged my package will be.

Proposal

Can we have a config that will allow to generate all mocks into one file?

LandonTClipp commented 1 year ago

I'm open to the idea. I think generating mocks in-package is generally the safer way to do this because of the cyclic dependency issue you mentioned. If we add this feature, I'd like it to be part of the packages feature first but then also backport it if needed.

yurii-vyrovyi commented 1 year ago

Sounds great. As for me this will be a point when I want to switch to mockery from mockgen. I really like strong typization in mockrey but we need to keep code clean. I hope it will be soon ;)

LandonTClipp commented 1 year ago

I can't give a good estimate on when this would be done, we need to stabilize the packages feature first. Adding this functionality is kind of non-trivial, but I do see the benefit in it.

LandonTClipp commented 1 year ago

Closing this as a duplicate of https://github.com/vektra/mockery/issues/322