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

Invalid mock generated with the same package names #291

Closed
RossHammer opened this issue Jun 17, 2020 · 10 comments · Fixed by #422
Closed

Invalid mock generated with the same package names #291

RossHammer opened this issue Jun 17, 2020 · 10 comments · Fixed by #422

Comments

@RossHammer
Copy link
Contributor

Using either 1.1.2 or 2.0.0-alpha.13 release builds of mockery we have the following issue

cloudformation/interface.go

package cloudformation

import "github.com/aws/aws-sdk-go/service/cloudformation"

type invalid interface {
	String(*cloudformation.CreateStackInput) string
}

main.go

package main

func main() {}

generates a mock like like the following when using mockery -all -testonly -inpkg

// Code generated by mockery v1.1.2. DO NOT EDIT.

package cloudformation

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

// mockInvalid is an autogenerated mock type for the invalid type
type mockInvalid struct {
	mock.Mock
}

// String provides a mock function with given fields: _a0
func (_m *mockInvalid) String(_a0 *cloudformation.CreateStackInput) string {
	ret := _m.Called(_a0)

	var r0 string
	if rf, ok := ret.Get(0).(func(*cloudformation.CreateStackInput) string); ok {
		r0 = rf(_a0)
	} else {
		r0 = ret.Get(0).(string)
	}

	return r0
}

You can see the import for cloudformation is missing and in turn the go reports undefined: cloudformation in the mock file.

It seems like when the cloudformation directory is at the root the issue cannot be re-created. When mockery is built from source either via go get or cloning and go install this issue seems to go away and the import is generated properly. As installing is not supported using go get anymore our mocks are no longer generating properly.

package cloudformation

import (
	"github.com/aws/aws-sdk-go/service/cloudformation"
	mock "github.com/stretchr/testify/mock"
)
@LandonTClipp
Copy link
Contributor

What version of go are you using in go get ? I'd say this is a pretty serious issue, let's get this addressed.

@LandonTClipp
Copy link
Contributor

I can reproduce this issue as you describe. I'm looking for a possible cause.

@LandonTClipp
Copy link
Contributor

So I added some debug messages and discovered that mockery is skipping this explicitly:

[ltclipp@landon-virtualbox test_issue]$ ../mockery --inpackage --all --log-level debug
17 Jun 20 19:38 CDT INF Starting mockery dry-run=false version=0.0.0-dev
17 Jun 20 19:38 CDT INF Walking dry-run=false version=0.0.0-dev
17 Jun 20 19:38 CDT DBG parsing dir=/home/ltclipp/git/vektra/mockery/test_issue/cloudformation dry-run=false file=interface.go version=0.0.0-dev
17 Jun 20 19:38 CDT DBG parsing dir=/home/ltclipp/git/vektra/mockery/test_issue/cloudformation dry-run=false file=mock_invalid.go version=0.0.0-dev
17 Jun 20 19:38 CDT DBG parsing dir=/home/ltclipp/git/vektra/mockery/test_issue/cloudformation dry-run=false file=interface.go version=0.0.0-dev
17 Jun 20 19:38 CDT DBG parsing dir=/home/ltclipp/git/vektra/mockery/test_issue/cloudformation dry-run=false file=mock_invalid.go version=0.0.0-dev
17 Jun 20 19:38 CDT DBG parsing dir=/home/ltclipp/git/vektra/mockery/test_issue dry-run=false file=main.go version=0.0.0-dev
17 Jun 20 19:38 CDT DBG creating writer to file dry-run=false interface=invalid path=/home/ltclipp/git/vektra/mockery/test_issue/cloudformation/mock_invalid.go qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT DBG populating imports dry-run=false interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT DBG func(*github.com/aws/aws-sdk-go/service/cloudformation.CreateStackInput) string dry-run=false interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT DBG (cloudformation/cloudformation.invalid).String dry-run=false interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT DBG found import dry-run=false import=github.com/aws/aws-sdk-go/service/cloudformation interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT DBG import equals interface package's path, skipping dry-run=false import=github.com/aws/aws-sdk-go/service/cloudformation interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT DBG found import dry-run=false import=github.com/stretchr/testify/mock interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT DBG writing out import dry-run=false import=github.com/stretchr/testify/mock interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev
17 Jun 20 19:38 CDT INF Generating mock dry-run=false interface=invalid qualified-name=cloudformation/cloudformation version=0.0.0-dev

Although, it's a bit mysterious to me that even though it says it's skipping it, the import is still inserted somehow. That I don't understand.

@LandonTClipp
Copy link
Contributor

LandonTClipp commented Jun 18, 2020

Additionally, I found the commit that caused this bug: d9507cd5

I don't have time to fix this today but at least I know what the problem is. I will fix the bug and write two unit tests to make sure this doesn't happen.

@RossHammer
Copy link
Contributor Author

Awesome thanks for looking into it so quickly. I would try and help more but I can't figure out how to reproduce it from the source code, only the compiled binaries. Here is the version of go I was using

❯ go version
go version go1.14.3 darwin/amd64

@LandonTClipp
Copy link
Contributor

Adding a few logging messages to see if we can catch why this is happening. Making a new build now, will see if this highlights any issues.

@RossHammer
Copy link
Contributor Author

I have discovered how to re-create the issue when building from source! When the formatter runs it can add/remove imports and it will add the import when build locally for some reason. So this line needs to be changed to the following so things are consistent. Still trying to track down how to fix the issue properly.

https://github.com/vektra/mockery/blob/master/pkg/generator.go#L662

opt := &imports.Options{Comments: true, FormatOnly: true}

RossHammer added a commit to RossHammer/mockery that referenced this issue Jun 22, 2020
…package would not get included in the mock
LandonTClipp added a commit that referenced this issue Jun 28, 2020
Fixing #291 where a different import with the same name as the package would not get included in the mock
LandonTClipp added a commit that referenced this issue Jun 28, 2020
…e package would not get included in the mock"
LandonTClipp added a commit that referenced this issue Jun 28, 2020
Revert "Fixing #291 where a different import with the same name as the package would not get included in the mock"
@RossHammer
Copy link
Contributor Author

Well that is a bummer it caused other issues. I guess this ticket should be re-opened for now then.

@LandonTClipp LandonTClipp reopened this Jun 28, 2020
@keshamin
Copy link

keshamin commented Oct 7, 2020

I faced the same issue. I use the latest docker image to generate mocks. Mocked interface is in package training (.../service/training/) and it uses structs from another package named training (.../models/training/). The import of training that holds models is missing, so the mock code can't be compiled.

rws-github pushed a commit to rws-github/mockery that referenced this issue Mar 16, 2021
@gogis79
Copy link

gogis79 commented Sep 15, 2021

Have you guys fixed that issue yet? Because it's still present in the current version. Yes, running go generate locally in the folder properly adds import, but it's unfeasible in a project with thousands of files and hundreds of directories.

I was hoping aliasing would help, but it isn't (it should?)

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

Successfully merging a pull request may close this issue.

4 participants