Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fail-on-missing): Allow mockery to return non-zero on missing interfaces #934

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ tasks:

test.e2e:
desc: run end-to-end tests
sources:
- "**/*.go"
- "./.mockery.yaml"
cmds:
- ./e2e/run_all.sh

Expand Down
12 changes: 11 additions & 1 deletion cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/chigopher/pathlib"
"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -289,6 +290,7 @@ func (r *RootApp) Run() error {
// Output interfaces that were specified but not found.
// We do that here and not before the loop because it's easier to
// see for the user.
var foundMissing bool
for _, p := range configuredPackages {
ifaceList, err := r.Config.GetInterfacesForPackage(ctx, p)
if err != nil {
Expand All @@ -297,12 +299,20 @@ func (r *RootApp) Run() error {

for _, name := range ifaceList {
if !parser.Has(p, name) {
log.Warn().Ctx(ctx).
level := zerolog.WarnLevel
if r.Config.FailOnMissing {
level = zerolog.ErrorLevel
}
log.WithLevel(level).Ctx(ctx).
Str(logging.LogKeyInterface, name).
Str(logging.LogKeyQualifiedName, p).
Msg("no such interface")
foundMissing = true
}
}
if foundMissing && r.Config.FailOnMissing {
os.Exit(1)
}
}

return nil
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Parameter Descriptions
| `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. |
| `fail-on-missing`[:fontawesome-solid-triangle-exclamation:{ .deprecation }](deprecations.md#fail-on-missing "Deprecated") | :fontawesome-solid-x: | `#!yaml false` | If set to `#!yaml fail-on-missing: true`, mockery will exit with a non-zero return code if any interfaces defined in config do not exist in the source package. |
| `filename` | :fontawesome-solid-check: | `#!yaml "mock_{{.InterfaceName}}.go"` | The name of the file the mock will reside in. |
| `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`. |
Expand Down
13 changes: 13 additions & 0 deletions docs/deprecations.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
Deprecations
=============

`fail-on-missing`
---------------

!!! tip ""

To resolve this warning:

```yaml title=".mockery.yaml"
fail-on-missing: True
```

This behavior will be permanently set to `true` in v3.

`packages`
----------

Expand Down
12 changes: 7 additions & 5 deletions e2e/run_all.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash
set -e
set +e
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

for file in $(ls $SCRIPT_DIR/test_*.sh); do
for test in $(ls -d $SCRIPT_DIR/test_*); do
file="$test"
if [ -d "$test" ]; then
file="$test/run.sh"
fi
echo "=========="
echo "RUNNING $file"
echo "=========="
go run github.com/go-task/task/v3/cmd/task mocks.remove || exit 1
go run github.com/go-task/task/v3/cmd/task mocks.generate || exit 1
$file
done
done
2 changes: 2 additions & 0 deletions e2e/test_disable_func_mocks.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
go run github.com/go-task/task/v3/cmd/task mocks.remove || exit 1
go run github.com/go-task/task/v3/cmd/task mocks.generate || exit 1

export MOCKERY_CONFIG="e2e/.mockery-disable-func-mock.yaml"
export MOCKERY_LOG_LEVEL="error"
Expand Down
1 change: 1 addition & 0 deletions e2e/test_infinite_mocking.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
# This tests https://github.com/vektra/mockery/issues/632, where
# mockery was generating mocks of its own auto-generated code.
go run github.com/go-task/task/v3/cmd/task mocks || exit 1

# New mocks may legimitately be created, so we run mockery once first
num_files_before=$(find . -type f | wc -l)
Expand Down
5 changes: 5 additions & 0 deletions e2e/test_missing_interface/.mockery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fail-on-missing: true
packages:
github.com/vektra/mockery/v3/internal:
interfaces:
InterfaceDoesntExist:
15 changes: 15 additions & 0 deletions e2e/test_missing_interface/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set +e

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
CONFIG=$SCRIPT_DIR/.mockery.yml
export MOCKERY_CONFIG=$CONFIG

go run github.com/go-task/task/v3/cmd/task mocks.generate

RT=$?
if [ $RT -eq 0 ]; then
echo "ERROR: Expected mockery to fail."
exit 1
fi
echo "SUCCESS: Mockery returned non-zero return code as expected."
1 change: 1 addition & 0 deletions e2e/test_mockery_generation.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
go run github.com/go-task/task/v3/cmd/task mocks || exit 1

go run github.com/go-task/task/v3/cmd/task mocks.generate
rt=$?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
# This tests https://github.com/vektra/mockery/pull/682
go run github.com/go-task/task/v3/cmd/task mocks || exit 1

FILE="pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go"
if ! [ -f $FILE ]; then
Expand Down
2 changes: 1 addition & 1 deletion mockery-tools.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=v2.52.4
VERSION=v2.53.0
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Config struct {
Exclude []string `mapstructure:"exclude"`
ExcludeRegex string `mapstructure:"exclude-regex"`
Exported bool `mapstructure:"exported"`
FailOnMissing bool `mapstructure:"fail-on-missing"`
FileName string `mapstructure:"filename"`
InPackage bool `mapstructure:"inpackage"`
InPackageSuffix bool `mapstructure:"inpackage-suffix"`
Expand Down