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

v3: Allow mocks from different source packages in the same file #888

Open
LandonTClipp opened this issue Jan 6, 2025 · 1 comment
Open
Labels

Comments

@LandonTClipp
Copy link
Collaborator

I currently do not allow this because mockery attempts to automatically determine if the output mock file is in the same package as the originating interface. I initially attempted to allow multiple packages in the output mock but was running into issues with this "is inpackage" determination, so I decided to defer this until later.

This is not theoretically impossible, it just requires extracting this logic out of the template generator constructor and into the logic that populates imports for each method.

@LandonTClipp LandonTClipp added the v3 label Jan 6, 2025
@LandonTClipp
Copy link
Collaborator Author

I've decided that we probably don't want to support this without changes to the config schema. The main issue arises when mockery needs to determine which package-level config to use for the top-level attributes such as pkgname, dir, filename etc. Which package-level config should it use? Well, we could enforce that for all of these top-level configs, mockery asserts the values are the same across all packages. This seems gross and brittle.

Instead, I think we should introduce an alternate config that looks something like:

files:
  path/to/mocks.go:
    config:
      foo: bar
    packages:
      github.com/vektra/mockery/v2:
        interfaces:
          [...]

This model is more explicit on how config merging is done and it's clearer where the top-level attributes come from.

It makes sense for us to support this in parallel to the typical packages: config scheme because most people utilize the templating system to dynamically specify where mock files should live, e.g. dir: "{{ .InterfaceDir }}". The files: schema requires you to explicitly specify where the mock lives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant