ernesto-jimenez / gogen

A set of go packages and command line tools to generate Go code
MIT License
95 stars 14 forks source link

wrongfully unqualified types in generated file #3

Open benma opened 8 years ago

benma commented 8 years ago

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.

ernesto-jimenez commented 8 years ago

@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?

benma commented 8 years ago

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).

ernesto-jimenez commented 8 years ago

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.

benma commented 8 years ago

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.

ernesto-jimenez commented 8 years ago

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 :)

benma commented 8 years ago

Thanks. I made a PR.

benma commented 7 years ago

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

benma commented 7 years ago

Bump :)