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

Support "exclude" in packages config #708

Closed
3 of 5 tasks
kpaulisse opened this issue Sep 21, 2023 · 4 comments · Fixed by #709
Closed
3 of 5 tasks

Support "exclude" in packages config #708

kpaulisse opened this issue Sep 21, 2023 · 4 comments · Fixed by #709

Comments

@kpaulisse
Copy link
Contributor

Description

The "exclude" parameter is not recognized when the packages configuration is used. Including this parameter in the configuration has no effect.

Mockery Version

v2.33.2 and latest master

Golang Version

1.20.8

Installation Method

  • Binary Distribution
  • Docker
  • brew
  • go install
  • Other: Compiling from source

Steps to Reproduce

We have a package in our code base that doesn't have any interfaces but consistently fails with errors like this:

21 Sep 23 15:03 CDT ERR encountered error when loading package error="/Users/xxxxxxxx/go/src/github.com/organization/reponame/blah/blah/clients/common/common_test_utils.go:4:10: could not import github.com/organization/reponame/blah/blah/mocks/clients/foo (invalid package name: \"\")" dry-run=false version=v0.0.0-dev

The common_test_utils.go file featured in that error message a test helper that contains code referencing mocks like this:

package clients

import (
	mockFoo "github.com/organization/reponame/blah/blah/mocks/clients/foo"
)

I want to use this configuration in my .mockery.yaml file, but there's no code in config.go that recognizes the values in the "Exclude" field:

---
with-expecter: False
filename: "{{.InterfaceName}}.go"
mockname: "{{.InterfaceName}}"
outpkg: "mocks"
packages:
  github.com/organization/reponame/blah/blah/clients:
    config:
      all: true
      dir: mocks/{{ .InterfaceDirRelative }}
      recursive: true
      exclude:
        - common

Expected Behavior

It should not descend into the "common" directory and therefore I should not get an error when it fails to process the file in that directory correctly.

Actual Behavior

It descends into the "common" directory and I get the error shown above.

@tzachshabtay
Copy link

@LandonTClipp @kpaulisse is this part of mockery 2.36.0?

My "exclude" direction in the config file is ignored, not sure if I'm doing it wrong?

i.e this is my config file:

with-expecter: true
dir: "{{.InterfaceDir}}/mock" 
outpkg: "mock"
filename: "{{ .InterfaceName | snakecase }}.go"
mockname: "{{ .InterfaceName | firstUpper }}"
packages:
  org/bla/bla/package1:
    config:
      recursive: True
      exclude:
        - reinforced
      include-regex: ".*"
      exclude-regex: ".*[Oo]ption"
  org/bla/bla/package2:
    interfaces:
      OptionMessageHandler:
  org/bla/bla/package3:
    config:
      dir: "./internal/db/testutils/mock"
    interfaces:
      Tx:
      Transactor:

It still generates mocks for the reinforced packages (I also tried using the complete import name, i.e "org/bla/bla/package1/whatever/reinforced", that didn't work either).

@LandonTClipp
Copy link
Collaborator

LandonTClipp commented Oct 17, 2023

@tzachshabtay I believe you need to specify a path that's relative to org/bla/bla/package1. So if the absolute path of the thing you're trying to exclude is org/bla/bla/package1/whatever/reinforced then you'll want

exclude: 
- whatever/reinforced

If this is indeed the case, I need to update the docs to make this more clear.

@tzachshabtay
Copy link

@LandonTClipp ah thanks, relative path works.

As an aside, what I really would have liked here is an exclude regex, this would have allowed me to exclude .*reinforced, and not to have to write a line for each reinforced package.

I would have renamed the current exclude-regex to exclude-interface-regex (and include-interface-regex) and have this one as exclude-package-regex.

@LandonTClipp
Copy link
Collaborator

You are right, I am kicking myself that I did not name this better. The reason this happened was because exclude was a parameter in the legacy config and someone sent in a pr to port this to the new config. I did not think clearly enough to prevent this unclear naming, that is my fault.

That doesn't mean we couldn't also add an exclude-package-regex parameter. To make this better for v3 I'm also totally fine with adding exclude-package that does the same thing as exclude, and then deprecate the usage of exclude.

If anyone wants to submit an issue to discuss a proposal for any of these ideas, I am likely to accept it.

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

Successfully merging a pull request may close this issue.

3 participants