From 0f55ecb59bba3d59affb8261d21d8f3e0cbf335b Mon Sep 17 00:00:00 2001
From: Landon Clipp <11232769+LandonTClipp@users.noreply.github.com>
Date: Tue, 14 Jan 2025 00:17:05 -0600
Subject: [PATCH] Add enhanced deprecation logging support. (#894)
* Add enhanced deprecation logging support.
This commit adds the ability to silence particular deprecation messages. It also
resolved #893.
Add dep warn for `disable-version-string`
Resolves #872.
Add deprecation for `structname`.
Resolves #844.
---
.golangci.yml | 1 -
cmd/mockery.go | 21 ++--
docs/configuration.md | 24 ++--
docs/deprecations.md | 48 +++++++-
docs/stylesheets/extra.css | 14 +++
mkdocs.yml | 10 +-
mockery-tools.env | 2 +-
pkg/config/config.go | 116 ++++++++++++------
.../mock_Interface2WithResolvedAlias_test.go | 2 +-
...mock_Interface2WithUnresolvedAlias_test.go | 2 +-
pkg/generator.go | 9 +-
pkg/logging/logging.go | 46 ++++++-
pkg/outputter.go | 1 +
13 files changed, 212 insertions(+), 84 deletions(-)
diff --git a/.golangci.yml b/.golangci.yml
index 1f3426534..66c67f52a 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -12,7 +12,6 @@ linters:
- ineffassign
- staticcheck
- typecheck
- - contextcheck
- durationcheck
- copyloopvar
- gocheckcompilerdirectives
diff --git a/cmd/mockery.go b/cmd/mockery.go
index 4cccae4ae..f9cb2e154 100644
--- a/cmd/mockery.go
+++ b/cmd/mockery.go
@@ -196,6 +196,10 @@ func (r *RootApp) Run() error {
fmt.Fprintf(os.Stderr, "Failed to initialize logger: %v\n", err)
return err
}
+ logging.DisableDeprecationWarnings = r.Config.DisableDeprecationWarnings
+ logging.DisabledDeprecationWarnings = r.Config.DisabledDeprecationWarnings
+ defer logging.LogDeprecationWarnings()
+
log = log.With().Bool(logging.LogKeyDryRun, r.Config.DryRun).Logger()
log.Info().Msgf("Starting mockery")
log.Info().Msgf("Using config: %s", r.Config.Config)
@@ -228,12 +232,13 @@ func (r *RootApp) Run() error {
boilerplate = string(data)
}
- if !r.Config.WithExpecter {
+ if r.Config.Packages == nil {
logging.WarnDeprecated(
- ctx,
- "with-expecter will be permanently set to True in v3",
+ "packages",
+ "use of the packages config will be the only way to generate mocks in v3. Please migrate your config to use the packages feature.",
map[string]any{
- "url": logging.DocsURL("/deprecations/#with-expecter"),
+ "url": logging.DocsURL("/features/#packages-configuration"),
+ "migration": logging.DocsURL("/migrating_to_packages/"),
},
)
}
@@ -309,14 +314,6 @@ func (r *RootApp) Run() error {
log.Fatal().Msgf("Use --name to specify the name of the interface or --all for all interfaces found")
}
- logging.WarnDeprecated(
- ctx,
- "use of the packages config will be the only way to generate mocks in v3. Please migrate your config to use the packages feature.",
- map[string]any{
- "url": logging.DocsURL("/features/#packages-configuration"),
- "migration": logging.DocsURL("/migrating_to_packages/"),
- })
-
if r.Config.Profile != "" {
f, err := os.Create(r.Config.Profile)
if err != nil {
diff --git a/docs/configuration.md b/docs/configuration.md
index 189081333..d43d427be 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -1,7 +1,7 @@
Configuration
==============
-mockery uses [spf13/viper](https://github.com/spf13/viper) under the hood for its configuration parsing.
+mockery uses [spf13/viper](https://github.com/spf13/viper) under the hood for its configuration parsing.
Merging Precedence
------------------
@@ -64,8 +64,10 @@ Parameter Descriptions
| `config` | :fontawesome-solid-x: | `#!yaml ""` | Set the location of the mockery config file. |
| `dir` | :fontawesome-solid-check: | `#!yaml "mocks/{{.PackagePath}}"` | The directory where the mock file will be outputted to. |
| `disable-config-search` | :fontawesome-solid-x: | `#!yaml false` | Disable searching for configuration files |
+| `disable-deprecation-warnings` | :fontawesome-solid-x: | `#!yaml false` | Disable all warnings for deprecated behavior. |
+| `disabled-deprecation-warnings` | :fontawesome-solid-x: | `#!yaml []` | A list of strings that will selectively disable certain deprecation warnings. The name of each warning is given in the `deprecation-name` attribute of the log message. |
| `disable-func-mocks` | :fontawesome-solid-x: | `#!yaml false` | Disable generation of function mocks. |
-| `disable-version-string` | :fontawesome-solid-x: | `#!yaml false` | Disable the version string in the generated mock files. |
+| `disable-version-string` [:fontawesome-solid-triangle-exclamation:{ .deprecation }](deprecations.md#disable-version-string "Deprecated") | :fontawesome-solid-x: | `#!yaml false` | Disable the version string in the generated mock files. |
| `dry-run` | :fontawesome-solid-x: | `#!yaml false` | Print the actions that would be taken, but don't perform the actions. |
| `exclude` | :fontawesome-solid-x: | `#!yaml []` | Specify subpackages to exclude when using `#!yaml recursive: True` |
| `exclude-regex` | :fontawesome-solid-x: | `#!yaml ""` | When set along with `include-regex`, then interfaces which match `include-regex` but also match `exclude-regex` will not be generated. If `all` is set, or if `include-regex` is not set, then `exclude-regex` has no effect. |
@@ -73,6 +75,7 @@ Parameter Descriptions
| `include-auto-generated` | :fontawesome-solid-x: | `#!yaml true` | Set to `#!yaml false` if you need mockery to skip auto-generated files during its recursive package discovery. When set to `#!yaml true`, mockery includes auto-generated files when determining if a particular directory is an importable package. |
| `include-regex` | :fontawesome-solid-x: | `#!yaml ""` | When set, only interface names that match the expression will be generated. This setting is ignored if `all: True` is specified in the configuration. To further refine the interfaces generated, use `exclude-regex`. |
| `inpackage` | :fontawesome-solid-x: | `#!yaml false` | When generating mocks alongside the original interfaces, you must specify `inpackage: True` to inform mockery that the mock is being placed in the same package as the original interface. |
+| `issue-845-fix` [:fontawesome-solid-triangle-exclamation:{ .deprecation }](deprecations.md#issue-845-fix "Deprecated") | :fontawesome-solid-x: | `#!yaml false` | This fixes a configuration consistency issue found in [issue 845](https://github.com/vektra/mockery/issues/845). |
| `log-level` | :fontawesome-solid-x: | `#!yaml "info"` | Set the level of the logger |
| `mock-build-tags` | :fontawesome-solid-x: | `#!yaml ""` | Set the build tags of the generated mocks. Read more about the [format](https://pkg.go.dev/cmd/go#hdr-Build_constraints). |
| `mockname` | :fontawesome-solid-check: | `#!yaml "Mock{{.InterfaceName}}"` | The name of the generated mock. |
@@ -81,8 +84,9 @@ Parameter Descriptions
| `print` | :fontawesome-solid-x: | `#!yaml false` | Use `print: True` to have the resulting code printed out instead of written to disk. |
| [`recursive`](features.md#recursive-package-discovery) | :fontawesome-solid-x: | `#!yaml false` | When set to `true` on a particular package, mockery will recursively search for all sub-packages and inject those packages into the config map. |
| [`replace-type`](features.md#replace-types) | :fontawesome-solid-x: | `#!yaml null` | Replaces aliases, packages and/or types during generation. |
+| `resolve-type-alias` [:fontawesome-solid-triangle-exclamation:{ .deprecation }](deprecations.md#resolve-type-alias "Deprecated") | :fontawesome-solid-x: | `#!yaml False` | Set to `True` if you would like mockery to resolve type aliases to their underlying type. In most cases, you do not want to resolve type aliases as it can break references to internal/private names. |
| `tags` | :fontawesome-solid-x: | `#!yaml ""` | A space-separated list of additional build tags to load packages. |
-| [`with-expecter`](features.md#expecter-structs) | :fontawesome-solid-x: | `#!yaml true` | Use `with-expecter: True` to generate `EXPECT()` methods for your mocks. This is the preferred way to set up your mocks. |
+| [`with-expecter`](features.md#expecter-structs) [:fontawesome-solid-triangle-exclamation:{ .deprecation }](deprecations.md#with-expecter "Deprecated") | :fontawesome-solid-x: | `#!yaml true` | Use `with-expecter: True` to generate `EXPECT()` methods for your mocks. This is the preferred way to set up your mocks. |
Layouts
-------
@@ -130,7 +134,7 @@ Using different configuration parameters, we can deploy our mocks on-disk in var
}
```
=== "adjacent to interface"
-
+
!!! warning
Mockery does not protect against modifying original source code. Do not generate mocks using this config with uncommitted code changes.
@@ -186,7 +190,7 @@ Using different configuration parameters, we can deploy our mocks on-disk in var
Templated Strings
------------------
-mockery configuration makes use of the Go templating system.
+mockery configuration makes use of the Go templating system.
### Variables
@@ -202,10 +206,10 @@ Variables that are marked as being templated are capable of using mockery-provid
| 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`.
DEPRECATED: use `{{ .InterfaceName | camelcase }}` instead |
-| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` .
DEPRECATED: use `{{ .InterfaceName | camelcase | firstLower }}` instead |
-| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` .
DEPRECATED: use `{{ .InterfaceName | snakecase }}` instead |
-| InterfaceNameLower | Converts `InterfaceName` to `interfacename` .
DEPRECATED: use `{{ .InterfaceName | lower }}` instead |
+| InterfaceNameCamel | Converts a string `interface_name` to `InterfaceName`.