matryer / moq

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

Self import issue #205

Closed spolischook closed 1 year ago

spolischook commented 1 year ago

Somehow in the mock, moq generated import that import package itself So after the second generate command I've got:

couldn't load source package: -: import cycle not allowed: import stack: [github.com/.../api github.com/.../api] (and 2 more errors)

An Interface used Response type as a returned value, and it's generated in mock as api.Response. If I remove import by hand and remove api. prefix from the type it works.

spolischook commented 1 year ago

Okay, so it seems like if the interface relates to the struct from the same package it will have "self include" issue in the mock.
For example code:

package something

//go:generate moq -pkg something -out ./doable_mock.go . DoableInterface:Mock
type DoableInterface interface {
    Do() Response
}

type Response struct {
}

will generate the mock with self include:

// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq

package something

import (
    "github.com/spolischook/moqIssue/something" // <--- this is actually the issue!
    "sync"
)
...
type Mock struct {
    // DoFunc mocks the Do method.
    DoFunc func() something.Response
...

Is this expected behavior? or what I'm doing wrong?

spolischook commented 1 year ago

It happens because of -pkg something parameter - without it, it works well