vektra / mockery

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

arg package name collision within generated mock #798

Open CoderCoco opened 3 months ago

CoderCoco commented 3 months ago

Description

When generating mocks that references a package named args a collision occurs with the generated mock Run function.

This is generated:

func (_c *mockArgsInterface_isMetricDestination_Call) Run(run func(_a0 args.MetricDestination)) *mockArgsInterface_isMetricDestination_Call {
    _c.Call.Run(func(args mock.Arguments) {
        run(args[0].(args.MetricDestination))
    })
    return _c
}

There is a collision between args.MetricDestination and the local variable args

Mockery Version

2.43.2

Go Version

1.22

Installation Method

Steps to Reproduce

  1. Create a package named args that has a type you wish to import.
  2. Create an interface in another package that has a parameter type from that package.
  3. Execute mockery
  4. Test compilation fails

Expected Behavior

I would expect the mock to be generated correctly.

Actual Behavior

A mock is generated but when running tests I get this message: render/mock_args_interface.go:99:21: args.MetricDestination is not a type

CoderCoco commented 3 months ago

I have a work around by replacing the package causing the issue in the configuration. However, I don't think that this really fits the use case defined by the replace-type option.

LandonTClipp commented 3 months ago

Sorry, the collision handling logic is long in the tooth and doesn't comprehensively handle cases like this. Unless someone wants to tackle that project, using replace-type is probably the correct approach.