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

Mocked Generated Classes using internal package #804

Closed
4 tasks
safaci2000 opened this issue Aug 15, 2024 · 4 comments
Closed
4 tasks

Mocked Generated Classes using internal package #804

safaci2000 opened this issue Aug 15, 2024 · 4 comments

Comments

@safaci2000
Copy link

safaci2000 commented Aug 15, 2024

PLEASE READ

DO NOT submit tickets without first using the latest version of Go, clearing your local golang package cache, and re-building mockery using the latest Go version and the latest version of mockery. Please provide evidence this has been done in your issue. Failure to provide this evidence will likely result in your issue being closed.

→ go version 
go version go1.23.0 darwin/arm64

[samirfaci@Samirs-MacBook-Pro.local:~/projects/ht/golang/ht-svc-controller] [22:54:50]
→ mockery --version
14 Aug 24 22:54 EDT INF Starting mockery dry-run=false version=v2.44.1
14 Aug 24 22:54 EDT INF Using config: /Users/samirfaci/projects/ht/golang/ht-svc-controller/.mockery.yaml dry-run=false version=v2.44.1
v2.44.1

Description

I'm currently using jet which lets me connect to postgres and run some queries. I'm not sure why this is the behavior and it's only in one of my repo files though the imports look similar.

In my class i import:

import (
	. "github.com/go-jet/jet/v2/postgres"
)

The generated mocked class looks like this:

  jet "github.com/go-jet/jet/v2/internal/jet"

which obviously fails as it's trying to import an internal library. postgres, exports some of the internal equivalent from internal/jet that I'm using.

Mockery Version

2.44.1

Go Version

1.23.0

Installation Method

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

Steps to Reproduce

run: mockery

Expected Behavior

Mock should not import an internal library.

Actual Behavior

Imported an internal library.


Related files can be found here: https://gist.github.com/safaci2000/34b1cef47a0b733d5cd73337bed16484

@safaci2000 safaci2000 changed the title Wrong Import generated Mocked Generated Classes using internal package Aug 15, 2024
@LandonTClipp
Copy link
Collaborator

Does https://vektra.github.io/mockery/latest/features/#replace-types do you what you need?

@safaci2000
Copy link
Author

@LandonTClipp that does seem to work.

I added this to my config which seems to work.

replace-type: 
  - github.com/go-jet/jet/v2/internal/jet=github.com/go-jet/jet/v2/postgres

I didn't see any concrete examples on: https://vektra.github.io/mockery/latest/configuration/#parameter-descriptions

but if there's a smarter way of doing this let me know.

As a side note, do you know why this is acting this way? It's seems peculiar to me. I have other repo classes that don't behave that way but I can't really tell what is different about this class in particular.

@LandonTClipp
Copy link
Collaborator

LandonTClipp commented Aug 15, 2024

Yes we do know why it happens, the reasons are enumerated in the link I posted above.

Basically, the Go AST did not provide any typing information for aliases. They would transparently resolve to the aliased type, so it was impossible for anything parsing the AST to know it was an alias.

I did do an investigation of this over a year ago to see if there was a fix with this new gotypesalias flag you can provide to the go build tool: #763

We now have go/types support, but I have yet to see if the compiler and export format preserve the typing info during compilation, which is also necessary: golang/go#64208

It doesn't appear that the Go devs have completely fixed this yet, so I'm doubtful there's anything further we can do.

https://github.com/go-jet/jet/blob/f55edafc0b0b122ea837e9309337296a7c84bb5c/postgres/expressions.go#L10

@safaci2000
Copy link
Author

Okay thank you for the info. I'll close this ticket out and keep my fingers crossed regarding the golang ticket.

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