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

Interfaces which contain generic types generate incorrectly if output to different package #112

Open
danvolchek opened this issue Jul 22, 2024 · 0 comments

Comments

@danvolchek
Copy link

danvolchek commented Jul 22, 2024

Setup

Using this file structure:

├── bar
│   └── bar.go
├── foo.go
└── gen.go

where

bar/bar.go:

package bar

type Bar[T any] interface {
	// methods
}

and

foo.go:

package foo

import (
   "bar"
)

type Foo interface {
	DoFoo() bar.Bar[Baz]
}

type Baz struct {
	// fields
}

Works

The following generates correctly:

gen.go:

package foo

//go:generate minimock -i Foo

mocks/foo_mock_test.go:

// Code generated by http://github.com/gojuno/minimock (v3.3.13). DO NOT EDIT.

package foo

import (
	"sync"
	mm_atomic "sync/atomic"
	mm_time "time"

	"path/to/bar"
	"github.com/gojuno/minimock/v3"
)

// FooMock implements foo.Foo
type FooMock struct {
	t          minimock.Tester
	finishOnce sync.Once

	funcDoFoo          func() (p1 bar.Bar[Baz])
	inspectFuncDoFoo   func()
	afterDoFooCounter  uint64
	beforeDoFooCounter uint64
	DoFooMock          mFooMockDoFoo
}
...

Here, the Baz reference in func() (p1 bar.Bar[Baz]) succeeds because the file is generated inside package foo.

Doesn't work

However, placing the output in a different package fails:

gen.go:

package foo

//go:generate minimock -o mocks -i Foo

mocks/foo_mock_test.go:

// Code generated by http://github.com/gojuno/minimock (v3.3.13). DO NOT EDIT.

package mocks

import (
	"sync"
	mm_atomic "sync/atomic"
	mm_time "time"

	"path/to/bar"
	"github.com/gojuno/minimock/v3"
)

// FooMock implements foo.Foo
type FooMock struct {
	t          minimock.Tester
	finishOnce sync.Once

	funcDoFoo          func() (p1 bar.Bar[Baz])
	inspectFuncDoFoo   func()
	afterDoFooCounter  uint64
	beforeDoFooCounter uint64
	DoFooMock          mFooMockDoFoo
}
...

Here, the Baz reference in func() (p1 bar.Bar[Baz]) fails because the file is generated inside package mocks - it needs to import /path/to/foo and reference it as foo.Baz.

Also

It works when not using generics

If the Foo interface doesn't use generics, then it works, even when placing mocks in a different package:

foo.go:

package foo

type Foo interface {
	DoFoo() Baz
}

type Baz struct {
	// fields
}

gen.go:

package foo

//go:generate minimock -o mocks -i Foo

mocks/foo_mock_test.go:

// Code generated by http://github.com/gojuno/minimock (v3.3.13). DO NOT EDIT.

package mocks

import (
	"sync"
	mm_atomic "sync/atomic"
	mm_time "time"

	mm_foo "path/to/foo"
	"github.com/gojuno/minimock/v3"
)

// FooMock implements foo.Foo
type FooMock struct {
	t          minimock.Tester
	finishOnce sync.Once

	funcDoFoo          func() (n1 mm_foo.Baz)
	inspectFuncDoFoo   func()
	afterDoFooCounter  uint64
	beforeDoFooCounter uint64
	DoFooMock          mFooMockDoFoo
}
...

It doesn't work even if the generic type is in the same package as the interface being mocked

foo.go:

package foo

type Bar[T any] interface {
	// methods
}

type Foo interface {
	DoFoo() Bar[Baz]
}

type Baz struct {
	// fields
}

gen.go:

package foo

//go:generate minimock -o mocks -i Foo

mocks/foo_mock_test.go:

// Code generated by http://github.com/gojuno/minimock (v3.3.13). DO NOT EDIT.

package mocks

import (
	"sync"
	mm_atomic "sync/atomic"
	mm_time "time"

	"github.com/gojuno/minimock/v3"
)

// FooMock implements foo.Foo
type FooMock struct {
	t          minimock.Tester
	finishOnce sync.Once

	funcDoFoo          func() (p1 Bar[Baz])
	inspectFuncDoFoo   func()
	afterDoFooCounter  uint64
	beforeDoFooCounter uint64
	DoFooMock          mFooMockDoFoo
}
...

Here, both Bar and Baz are referenced incorrectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant