gogen icon indicating copy to clipboard operation
gogen copied to clipboard

wrongfully unqualified types in generated file

Open benma opened this issue 7 years ago • 8 comments

Hi

Example input:

package main

type Foo struct{}

//go:generate goautomock -mock-name fooMock -mock-pkg main_test -o mock.auto_test.go fooInterface
type fooInterface interface {
    Foo() Foo
}

func main() {}

After running go generate ./..., this file is generated:

/*
* CODE GENERATED AUTOMATICALLY WITH github.com/ernesto-jimenez/gogen/automock
* THIS FILE SHOULD NOT BE EDITED BY HAND
 */

package main_test

import (
    "fmt"
    mock "github.com/stretchr/testify/mock"

    main "github.com/test/test"
)

// fooMock mock
type fooMock struct {
    mock.Mock
}

// Foo mocked method
func (m *fooMock) Foo() Foo {

    ret := m.Called()

    var r0 Foo
    switch res := ret.Get(0).(type) {
    case nil:
    case Foo:
        r0 = res
    default:
        panic(fmt.Sprintf("unexpected type: %v", res))
    }

    return r0

}

In the generated file, the main package is imported, but the mock of Foo has an error:

func (m *fooMock) Foo() Foo should be func (m *fooMock) Foo() main.Foo.

benma avatar Aug 26 '16 10:08 benma

@benma the bigger issue is with mixing two packages (main and main_test) in the same directory. Even if the generated code was OK, once it is generated go generate will fail from then on because it expects one package per folder.

$ ls
main.go
$ cat main.go
package main

type Foo struct{}

//go:generate goautomock -mock-name fooMock -mock-pkg main_test -o mock.auto_test.go fooInterface
type fooInterface interface {
        Foo() Foo
}

func main() {}
$ go generate 
Generating mock for fooInterface in mock.auto_test.go
$ go generate
/Users/ernesto/Projects/go/src/github.com/ernesto-jimenez/test/mock.auto_test.go:6:1: package main_test; expected main
main.go:5: running "goautomock": exit status 1

Do you want to look into the code to add support for folders with two packages?

ernesto-jimenez avatar Aug 26 '16 11:08 ernesto-jimenez

Thanks for the quick response.

This is a _test package, which should be in the same folder (black box testing). I am using the generated mock only in the unit tests, which is why I assumed I should generate it like that.

Maybe this is the bug then? The tool should allow the unit test package to be in the same folder, and correctly import the types from (main.Foo).

benma avatar Aug 26 '16 11:08 benma

Yes, the bigger bug is allowing a _test package alongside a package. Right now the tool expects just one package per folder.

I never use _test packages myself, so never ran into the issue.

ernesto-jimenez avatar Aug 26 '16 11:08 ernesto-jimenez

I confirm that it works if the generated file of a different package lives in another folder.

Do you plan on fixing the bug to enable _test packages? I can try to take care of it if you don't have the time, but I figure it you would be faster because I don't know the code yet.

benma avatar Aug 26 '16 11:08 benma

I'm travelling the next few weeks, so it will take some time for me to be able to review it.

If you want to give it a try, I will be happy to answer questions and review the PR :)

ernesto-jimenez avatar Aug 26 '16 12:08 ernesto-jimenez

Thanks. I made a PR.

benma avatar Aug 26 '16 14:08 benma

@ernesto-jimenez, what is the status of this?

benma avatar Apr 02 '17 20:04 benma

Bump :)

benma avatar May 23 '17 06:05 benma