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: moq template, referring to interfaces in other packages. #882

Closed
1 of 5 tasks
ucirello opened this issue Jan 2, 2025 · 6 comments
Closed
1 of 5 tasks

v3: moq template, referring to interfaces in other packages. #882

ucirello opened this issue Jan 2, 2025 · 6 comments
Labels

Comments

@ucirello
Copy link

ucirello commented Jan 2, 2025

Description

Currently one of my workflows involve generating interfaces that belong to other packages as in:

go run github.com/matryer/moq -pkg somepkg -out logger_mock_test.go ../../../log Logger:loggerMock

Mockery Version

v3.0.0-alpha.12 aka 365b8ebb425518810e9327b7356407ac415edd96

Go Version

go1.23

Installation Method

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

Steps to Reproduce

I can't reproduce in any steps because it doesn't seem to be supported.

Expected Behavior

Have a way to declare an interface from one package but store the result into another.

Actual Behavior

Behavior not available.

@LandonTClipp
Copy link
Collaborator

This is certainly supported if I'm understanding you correctly. You can generate mocks for any package and store them just about anywhere. For example lets's say you have a project at github.com/user/repo/subpkg1 and you want to store its mocks in github.com/user/repo/subpkg2. It would look something like this:

packages:
   github.com/user/repo/subpkg1:
    config:
      all: True
      # Store them in the location of subpkg2
      dir: "./subpkg2/"
      filename: "mocks_subpkg1.go"
  # generate mocks from subpkg2 as usual
  github.com/user/repo/subpkg2:
    config:
      all: True

Is that what you're asking for?

@LandonTClipp LandonTClipp added the v3 label Jan 3, 2025
@ucirello
Copy link
Author

ucirello commented Jan 3, 2025

I tried your suggestion, however, I am not able to override the package name:

all: False
recursive: False
template: moq
dir: "{{.InterfaceDir}}"
filename: "mocks_test.go"
mockname: "{{ firstLower .InterfaceName }}Mock"
packages:
  github.com/company/repo/pkg/one/internal/subpkg1:
    interfaces:
      Service:
          config:
            filename: "service_mock_test.go"

  github.com/company/repo/pkg/log:
    interfaces:
      Logger:
        configs:
          - dir: "./pkg/one/internal/subpkg1"
            outpkg: "subpkg1"
            filename: "logger_mock_test.go"

Errors with:

unable to decode interface config: 1 error(s) decoding:

* '' has invalid keys: outpkg
goroutine 1 [running]:
runtime/debug.Stack()
        /usr/local/go/src/runtime/debug/stack.go:26 +0x64
github.com/vektra/mockery/v3/internal/stackerr.NewStackErr(...)
        /home/user/go/pkg/mod/github.com/vektra/mockery/[email protected]/internal/stackerr/stackerr.go:17
github.com/vektra/mockery/v3/internal/stackerr.NewStackErrf({0x10492fea0, 0x1400137fa10}, {0x10477f902?, 0x14000714640?}, {0x0?, 0x0?, 0x65?})
        /home/user/go/pkg/mod/github.com/vektra/mockery/[email protected]/internal/stackerr/stackerr.go:24 +0xa0
github.com/vektra/mockery/v3/internal.(*Config).GetInterfaceConfig(0x14000111b80, {0x104933ce8, 0x140001e0ff0}, {0x140000dc6c0, 0x20}, {0x140007021f0, 0x6})
        /home/user/go/pkg/mod/github.com/vektra/mockery/[email protected]/internal/config.go:400 +0xa88
github.com/vektra/mockery/v3/internal/cmd.(*RootApp).Run(0x14000111b80)
        /home/user/go/pkg/mod/github.com/vektra/mockery/[email protected]/internal/cmd/mockery.go:262 +0x7a4
github.com/vektra/mockery/v3/internal/cmd.NewRootCmd.func1(0x14000184e00?, {0x10474cc88?, 0x4?, 0x10474cc8c?})
        /home/user/go/pkg/mod/github.com/vektra/mockery/[email protected]/internal/cmd/mockery.go:41 +0x44
github.com/spf13/cobra.(*Command).execute(0x1400019c908, {0x14000114010, 0x0, 0x0})
        /home/user/go/pkg/mod/github.com/spf13/[email protected]/command.go:920 +0x630
github.com/spf13/cobra.(*Command).ExecuteC(0x1400019c908)
        /home/user/go/pkg/mod/github.com/spf13/[email protected]/command.go:1044 +0x320
github.com/spf13/cobra.(*Command).Execute(...)
        /home/user/go/pkg/mod/github.com/spf13/[email protected]/command.go:968
github.com/vektra/mockery/v3/internal/cmd.Execute()
        /home/user/go/pkg/mod/github.com/vektra/mockery/[email protected]/internal/cmd/mockery.go:79 +0x38
main.main()
        /home/user/go/pkg/mod/github.com/vektra/mockery/[email protected]/main.go:8 +0x1c

@ucirello
Copy link
Author

ucirello commented Jan 3, 2025

When I replaced outpkg with pkgname, it executes but silently ignores the value I declare in the interface subconfiguration item.

@LandonTClipp
Copy link
Collaborator

I think I see what's happening. The reason isn't entirely interesting but the gist is that for pkgname, mockery only uses the package-level config and ignores the interface config. I'll fix this.

@LandonTClipp
Copy link
Collaborator

https://github.com/vektra/mockery/releases/tag/v3.0.0-alpha.15 should fix your issue. Just note that mockery will (expectedly) throw an error if the mocks you've placed in a particular file have been configured with different pkgnames. A file can only have one pkgname.

@ucirello
Copy link
Author

ucirello commented Jan 3, 2025

It works.

@ucirello ucirello closed this as completed Jan 3, 2025
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

2 participants