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

Type declarations that are *ast.IndexListExpr are not accepted #856

Closed
1 of 5 tasks
volmedo opened this issue Nov 29, 2024 · 3 comments · Fixed by #857
Closed
1 of 5 tasks

Type declarations that are *ast.IndexListExpr are not accepted #856

volmedo opened this issue Nov 29, 2024 · 3 comments · Fixed by #857

Comments

@volmedo
Copy link
Contributor

volmedo commented Nov 29, 2024

Description

Let's consider the following code:

package pkg

type Fooer[T any] interface {
    Foo(T)
}

type StringFooer Fooer[string]

mockery will generate both mocks, as expected. Note that StringFooer's declaration is an *ast.IndexExpr, which is accepted when parsing as per this line in pkg/parse.go.

However, when there is more than one type parameter:

package pkg

type Fooer[T1 any, T2 any] interface {
    Foo(T1, T2)
}

type StringFooer Fooer[string, int]

StringFooer's declaration is now an *ast.IndexListExpr, which is regarded as unacceptable, the interface is ignored and no mock is produced. I discovered the fact by running mockery with --log-level debug.

I tried the naive approach of adding *ast.IndexListExpr to the aforementioned case and produced a custom mockery binary that I then ran on the previous example and a working mock was generated.

I ran mockery e2e and unit tests after the change and all of them passed. I also used the mock generated previously in a (admittedly simple) test with successful results.

Is there a reason *ast.IndexListExpr is not accepted or was it just an overlook? I'm not super familiar with Go's ast package, so I may be missing something. I'd be more than happy to open a PR with the proposed change if it was just a mistake.

Mockery Version

v2.49.1

Go Version

1.23.3

Installation Method

  • Binary Distribution
  • Docker
  • brew
  • go install
  • Other: [specify]

Steps to Reproduce

  1. Create a source file such as:
package pkg

type Fooer[T1 any, T2 any] interface {
    Foo(T1, T2)
}

type StringFooer Fooer[string, int]
  1. Create a .mockery.yml file:
with-expecter: True
packages:
  path/to/pkg:
    interfaces:
      Fooer:
      StringFooer:
  1. Run mockery

Expected Behavior

I'd expect to get mocks for both Fooer and StringFooer.

Actual Behavior

Only Fooer gets a mock created.

@LandonTClipp
Copy link
Collaborator

Thanks for the report. I think this is indeed an overlook. It's kind of annoying that the Go AST treats this differently but it should be a matter of adding it to the switch statement that controls this, as you probably did. If you could submit a PR that would be great!

@volmedo
Copy link
Contributor Author

volmedo commented Dec 2, 2024

awesome, I'll open a PR shortly!

In Go AST's defence I'll say that index expressions got a lot more varied after the introduction of generics syntax.

@volmedo
Copy link
Contributor Author

volmedo commented Dec 3, 2024

Aside from the main PR, I submitted an additional PR with fixes to minor logging issues I found while debugging 🙂.

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.

2 participants