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

wrongfully unqualified types in generated file #3

Open
benma opened this issue Aug 26, 2016 · 8 comments
Open

wrongfully unqualified types in generated file #3

benma opened this issue Aug 26, 2016 · 8 comments

Comments

@benma
Copy link

benma commented Aug 26, 2016

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
Copy link
Owner

@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
Copy link
Author

benma commented Aug 26, 2016

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
Copy link
Owner

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
Copy link
Author

benma commented Aug 26, 2016

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
Copy link
Owner

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 added a commit to benma/gogen that referenced this issue Aug 26, 2016
@benma
Copy link
Author

benma commented Aug 26, 2016

Thanks. I made a PR.

benma added a commit to benma/gogen that referenced this issue Aug 26, 2016
@benma
Copy link
Author

benma commented Apr 2, 2017

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

@benma
Copy link
Author

benma commented May 23, 2017

Bump :)

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

2 participants