Skip to content

Commit

Permalink
Lots of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 2, 2025
1 parent dcf72ec commit bd61701
Show file tree
Hide file tree
Showing 42 changed files with 1,129 additions and 1,573 deletions.
65 changes: 7 additions & 58 deletions .mockery.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
template: "mockery"
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}_mock.go"
pkgname: mocks
tags: "custom2"
formatter: "noop"
_anchors: &inpackage_config
all: True
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
pkgname: "{{.SrcPackageName}}_test"
filename: "mock_{{.InterfaceName | snakecase }}_test.go"
all: True
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
pkgname: "{{.SrcPackageName}}"
filename: "mocks_test.go"
packages:
github.com/vektra/mockery/v3/internal/fixtures/buildtag/comment:
config:
mock-build-tags: "custom3 && (!windows || !darwin || !freebsd)"
interfaces:
IfaceWithCustomBuildTagInComment:
github.com/vektra/mockery/v3/pkg:
config:
all: False
interfaces:
TypesPackage:
github.com/vektra/mockery/v3/internal/fixtures:
config:
all: True
dir: "{{.InterfaceDir}}"
filename: "mocks.go"
pkgname: "test"
mockname: "Mock{{.InterfaceName}}"
interfaces:
Requester:
RequesterArgSameAsNamedImport:
RequesterVariadic:
configs:
- mockname: MockRequesterVariadicOneArgument
Expand All @@ -45,52 +31,15 @@ packages:
- mockname: MockExpecter
template-data:
unroll-variadic: True
RequesterReturnElided:
VariadicNoReturnInterface:
config:
template-data:
unroll-variadic: False
github.com/vektra/mockery/v3/internal/fixtures/recursive_generation:
config:
recursive: True
all: True
dir: "{{.InterfaceDir}}"
filename: "{{.InterfaceName}}_mock.go"
mockname: "Mock{{.InterfaceName}}"
pkgname: "{{.SrcPackageName}}"
github.com/vektra/mockery/v3/internal/fixtures/empty_return:
config:
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}Mock"
pkgname: "{{.SrcPackageName}}"
filename: "mock_{{.InterfaceName}}_test.go"
github.com/vektra/mockery/v3/internal/fixtures/method_args/same_name_arg_and_type:
config:
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}Mock"
pkgname: "{{.SrcPackageName}}"
filename: "mock_{{.InterfaceName}}_test.go"
github.com/vektra/mockery/v3/internal/fixtures/iface_typed_param:
config: *inpackage_config
github.com/vektra/mockery/v3/internal/fixtures/example_project:
config: *inpackage_config
github.com/vektra/mockery/v3/internal/fixtures/index_list_expr:
config: *inpackage_config
github.com/vektra/mockery/v3/internal/fixtures/iface_new_type:
config: *inpackage_config
github.com/vektra/mockery/v3/internal/fixtures/issue845:
config:
<<: *inpackage_config
filename: "mock_{{.MockName}}_test.go"
interfaces:
Interface:
configs:
- mockname: WithFix
github.com/vektra/mockery/v3/internal/fixtures/type_alias:
config:
all: True
dir: "{{.InterfaceDir}}"
filename: "mock_{{.MockName}}_test.go"
pkgname: "{{.SrcPackageName}}_test"
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tasks:
mocks.remove:
desc: remove all mock files
cmds:
- find . -name '*_mock.go' -o -name 'mock_*_test.go' -o -name 'mocks.go' -o -name "mocks_moq.go" | xargs -r rm
- find . -name '*_mock.go' -o -name 'mock_*_test.go' -o -name 'mocks.go' -o -name "mocks_moq.go" -o -name 'mocks_test.go' -o -name 'mocks_moq_test.go' | xargs -r rm
- rm -rf mocks/

mocks.generate.mockery:
Expand Down
92 changes: 23 additions & 69 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,79 +302,33 @@ Moq-style mocks are far simpler, and probably more intuitive, than mockery-style
|-----|------|-------------|
| `skip-ensure` | `#!yaml bool` | Suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package. |
| `stub-impl` | `#!yaml bool` | Return zero values when no mock implementation is provided, do not panic. |
| `with-resets` | `#!yaml bool` | Generates methods that allow resetting calls made to the mocks. |

### `#!yaml template: "file://`

You may also provide mockery a path to your own file using the `file://` protocol specifier. The string after `file://` will be the relative or absolute path of your template.

## Data Provided To Templates

!!! warning "Construction"

This section is under construction.

mockery configuration makes use of the Go templating system.

### Variables

!!! note
Templated variables are only available when using the `packages` config feature.

Variables that are marked as being templated are capable of using mockery-provided template parameters.

| name | description |
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ConfigDir | The directory path of the config file used. This is used to allow generation of mocks in a directory relative to the `.mockery.yaml` file, e.g. external interfaces. |
| InterfaceDir | The directory path of the original interface being mocked. This can be used as <br>`#!yaml dir: "{{.InterfaceDir}}"` to place your mocks adjacent to the original interface. This should not be used for external interfaces. |
| InterfaceDirRelative | The directory path of the original interface being mocked, relative to the current working directory. If the path cannot be made relative to the current working directory, this variable will be set equal to `PackagePath` |
| InterfaceFile | The file path of the original interface being mocked. **NOTE:** This option will only write one mock implementation to the output file. If multiple mocks are defined in your original file, only one mock will be written to the output. |
| InterfaceName | The name of the original interface being mocked |
| InterfaceNameCamel | Converts a string `interface_name` to `InterfaceName`. <br /><b style="color:var(--md-code-hl-number-color);">DEPRECATED</b>: use `{{ .InterfaceName | camelcase }}` instead |
| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` . <br /><b style="color:var(--md-code-hl-number-color);">DEPRECATED</b>: use `{{ .InterfaceName | camelcase | firstLower }}` instead |
| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` . <br /><b style="color:var(--md-code-hl-number-color);">DEPRECATED</b>: use `{{ .InterfaceName | snakecase }}` instead |
| InterfaceNameLower | Converts `InterfaceName` to `interfacename` . <br /><b style="color:var(--md-code-hl-number-color);">DEPRECATED</b>: use `{{ .InterfaceName | lower }}` instead |
| Mock | A string that is `Mock` if the interface is exported, or `mock` if it is not exported. Useful when setting the name of your mock to something like: <br>`#!yaml mockname: "{{.Mock}}{{.InterfaceName}}"`<br> This way, the mock name will retain the exported-ness of the original interface. |
| MockName | The name of the mock that will be generated. Note that this is simply the `mockname` configuration variable |
| PackageName | The name of the package from the original interface |
| PackagePath | The fully qualified package path of the original interface |

### Functions

!!! note
Templated functions are only available when using the `packages` config feature.

Template functions allow you to inspect and manipulate template variables.

All template functions are calling native Go functions under the hood, so signatures and return values matches the Go functions you are probably already familiar with.

To learn more about the templating syntax, please [see the Go `text/template` documentation](https://pkg.go.dev/text/template)

* [`contains` string substr](https://pkg.go.dev/strings#Contains)
* [`hasPrefix` string prefix](https://pkg.go.dev/strings#HasPrefix)
* [`hasSuffix` string suffix](https://pkg.go.dev/strings#HasSuffix)
* [`join` elems sep](https://pkg.go.dev/strings#Join)
* [`replace` string old new n](https://pkg.go.dev/strings#Replace)
* [`replaceAll` string old new](https://pkg.go.dev/strings#ReplaceAll)
* [`split` string sep](https://pkg.go.dev/strings#Split)
* [`splitAfter` string sep](https://pkg.go.dev/strings#SplitAfter)
* [`splitAfterN` string sep n](https://pkg.go.dev/strings#SplitAfterN)
* [`trim` string cutset](https://pkg.go.dev/strings#Trim)
* [`trimLeft` string cutset](https://pkg.go.dev/strings#TrimLeft)
* [`trimPrefix` string prefix](https://pkg.go.dev/strings#TrimPrefix)
* [`trimRight` string cutset](https://pkg.go.dev/strings#TrimRight)
* [`trimSpace` string](https://pkg.go.dev/strings#TrimSpace)
* [`trimSuffix` string suffix](https://pkg.go.dev/strings#TrimSuffix)
* [`lower` string](https://pkg.go.dev/strings#ToLower)
* [`upper` string](https://pkg.go.dev/strings#ToUpper)
* [`camelcase` string](https://pkg.go.dev/github.com/huandu/xstrings#ToCamelCase)
* [`snakecase` string](https://pkg.go.dev/github.com/huandu/xstrings#ToSnakeCase)
* [`kebabcase` string](https://pkg.go.dev/github.com/huandu/xstrings#ToKebabCase)
* [`firstLower` string](https://pkg.go.dev/github.com/huandu/xstrings#FirstRuneToLower)
* [`firstUpper` string](https://pkg.go.dev/github.com/huandu/xstrings#FirstRuneToUpper)
* [`matchString` pattern](https://pkg.go.dev/regexp#MatchString)
* [`quoteMeta` string](https://pkg.go.dev/regexp#QuoteMeta)
* [`base` string](https://pkg.go.dev/path/filepath#Base)
* [`clean` string](https://pkg.go.dev/path/filepath#Clean)
* [`dir` string](https://pkg.go.dev/path/filepath#Dir)
* [`expandEnv` string](https://pkg.go.dev/os#ExpandEnv)
* [`getenv` string](https://pkg.go.dev/os#Getenv)
Mockery has two separate template instances: one for the `.mockery.yml` file, and one for the mock templates. Each instance has a different set of variables and functions available to it. All functions are [pipeline-compatible](https://pkg.go.dev/text/template#hdr-Pipelines).

### `.mockery.yml`

#### Functions

As seen in the [configuration](configuration.md) section, mockery configuration has template variables and methods available to it. The functions available for use are defined in the [`StringManipulationFuncs`](https://pkg.go.dev/github.com/vektra/mockery/v3/template#pkg-variables).

#### Variables

The variables available are defined in the [`template.ConfigData`](https://pkg.go.dev/github.com/vektra/mockery/v3/template#ConfigData) struct.


### Template Files

#### Functions

Template files have both [`StringManipulationFuncs`](https://pkg.go.dev/github.com/vektra/mockery/v3/template#pkg-variables) and [`TemplateMockFuncs`](https://pkg.go.dev/github.com/vektra/mockery/[email protected]/template#pkg-variables) available as functions.

#### Variables

The template is supplied with the [`template.Data`](https://pkg.go.dev/github.com/vektra/mockery/v3/template#Data) struct. Some attributes return types such as [`template.MockData`](https://pkg.go.dev/github.com/vektra/mockery/[email protected]/template#MockData) and [`registry.Package`](https://pkg.go.dev/github.com/vektra/mockery/v3/registry#Package) which themselves contain methods that may also be called.
12 changes: 1 addition & 11 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,17 +797,7 @@ func (c *Config) ParseTemplates(ctx context.Context, iface *Interface, srcPkg *p
interfaceDirRelative = interfaceDirRelativePath.String()
}
// data is the struct sent to the template parser
data := struct {
ConfigDir string
InterfaceDir string
InterfaceDirRelative string
InterfaceFile string
InterfaceName string
Mock string
MockName string
SrcPackageName string
SrcPackagePath string
}{
data := mockeryTemplate.ConfigData{
ConfigDir: filepath.Dir(c.Config),
InterfaceDir: interfaceDir,
InterfaceDirRelative: interfaceDirRelative,
Expand Down
Loading

0 comments on commit bd61701

Please sign in to comment.