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

Not generating mocks when the package only consists of generated files #679

Closed
1 of 5 tasks
amirsalarsafaei opened this issue Aug 1, 2023 · 3 comments
Closed
1 of 5 tasks

Comments

@amirsalarsafaei
Copy link

amirsalarsafaei commented Aug 1, 2023

Description

When there is a package only consisting of generated codes the mockery won't generate mocks for the interfaces. For example, we use SQLC which is a code generator for SQL queries when the package specified for generated querier has only SQLC-generated files the mockery skips the package and doesn't create any mocks.

Mockery Version

v2.32.2
(I tried v2.30.2 too)

Golang Version

go 1.20

Installation Method

  • Binary Distribution
  • Docker
  • brew
  • go install
  • Other: [specify]

Mockery Config

with-expecter: True
packages:
  path/to/your/project:
    config:
      all: True
      recursive: True
      filename: "mock_{{.InterfaceNameLower}}.go"
      dir: "{{.InterfaceDir}}"
      mockname: "{{.Mock}}{{.InterfaceName}}"
      outpkg: "{{.PackageName}}"
      inpackage: True

Steps to Reproduce

  1. In your go project create a new package let's call it somepackage
  2. In the package create a new go file called somefile.go
  3. In the file paste a code like below
  4. now run mockery
// Code generated by someGenerator . DO NOT EDIT.

package somepackage

type MyRandomInterface interface {
	SomeRandomFunc()
}

Expected Behavior

a file named mock_myrandominterface.go to be created

Actual Behavior

nothing happens

But If you create a file that isn't a generated code and put it beside the first file even if its empty(only contains package name) the mocks will be created. I think this has something to do with mocks packages and there is an condition not the check packages with only generated files.

@LandonTClipp
Copy link
Contributor

This unfortunately is a limitation of the go-provided package parser. The docs note this here: https://vektra.github.io/mockery/latest/notes/#error-no-go-files-found-in-root-search-path

It requires you to have at least one non-test go file. If you don't have that, packages.Load assumes there is nothing that you could be importing from that package, and as an optimization I guess it simply doesn't return any metadata about the package. The solution is to create one non-test go file so it induces it to download.

I've tried many ways to fix this but it doesn't seem possible. I'm open to ideas.

@amirsalarsafaei
Copy link
Author

@LandonTClipp It's not a problem with test files. I think I may have explained it poorly. It's a problem with generated files.
I changed the description and clarified the problem.

@LandonTClipp
Copy link
Contributor

Okay I understand your problem now. This is also mentioned here: #681, an issue with protobuf clients. We definitely need to fix this. I'll close this issue as a duplicate and we can track the linked issue.

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

No branches or pull requests

2 participants