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

Mockery mistakenly imports internal packages when running the 'mockery' command #749

Closed NawafSwe closed 5 months ago

NawafSwe commented 5 months ago

Description

I am creating a mock for my Google Pub/Sub client to be tested without making actual calls to Google Pub/Sub. I am mocking an interface defined below.

package messaging

import (
    "context"
    "cloud.google.com/go/pubsub"
    "google.golang.org/grpc/status"
)

type MessageSender interface {
    PublishAsync(ctx context.Context, topicId string, msg *pubsub.Message)
}
type MessageService interface {
    MessageSender
    CreateSub(id string, topic *pubsub.Topic)
    CreateTopic(topic string) *pubsub.Topic
    GetSubscription(ctx context.Context, id string) (*pubsub.Subscription, error)
    CreateTopicWithSchema(topic string, tc pubsub.TopicConfig)
    GetTopic(ctx context.Context, topicId string) (*pubsub.Topic, error)
}

And here is my mockery config

with-expecter: true
packages:
  github.com/nawafswe/repoName/pkg/messaging:
    config:
    interfaces:
      MessageService:

Running the command mockery in the root directory successfully generates files for the mocked interfaces. However, the issue is that it is importing internal packages, as shown in the picture below. Screenshot 2024-01-20 at 7 46 01 PM

Mockery Version

v2.40.1

Golang Version

1.21.6 darwin/arm64

Installation Method

Steps to Reproduce

  1. Run mockery
  2. File generated with wrong imports
  3. Running test cases will fail as shown below

Expected Behavior

The expected behavior is to import from a public package. I had to modify it to make it work, as shown below in the picture:

Screenshot 2024-01-20 at 7 59 08 PM

Actual Behavior

Importing from the incorrect location, as shown above, is not allowed. As a result, test cases won't run, as illustrated below: Screenshot 2024-01-20 at 8 02 48 PM

Thanks, in advance for your help, and I hope I made it clear 🙏🏻

LandonTClipp commented 5 months ago

You want to use replacetypes: https://vektra.github.io/mockery/latest/features/#replace-types

Let me know if that helps.

NawafSwe commented 5 months ago

It worked, thank you very much, sorry for the inconvenience 🙏🏻