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

Configuration option `inpackage` is not functioning as expected #777

Closed emreler closed 1 month ago

emreler commented 1 month ago

PLEASE READ

DO NOT submit tickets without first using the latest version of Golang, clearing your local golang package cache, and re-building mockery using the latest Golang version and the latest version of mockery. Please provide evidence this has been done in your issue. Failure to provide this evidence will likely result in your issue being closed.

Description

inpackage config option is not used when used under packages

Mockery Version

2.43.0

Golang Version

1.22.2

Installation Method

Steps to Reproduce

  1. Use the package configuration method
  2. Create a go module, e.g. github.com/emreler/foo
  3. Define an interface in a subpackage, e.g. github.com/emreler/foo/bar
  4. Create a .mockery.yaml config file like provided below
  5. Run mockery

Config:

packages:
  github.com/emreler/foo/bar:
    config:
      all: true
      with-expecter: true
      inpackage: true

Expected Behavior

Mock file to be created under github.com/emreler/foo/bar

Actual Behavior

Mock file is created under github.com/emreler/mocks/github.com/emreler/foo/bar

The issue seems to be not there when using the old configuration method, e.g. like below:

inpackage: true
name: studentStore
recursive: true
LandonTClipp commented 1 month ago

Hi, sorry to be terse but migrating to packages has been thoroughly documented. It describes how you need additional configuration to specify where your mocks are placed. It seems you are simply lacking the right config.

Also we have a section in the docs specifically for this question: https://vektra.github.io/mockery/latest/configuration/#layouts

emreler commented 1 month ago

Indeed I've missed the dir parameter from my config and I get the desired behaviour when I set it to "{{.InterfaceDir}}", thanks for the reply.

The reason for the confusion was that I was expecting the mock to be placed in the same package as my interface when inpackage: true was set, regardless of the dir parameter. It feels like dir could be set to "{{.InterfaceDir}}" automatically when inpackage: true since if you place your mock in a different package then it's not in package anymore.

LandonTClipp commented 1 month ago

inpackage can probably be removed at some point once we make mockery a bit smarter. It's needed to tell mockery whether or not you've placed the mocks in the same location as the original interface, as the imports and namespaces will be different. Mockery could probably determine this automatically. I just never got around to it, thus we're stuck with needing inpackage for now.