-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add back `replace-types` Port the `replace-type` parameter into mockery v3. This changes the config schema used in the v2 replace-type parameter to be more grokable. This is also a practical matter: the somewhat complex parsing mechanism in v2 is replaced by simple struct attributes. The implementation here is quite different from v2. The templating system in v3 requires type information for all types, and thus we cannot do simple string replacements. We have to call `packages.Load` on the referenced type so we can get real type information. This means that `replace-type` will incur additional latency. This latency penalty, however, grants us additional correctness of implementation: the mock templates will have full type information, and if something about the `replace-type` parameter is wrong (either package path or type name don't exist), mockery will explicitly log this as an error. This does _not_ implement replacements of type constraints as done in v2: #750. This work still needs to be done. Per #864 * Additional documentation * Add docs and hint in log message on how to enable force-file-write.
- Loading branch information
1 parent
85c4718
commit e307bc6
Showing
35 changed files
with
650 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ jobs: | |
test: | ||
uses: ./.github/workflows/reusable-testing.yml | ||
with: | ||
ref: ${{ inputs.ref }} | ||
ref: ${{ inputs.ref }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: replace-type | ||
--- | ||
|
||
## Description | ||
|
||
The `#!yaml replace-type:` parameter allows you to replace a type in the generated mocks with another type. Take for example the following interface: | ||
|
||
|
||
```go title="interface.go" | ||
package replace_type | ||
|
||
import ( | ||
"github.com/vektra/mockery/v3/internal/fixtures/example_project/replace_type/rti/rt1" | ||
"github.com/vektra/mockery/v3/internal/fixtures/example_project/replace_type/rti/rt2" | ||
) | ||
|
||
type RType interface { | ||
Replace1(f rt1.RType1) | ||
} | ||
``` | ||
|
||
You can selectively replace the `rt1.RType1` with a new type if so desired. For example: | ||
|
||
```yaml title=".mockery.yml" | ||
replace-type: | ||
github.com/vektra/mockery/v3/internal/fixtures/example_project/replace_type/rti/rt1: | ||
RType1: | ||
pkg-path: github.com/vektra/mockery/v3/internal/fixtures/example_project/replace_type/rti/rt2 | ||
type-name: RType2 | ||
``` | ||
The mock will now replace all instances of `rt1.RType1` with `rt2.RType2`. You can see the before and after of `mockery`-style mocks: | ||
|
||
=== "before" | ||
|
||
```go | ||
// Replace2 provides a mock function for the type RTypeReplaced1 | ||
func (_mock *RTypeReplaced1) Replace1(f rt1.RType1) { | ||
_mock.Called(f) | ||
return | ||
} | ||
``` | ||
|
||
=== "after" | ||
|
||
```go | ||
// Replace2 provides a mock function for the type RTypeReplaced1 | ||
func (_mock *RTypeReplaced1) Replace1(f rt2.RType2) { | ||
_mock.Called(f) | ||
return | ||
} | ||
``` | ||
|
||
## Background | ||
|
||
This parameter is useful if you need to need to work around packages that use internal types. Take for example the situation found [here](https://github.com/vektra/mockery/issues/864#issuecomment-2567788637), noted by [RangelReale](https://github.com/RangelReale). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pkg | ||
package internal | ||
|
||
import "fmt" | ||
|
||
|
Oops, something went wrong.