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

Add resolve-type-alias parameter #843

Merged
merged 1 commit into from
Nov 20, 2024

Conversation

LandonTClipp
Copy link
Collaborator

@LandonTClipp LandonTClipp commented Nov 19, 2024

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

@LandonTClipp LandonTClipp force-pushed the LandonTClipp/issue_839 branch from c3a9cee to 13fc607 Compare November 19, 2024 20:33
@LandonTClipp LandonTClipp marked this pull request as ready for review November 19, 2024 20:33
@max-melentyev
Copy link

Do I understand correctly that this PR makes mocks to be created in _test package by default?
AFAIK _test packages are similar to /internal/ and their content is not visible from other packages, even from other tests.

@LandonTClipp
Copy link
Collaborator Author

Not quite. The relevant part of this PR is here: https://github.com/vektra/mockery/pull/843/files#diff-9233145a2afa8ecc02f5d52807d154baf4d2e2a118f22721cb80277f9b2e6d82R541-R553 The .mockery.yaml changes were just some cleanup I was doing.

You can see the two tests that demonstrate the new behavior: https://github.com/vektra/mockery/pull/843/files#diff-f52d7d97345ebe96212f5c1dca1585a844ef7af865ec3273358bc9b6597c0c83

@LandonTClipp LandonTClipp merged commit a27cb03 into vektra:master Nov 20, 2024
4 checks passed
@LandonTClipp LandonTClipp deleted the LandonTClipp/issue_839 branch November 20, 2024 23:17
@max-melentyev
Copy link

@LandonTClipp looks like it now expands type to a struct definition:

-// Code generated by mockery v2.44.1. DO NOT EDIT.
+// Code generated by mockery v2.49.0. DO NOT EDIT.
....
-func (_m *CassandraAgentClientPool) GetClientForInstance(_a0 *manualv1alpha1.Instance) (pb.CassandraAgentClient, error) {
+func (_m *CassandraAgentClientPool) GetClientForInstance(_a0 *struct {
+       v1.TypeMeta
+       v1.ObjectMeta
+       Spec   manualv1alpha1.InstanceSpec   `json:"spec"`
+       Status manualv1alpha1.InstanceStatus `json:"status,omitempty"`
+}) (pb.CassandraAgentClient, error) {

Is this expected?

@LandonTClipp
Copy link
Collaborator Author

Can you show the definition of your type?

@max-melentyev
Copy link

import (
        // ...
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ...
type Instance struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   InstanceSpec   `json:"spec"`
	Status InstanceStatus `json:"status,omitempty"`
}

@LandonTClipp
Copy link
Collaborator Author

I'm not able to replicate this behavior when using the packages config. Keep in mind that your configuration style is deprecated and will be removed soon.

@LandonTClipp
Copy link
Collaborator Author

I used your reproducer for the original issue (modifying the Makefile so I can use the latest mockery).

package p

type T = int

type Instance struct {
	attribute int
}

//go:generate mockery --quiet --name I
type I interface{ F(T) }

//go:generate mockery --quiet --name I2
type I2 interface{ F(Instance) }

The generated mock file for I2:

// Code generated by mockery v2.49.0. DO NOT EDIT.

package mocks

import (
	p "mockery-issue/p"

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

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

// F provides a mock function with given fields: _a0
func (_m *I2) F(_a0 p.Instance) {
	_m.Called(_a0)
}

You can see it is not writing out the struct definition, so I'm unclear how this is happening for you.

@max-melentyev
Copy link

Here I could reproduce it: max-melentyev/mockery-issue@ee08ac9

Aliased struct gets inlined.

@LandonTClipp
Copy link
Collaborator Author

I'm able to reproduce it as well, working on a fix.

LandonTClipp added a commit to LandonTClipp/mockery that referenced this pull request Nov 26, 2024
@max-melentyev found in vektra#843 that type aliases to structs were being
rendered as a struct literal. The problem was that we need the type of the
right hand side of the alias definition, not the underlying type itself.
@LandonTClipp
Copy link
Collaborator Author

The merged PR above should resolve the issue for you.

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 this pull request may close these issues.

Circular dependency in go 1.23 when mock is used in the package where it's defined
2 participants