vektra / mockery

A mock code autogenerator for Go
https://vektra.github.io/mockery/
BSD 3-Clause "New" or "Revised" License
6.15k stars 412 forks source link

Mocked Generated Classes using internal package #804

Closed safaci2000 closed 3 months ago

safaci2000 commented 3 months ago

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

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

LandonTClipp commented 3 months ago

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

safaci2000 commented 3 months ago

@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 commented 3 months ago

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: https://github.com/vektra/mockery/issues/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: https://github.com/golang/go/issues/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 commented 3 months ago

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