matryer / moq

Interface mocking tool for go generate
http://bit.ly/meetmoq
MIT License
1.96k stars 126 forks source link

moq should not follow type aliases to internal packages #214

Closed davidmdm closed 3 months ago

davidmdm commented 5 months ago

Moq resolves type aliases which can lead to non-compilable code.

For example:

package main

import (
    "context"

    "cloud.google.com/go/pubsub"
)

//go:generate moq -out ./receiver_mock.go . Receiver
type Receiver interface {
    Receive(ctx context.Context, f func(context.Context, *pubsub.Message)) error
}

In the google pubsub package Message is declared as:

import (
  ipubsub "cloud.google.com/go/internal/pubsub"
)

...

type Message = ipubsub.Message

And this the code generated by moq resolves the type alias and produces:

package main

import (
    "cloud.google.com/go/internal/pubsub" // ERROR CANNOT COMPILE BECAUSE OF INTERNAL IMPORT
    "context"
    "sync"
)

// Ensure, that ReceiverMock does implement Receiver.
// If this is not the case, regenerate this file with moq.
var _ Receiver = &ReceiverMock{}

...

Ideally moq would generate the code using the non-internal import inferred from the type alias.