Skip to content

Commit 012e2ea

Browse files
authored
feat(fail-on-missing): Allow mockery to return non-zero on missing interfaces (#934)
Closes #924
1 parent ae7c777 commit 012e2ea

13 files changed

+59
-10
lines changed

Taskfile.yml

-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ tasks:
6565

6666
test.e2e:
6767
desc: run end-to-end tests
68-
sources:
69-
- "**/*.go"
70-
- "./.mockery.yaml"
7168
cmds:
7269
- ./e2e/run_all.sh
7370

cmd/mockery.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

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

298300
for _, name := range ifaceList {
299301
if !parser.Has(p, name) {
300-
log.Warn().Ctx(ctx).
302+
level := zerolog.WarnLevel
303+
if r.Config.FailOnMissing {
304+
level = zerolog.ErrorLevel
305+
}
306+
log.WithLevel(level).Ctx(ctx).
301307
Str(logging.LogKeyInterface, name).
302308
Str(logging.LogKeyQualifiedName, p).
303309
Msg("no such interface")
310+
foundMissing = true
304311
}
305312
}
313+
if foundMissing && r.Config.FailOnMissing {
314+
os.Exit(1)
315+
}
306316
}
307317

308318
return nil

docs/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Parameter Descriptions
7171
| `dry-run` | :fontawesome-solid-x: | `#!yaml false` | Print the actions that would be taken, but don't perform the actions. |
7272
| `exclude` | :fontawesome-solid-x: | `#!yaml []` | Specify subpackages to exclude when using `#!yaml recursive: True` |
7373
| `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. |
74+
| `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. |
7475
| `filename` | :fontawesome-solid-check: | `#!yaml "mock_{{.InterfaceName}}.go"` | The name of the file the mock will reside in. |
7576
| `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. |
7677
| `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`. |

docs/deprecations.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
Deprecations
22
=============
33

4+
`fail-on-missing`
5+
---------------
6+
7+
!!! tip ""
8+
9+
To resolve this warning:
10+
11+
```yaml title=".mockery.yaml"
12+
fail-on-missing: True
13+
```
14+
15+
This behavior will be permanently set to `true` in v3.
16+
417
`packages`
518
----------
619

e2e/run_all.sh

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
2-
set -e
2+
set +e
33
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
44

5-
for file in $(ls $SCRIPT_DIR/test_*.sh); do
5+
for test in $(ls -d $SCRIPT_DIR/test_*); do
6+
file="$test"
7+
if [ -d "$test" ]; then
8+
file="$test/run.sh"
9+
fi
610
echo "=========="
711
echo "RUNNING $file"
812
echo "=========="
9-
go run github.com/go-task/task/v3/cmd/task mocks.remove || exit 1
10-
go run github.com/go-task/task/v3/cmd/task mocks.generate || exit 1
1113
$file
12-
done
14+
done

e2e/test_disable_func_mocks.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/bin/bash
2+
go run github.com/go-task/task/v3/cmd/task mocks.remove || exit 1
3+
go run github.com/go-task/task/v3/cmd/task mocks.generate || exit 1
24

35
export MOCKERY_CONFIG="e2e/.mockery-disable-func-mock.yaml"
46
export MOCKERY_LOG_LEVEL="error"

e2e/test_infinite_mocking.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22
# This tests https://github.com/vektra/mockery/issues/632, where
33
# mockery was generating mocks of its own auto-generated code.
4+
go run github.com/go-task/task/v3/cmd/task mocks || exit 1
45

56
# New mocks may legimitately be created, so we run mockery once first
67
num_files_before=$(find . -type f | wc -l)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fail-on-missing: true
2+
packages:
3+
github.com/vektra/mockery/v3/internal:
4+
interfaces:
5+
InterfaceDoesntExist:

e2e/test_missing_interface/run.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set +e
3+
4+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
5+
CONFIG=$SCRIPT_DIR/.mockery.yml
6+
export MOCKERY_CONFIG=$CONFIG
7+
8+
go run github.com/go-task/task/v3/cmd/task mocks.generate
9+
10+
RT=$?
11+
if [ $RT -eq 0 ]; then
12+
echo "ERROR: Expected mockery to fail."
13+
exit 1
14+
fi
15+
echo "SUCCESS: Mockery returned non-zero return code as expected."

e2e/test_mockery_generation.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
go run github.com/go-task/task/v3/cmd/task mocks || exit 1
23

34
go run github.com/go-task/task/v3/cmd/task mocks.generate
45
rt=$?

e2e/test_recursive_package_with_only_autogenerated_files.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22
# This tests https://github.com/vektra/mockery/pull/682
3+
go run github.com/go-task/task/v3/cmd/task mocks || exit 1
34

45
FILE="pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go"
56
if ! [ -f $FILE ]; then

mockery-tools.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION=v2.52.4
1+
VERSION=v2.53.0

pkg/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Config struct {
4949
Exclude []string `mapstructure:"exclude"`
5050
ExcludeRegex string `mapstructure:"exclude-regex"`
5151
Exported bool `mapstructure:"exported"`
52+
FailOnMissing bool `mapstructure:"fail-on-missing"`
5253
FileName string `mapstructure:"filename"`
5354
InPackage bool `mapstructure:"inpackage"`
5455
InPackageSuffix bool `mapstructure:"inpackage-suffix"`

0 commit comments

Comments
 (0)