vektra / mockery

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

Feature Request: Infer Local Package During Generation #737

Open mkeeler opened 7 months ago

mkeeler commented 7 months ago

Description

I have a mockery config like:

with-expecter: true
all: true
recursive: false
dir: "{{.PackageName}}mock"
outpkg: "{{.PackageName}}mock"
mockname: "{{.InterfaceName}}"
packages:
  github.com/mkeeler/repo/path/to/sub/package:

I am placing a .mockery.yaml file into the sub-packages directory to control how that package generates mocks. It would be great if I could omit the packages and have mockery execute on the package within the same directory or if instead the package names could be templated. Something like the following would be nice:

with-expecter: true
all: true
recursive: false
dir: "{{.PackageName}}mock"
outpkg: "{{.PackageName}}mock"
mockname: "{{.InterfaceName}}"
packages:
  {{.PackageFromDir}}:

With that I could define a single configuration and run mockery within different packages to generate the mocks for that package.

I am trying to avoid having a single .mockery.yaml file for the entire repo as different parts of the repo are owned by different teams in my organization.

go list . will output the current directories corresponding Go package so I assume it should be relatively simple to get it programmatically as well.

Mockery Version

2.37.1

Golang Version

1.21

Installation Method

Steps to Reproduce

N/A

Expected Behavior

N/A

Actual Behavior

N/A

LandonTClipp commented 7 months ago

In theory, this could be done by the Config object looking for go.mod in the working directory and inferring from that. I want to make mockery more friendly to monorepo-style repos, but for now I don't see this particular feature as something that would dramatically improve quality of life.

I can leave it up for consideration. For the audience, vote up on it if you want this feature.