From ee33b3deeb77bd60c9407dcfbdaea6e788c01924 Mon Sep 17 00:00:00 2001 From: Rangel Reale Date: Sat, 23 Sep 2023 09:42:34 -0300 Subject: [PATCH 01/30] - take type in account when using import replace types - check more specific replace types first --- .../example_project/replace_type/README.md | 9 +++++ .../example_project/replace_type/rt.go | 11 ++++++ .../replace_type/rti/internal/rti.go | 9 +++++ .../replace_type/rti/rt1/rt1.go | 5 +++ .../replace_type/rti/rt2/rt2.go | 5 +++ pkg/generator.go | 23 ++++++----- pkg/generator_test.go | 38 +++++++++++++++++++ 7 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 pkg/fixtures/example_project/replace_type/README.md create mode 100644 pkg/fixtures/example_project/replace_type/rt.go create mode 100644 pkg/fixtures/example_project/replace_type/rti/internal/rti.go create mode 100644 pkg/fixtures/example_project/replace_type/rti/rt1/rt1.go create mode 100644 pkg/fixtures/example_project/replace_type/rti/rt2/rt2.go diff --git a/pkg/fixtures/example_project/replace_type/README.md b/pkg/fixtures/example_project/replace_type/README.md new file mode 100644 index 00000000..3ce60077 --- /dev/null +++ b/pkg/fixtures/example_project/replace_type/README.md @@ -0,0 +1,9 @@ +## Fix replace-type for different packages from the same source + +[Issue 710](https://github.com/vektra/mockery/pull/710) + +This package is used to test the case where multiple types come from the same package (`replace_type/rti/internal`), +but results in types in different packages (`replace_type/rt1` and `replace_type/rt2`). + +Tests `TestReplaceTypePackageMultiplePrologue` and `TestReplaceTypePackageMultiple` use it to check if this outputs +the correct import and type names. \ No newline at end of file diff --git a/pkg/fixtures/example_project/replace_type/rt.go b/pkg/fixtures/example_project/replace_type/rt.go new file mode 100644 index 00000000..ccd9679a --- /dev/null +++ b/pkg/fixtures/example_project/replace_type/rt.go @@ -0,0 +1,11 @@ +package replace_type + +import ( + "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt1" + "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt2" +) + +type RType interface { + Replace1(f rt1.RType1) + Replace2(f rt2.RType2) +} diff --git a/pkg/fixtures/example_project/replace_type/rti/internal/rti.go b/pkg/fixtures/example_project/replace_type/rti/internal/rti.go new file mode 100644 index 00000000..69e2a56e --- /dev/null +++ b/pkg/fixtures/example_project/replace_type/rti/internal/rti.go @@ -0,0 +1,9 @@ +package internal + +type RTInternal1 struct { + A int +} + +type RTInternal2 struct { + B string +} diff --git a/pkg/fixtures/example_project/replace_type/rti/rt1/rt1.go b/pkg/fixtures/example_project/replace_type/rti/rt1/rt1.go new file mode 100644 index 00000000..42ce24c0 --- /dev/null +++ b/pkg/fixtures/example_project/replace_type/rti/rt1/rt1.go @@ -0,0 +1,5 @@ +package rt1 + +import "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/internal" + +type RType1 = internal.RTInternal1 diff --git a/pkg/fixtures/example_project/replace_type/rti/rt2/rt2.go b/pkg/fixtures/example_project/replace_type/rti/rt2/rt2.go new file mode 100644 index 00000000..d08e3194 --- /dev/null +++ b/pkg/fixtures/example_project/replace_type/rti/rt2/rt2.go @@ -0,0 +1,5 @@ +package rt2 + +import "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/internal" + +type RType2 = internal.RTInternal2 diff --git a/pkg/generator.go b/pkg/generator.go index cd3df88a..8f5985a6 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -103,7 +103,7 @@ func NewGenerator(ctx context.Context, c GeneratorConfig, iface *Interface, pkg } g.parseReplaceTypes(ctx) - g.addPackageImportWithName(ctx, "github.com/stretchr/testify/mock", "mock") + g.addPackageImportWithName(ctx, "github.com/stretchr/testify/mock", "mock", nil) return g } @@ -161,7 +161,7 @@ func (g *Generator) getPackageScopedType(ctx context.Context, o *types.TypeName) (!g.config.KeepTree && g.config.InPackage && o.Pkg() == g.iface.Pkg) { return o.Name() } - pkg := g.addPackageImport(ctx, o.Pkg()) + pkg := g.addPackageImport(ctx, o.Pkg(), o) name := o.Name() g.checkReplaceType(ctx, func(from *replaceType, to *replaceType) bool { if o.Pkg().Path() == from.pkg && name == from.typ { @@ -173,23 +173,28 @@ func (g *Generator) getPackageScopedType(ctx context.Context, o *types.TypeName) return pkg + "." + name } -func (g *Generator) addPackageImport(ctx context.Context, pkg *types.Package) string { - return g.addPackageImportWithName(ctx, pkg.Path(), pkg.Name()) +func (g *Generator) addPackageImport(ctx context.Context, pkg *types.Package, o *types.TypeName) string { + return g.addPackageImportWithName(ctx, pkg.Path(), pkg.Name(), o) } func (g *Generator) checkReplaceType(ctx context.Context, f func(from *replaceType, to *replaceType) bool) { - for _, item := range g.replaceTypeCache { - if !f(item.from, item.to) { - break + // check most specific first + for _, hasType := range []bool{true, false} { + for _, item := range g.replaceTypeCache { + if (item.from.typ != "") == hasType { + if !f(item.from, item.to) { + break + } + } } } } -func (g *Generator) addPackageImportWithName(ctx context.Context, path, name string) string { +func (g *Generator) addPackageImportWithName(ctx context.Context, path, name string, o *types.TypeName) string { log := zerolog.Ctx(ctx) replaced := false g.checkReplaceType(ctx, func(from *replaceType, to *replaceType) bool { - if path == from.pkg { + if o != nil && path == from.pkg && (from.typ == "" || o.Name() == from.typ) { log.Debug().Str("from", path).Str("to", to.pkg).Msg("changing package path") replaced = true path = to.pkg diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 492ef7e5..5ea54b5b 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -711,6 +711,44 @@ func (s *GeneratorSuite) TestReplaceTypePackage() { }) } +func (s *GeneratorSuite) TestReplaceTypePackageMultiplePrologue() { + expected := `package mocks + +import mock "github.com/stretchr/testify/mock" +import replace_type "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type" +import rt1 "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt1" +import rt2 "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt2" + +` + generator := NewGenerator( + s.ctx, + GeneratorConfig{InPackage: false, ReplaceType: []string{ + "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/internal.RTInternal1=rt1:github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt1.RType1", + "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/internal.RTInternal2=rt2:github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt2.RType2", + }}, + s.getInterfaceFromFile("example_project/replace_type/rt.go", "RType"), + pkg, + ) + + s.checkPrologueGeneration(generator, expected) +} + +func (s *GeneratorSuite) TestReplaceTypePackageMultiple() { + cfg := GeneratorConfig{InPackage: false, ReplaceType: []string{ + // first one will be ignored by the more specific ones + "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/internal=fiz3:github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt3", + "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/internal.RTInternal1=rt1:github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt1.RType1", + "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/internal.RTInternal2=rt2:github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt2.RType2", + }} + + s.checkGenerationRegexWithConfig("example_project/replace_type/rt.go", "RType", cfg, []regexpExpected{ + // func (_m *RType) Replace1(f rt1.RType1) + {true, regexp.MustCompile(`func \([^\)]+\) Replace1\(f rt1\.RType1`)}, + // func (_m *RType) Replace2(f rt2.RType2) + {true, regexp.MustCompile(`func \([^\)]+\) Replace2\(f rt2\.RType2`)}, + }) +} + func (s *GeneratorSuite) TestGenericGenerator() { s.checkGeneration("generic.go", "RequesterGenerics", false, "", "") } From 9d79f3a94765789bba51ea3a4d0cf5f77184c3a3 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Mon, 9 Oct 2023 09:46:17 -0500 Subject: [PATCH 02/30] fix docs --- docs/configuration.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 36a3c6e7..cf391311 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -45,7 +45,7 @@ packages: mockery will search upwards from your current-working-directory up to the root path, so the same configuration should be able to follow you within your project. -See the [`features` section](features.md#examples) for more details on how the config is structuvar(--md-code-hl-number-color). +See the [`features` section](features.md#examples) for more details on how the config is structured. Parameter Descriptions ----------------------- @@ -69,7 +69,7 @@ Parameter Descriptions | `exclude` | :fontawesome-solid-x: | `#!yaml []` | Specify subpackages to exclude when using `#!yaml recursive: True` | | `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 ignovar(--md-code-hl-number-color) if `all: True` is specified in the configuration | +| `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 | | `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. | | `mockname` | :fontawesome-solid-check: | `#!yaml "Mock{{.InterfaceName}}"` | The name of the generated mock. | | `outpkg` | :fontawesome-solid-check: | `#!yaml "{{.PackageName}}"` | Use `outpkg` to specify the package name of the generated mocks. | @@ -78,7 +78,7 @@ 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. | | `tags` | :fontawesome-solid-x: | `#!yaml ""` | Set the build tags of the generated mocks. | -| [`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 prefervar(--md-code-hl-number-color) way to setup your mocks. | +| [`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 setup your mocks. | | [`replace-type`](features.md#replace-types) | :fontawesome-solid-x: | `#!yaml null` | Replaces aliases, packages and/or types during generation. | Layouts From 5d48467aa7d5882ae48a74379aaa41546af450a0 Mon Sep 17 00:00:00 2001 From: Kristian Bolino Date: Mon, 9 Oct 2023 16:56:31 -0400 Subject: [PATCH 03/30] Add exclude-regex to config Addresses https://github.com/vektra/mockery/issues/718 --- docs/configuration.md | 3 +- docs/features.md | 15 ++++++- pkg/config/config.go | 27 ++++++++++-- pkg/config/config_test.go | 91 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 127 insertions(+), 9 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index cf391311..473b9b02 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -67,9 +67,10 @@ Parameter Descriptions | `disable-version-string` | :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. | | `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 | +| `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. | | `mockname` | :fontawesome-solid-check: | `#!yaml "Mock{{.InterfaceName}}"` | The name of the generated mock. | | `outpkg` | :fontawesome-solid-check: | `#!yaml "{{.PackageName}}"` | Use `outpkg` to specify the package name of the generated mocks. | diff --git a/docs/features.md b/docs/features.md index 748fc44c..979b03b6 100644 --- a/docs/features.md +++ b/docs/features.md @@ -193,8 +193,21 @@ packages: include-regex: ".*Client" ``` +To further refine matched interfaces, you can also use `exclude-regex`. If an interface matches both `include-regex` and `exclude-regex` then it will not be generated. For example, to generate all interfaces except those ending in `Func`: + +```yaml +packages: + github.com/user/project: + config: + recursive: true + include-regex: ".*" + exclude-regex: ".*Func" +``` + +You can only use `exclude-regex` with `include-regex`. If set by itself, `exclude-regex` has no effect. + ??? note "all: true" - Using `all: true` will override `include-regex` and issue a warning. + Using `all: true` will override `include-regex` (and `exclude-regex`) and issue a warning. Mock Constructors ----------------- diff --git a/pkg/config/config.go b/pkg/config/config.go index 86439929..2b4f205b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -41,6 +41,7 @@ type Config struct { DisableConfigSearch bool `mapstructure:"disable-config-search"` DisableVersionString bool `mapstructure:"disable-version-string"` DryRun bool `mapstructure:"dry-run"` + ExcludeRegex string `mapstructure:"exclude-regex"` Exported bool `mapstructure:"exported"` FileName string `mapstructure:"filename"` IncludeAutoGenerated bool `mapstructure:"include-auto-generated"` @@ -271,19 +272,37 @@ func (c *Config) ShouldGenerateInterface(ctx context.Context, packageName, inter } _, interfaceExists := interfacesSection[interfaceName] - var matchedByRegex bool + matchedByRegex := false if pkgConfig.IncludeRegex != "" { if pkgConfig.All { log := zerolog.Ctx(ctx) - log.Warn().Msg("interface config has both `all` and `include-regex` set. `include-regex` will be ignored") + log.Warn().Msg("interface config has both `all` and `include-regex` set: `include-regex` will be ignored") } else { matchedByRegex, err = regexp.MatchString(pkgConfig.IncludeRegex, interfaceName) if err != nil { - return false, err + return false, fmt.Errorf("evaluating `include-regex`: %w", err) } } } - return pkgConfig.All || interfaceExists || matchedByRegex, nil + excludedByRegex := false + if pkgConfig.ExcludeRegex != "" { + if pkgConfig.All { + log := zerolog.Ctx(ctx) + log.Warn().Msg("interface config has both `all` and `exclude-regex` set: `exclude-regex` will be ignored") + } else if pkgConfig.IncludeRegex == "" { + log := zerolog.Ctx(ctx) + log.Warn().Msg("interface config has `exclude-regex` set but not `include-regex`: `exclude-regex` will be ignored") + } else { + excludedByRegex, err = regexp.MatchString(pkgConfig.ExcludeRegex, interfaceName) + if err != nil { + return false, fmt.Errorf("evaluating `exclude-regex`: %w", err) + } + if excludedByRegex { + matchedByRegex = false + } + } + } + return pkgConfig.All || interfaceExists || (matchedByRegex && !excludedByRegex), nil } func (c *Config) getInterfacesSection(ctx context.Context, packageName string) (map[string]any, error) { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 2e151e07..b5758052 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -609,7 +609,7 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) { want: true, }, { - name: "should generate using included-regex", + name: "should generate using include-regex", c: &Config{ Packages: map[string]interface{}{ "some_package": map[string]interface{}{ @@ -622,7 +622,7 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) { want: true, }, { - name: "should generate when using all and included-regex doesn't match", + name: "should generate when using all and include-regex doesn't match", c: &Config{ Packages: map[string]interface{}{ "some_package": map[string]interface{}{ @@ -636,7 +636,7 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) { want: true, }, { - name: "should not generate when included-regex doesn't match", + name: "should not generate when include-regex doesn't match", c: &Config{ Packages: map[string]interface{}{ "some_package": map[string]interface{}{ @@ -648,6 +648,91 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) { }, want: false, }, + { + name: "should not generate when include-regex and exclude-regex both match", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "include-regex": ".*Interface", + "exclude-regex": "Some.*", + }, + }, + }, + }, + want: false, + }, + { + name: "should generate when include-regex matches but not exclude-regex", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "include-regex": ".*Interface", + "exclude-regex": "Foo.*", + }, + }, + }, + }, + want: true, + }, + { + name: "should not generate when neither include-regex nor exclude-regex match", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "include-regex": ".*XInterface", + "exclude-regex": "Foo.*", + }, + }, + }, + }, + want: false, + }, + { + name: "should not generate when exclude-regex doesn't match but include-regex isn't set", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "exclude-regex": "Foo.*", + }, + }, + }, + }, + want: false, + }, + { + name: "should generate when using all and exclude-regex matches", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "all": true, + "exclude-regex": ".*Interface", + }, + }, + }, + }, + want: true, + }, + { + name: "should generate when interface is selected and exclude-regex matches", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "interfaces": map[string]interface{}{ + "SomeInterface": struct{}{}, + }, + "config": map[string]interface{}{ + "exclude-regex": ".*Interface", + }, + }, + }, + }, + want: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 8939c75f51783166286c150e25a50aa8f8b9037c Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Wed, 11 Oct 2023 11:47:27 -0500 Subject: [PATCH 04/30] Update release to use Go 1.21 --- .github/workflows/release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d11058e3..88055b68 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,9 +17,9 @@ jobs: fetch-depth: 0 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.21' - name: Set up QEMU uses: docker/setup-qemu-action@v2 From e8ebf521445ed27e5113c99cc4e2d5bcd85d93fa Mon Sep 17 00:00:00 2001 From: Kristian Bolino Date: Fri, 13 Oct 2023 10:36:22 -0400 Subject: [PATCH 05/30] Refactor Config.ShouldGenerateInterface PR https://github.com/vektra/mockery/pull/720 --- pkg/config/config.go | 66 ++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 2b4f205b..dc46aad6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -263,46 +263,52 @@ func (c *Config) ExcludePath(path string) bool { func (c *Config) ShouldGenerateInterface(ctx context.Context, packageName, interfaceName string) (bool, error) { pkgConfig, err := c.GetPackageConfig(ctx, packageName) if err != nil { - return false, err + return false, fmt.Errorf("getting package config: %w", err) + } + + log := zerolog.Ctx(ctx) + if pkgConfig.All { + if pkgConfig.IncludeRegex != "" { + log.Warn().Msg("interface config has both `all` and `include-regex` set: `include-regex` will be ignored") + } + if pkgConfig.ExcludeRegex != "" { + log.Warn().Msg("interface config has both `all` and `exclude-regex` set: `exclude-regex` will be ignored") + } + return true, nil } interfacesSection, err := c.getInterfacesSection(ctx, packageName) if err != nil { - return false, err + return false, fmt.Errorf("getting interfaces section: %w", err) } _, interfaceExists := interfacesSection[interfaceName] - - matchedByRegex := false - if pkgConfig.IncludeRegex != "" { - if pkgConfig.All { - log := zerolog.Ctx(ctx) - log.Warn().Msg("interface config has both `all` and `include-regex` set: `include-regex` will be ignored") - } else { - matchedByRegex, err = regexp.MatchString(pkgConfig.IncludeRegex, interfaceName) - if err != nil { - return false, fmt.Errorf("evaluating `include-regex`: %w", err) - } - } + if interfaceExists { + return true, nil } - excludedByRegex := false - if pkgConfig.ExcludeRegex != "" { - if pkgConfig.All { - log := zerolog.Ctx(ctx) - log.Warn().Msg("interface config has both `all` and `exclude-regex` set: `exclude-regex` will be ignored") - } else if pkgConfig.IncludeRegex == "" { - log := zerolog.Ctx(ctx) + + includeRegex := pkgConfig.IncludeRegex + excludeRegex := pkgConfig.ExcludeRegex + if includeRegex == "" { + if excludeRegex != "" { log.Warn().Msg("interface config has `exclude-regex` set but not `include-regex`: `exclude-regex` will be ignored") - } else { - excludedByRegex, err = regexp.MatchString(pkgConfig.ExcludeRegex, interfaceName) - if err != nil { - return false, fmt.Errorf("evaluating `exclude-regex`: %w", err) - } - if excludedByRegex { - matchedByRegex = false - } } + return false, nil + } + includedByRegex, err := regexp.MatchString(includeRegex, interfaceName) + if err != nil { + return false, fmt.Errorf("evaluating `include-regex`: %w", err) + } + if !includedByRegex { + return false, nil + } + if excludeRegex == "" { + return true, nil + } + excludedByRegex, err := regexp.MatchString(excludeRegex, interfaceName) + if err != nil { + return false, fmt.Errorf("evaluating `exclude-regex`: %w", err) } - return pkgConfig.All || interfaceExists || (matchedByRegex && !excludedByRegex), nil + return !excludedByRegex, nil } func (c *Config) getInterfacesSection(ctx context.Context, packageName string) (map[string]any, error) { From 25befa273a013f6eabf78666786f1234bf4c6f0a Mon Sep 17 00:00:00 2001 From: Kristian Bolino Date: Fri, 13 Oct 2023 11:07:33 -0400 Subject: [PATCH 06/30] Cover error conditions in tests PR https://github.com/vektra/mockery/pull/720 --- pkg/config/config.go | 6 ++- pkg/config/config_test.go | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index dc46aad6..3747da20 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -320,7 +320,11 @@ func (c *Config) getInterfacesSection(ctx context.Context, packageName string) ( if !exists { return make(map[string]any), nil } - return interfaceSection.(map[string]any), nil + mapConfig, ok := interfaceSection.(map[string]any) + if !ok { + return nil, fmt.Errorf("interfaces section has type %T, expected map[string]any", interfaceSection) + } + return mapConfig, nil } func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, interfaceName string) ([]*Config, error) { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index b5758052..51550bf3 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -572,6 +572,17 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) { want: false, wantErr: true, }, + { + name: "invalid interfaces section returns error", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "interfaces": true, + }, + }, + }, + wantErr: true, + }, { name: "should generate all interfaces", c: &Config{ @@ -733,6 +744,75 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) { }, want: true, }, + { + name: "invalid include-regex is ignored if all is set", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "all": true, + "include-regex": "[", + }, + }, + }, + }, + want: true, + }, + { + name: "invalid include-regex results in error", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "include-regex": "[", + }, + }, + }, + }, + wantErr: true, + }, + { + name: "invalid exclude-regex is ignored if all is set", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "all": true, + "include-regex": ".*", + "exclude-regex": "[", + }, + }, + }, + }, + want: true, + }, + { + name: "invalid exclude-regex is ignored if include-regex is not set", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "exclude-regex": "[", + }, + }, + }, + }, + want: false, + }, + { + name: "invalid exclude-regex results in error", + c: &Config{ + Packages: map[string]interface{}{ + "some_package": map[string]interface{}{ + "config": map[string]interface{}{ + "include-regex": ".*", + "exclude-regex": "[", + }, + }, + }, + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 726d76c3c39b5dec350c57fa294a3615e208a547 Mon Sep 17 00:00:00 2001 From: Landon Clipp Date: Wed, 25 Oct 2023 17:32:04 -0500 Subject: [PATCH 07/30] Update running.md --- docs/running.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/running.md b/docs/running.md index 1f0a5180..4f913307 100644 --- a/docs/running.md +++ b/docs/running.md @@ -27,4 +27,4 @@ mockery ``` !!! question "Command line arguments" - It is valid to specify arguments from the command line. The configuration precedence is specified in the [Configuration](configuration.md#merging-precedence) docs. \ No newline at end of file + It is valid to specify arguments from the command line. The configuration precedence is specified in the [Configuration](configuration.md#merging-precedence) docs. From 77064ad361c96322dee67c16bebb30e8a75e1452 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sat, 4 Nov 2023 19:09:43 -0500 Subject: [PATCH 08/30] Fix config bug where mockery crashes when package map is nil Fixes issue #726. We needed an additional bit of logic to ensure that if the `config` section is nil that we default it to being an empty map. --- pkg/config/config.go | 21 ++++++++++++++------- pkg/config/config_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 3747da20..8206bcb1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -209,6 +209,7 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m if isMap { return configAsMap, nil } + return map[string]any{}, nil } @@ -430,7 +431,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP Str("parent-package", parentPkgPath). Str("sub-package", subPkgPath).Logger() - log.Trace().Msg("adding sub-package to config map") + log.Debug().Msg("adding sub-package to config map") parentPkgConfig, err := c.getPackageConfigMap(ctx, parentPkgPath) if err != nil { log.Err(err). @@ -438,13 +439,13 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP return fmt.Errorf("failed to get package config: %w", err) } - log.Trace().Msg("getting config") + log.Debug().Msg("getting config") cfg, err := c.CfgAsMap(ctx) if err != nil { return fmt.Errorf("failed to get configuration map: %w", err) } - log.Trace().Msg("getting packages section") + log.Debug().Msg("getting packages section") packagesSection := cfg["packages"].(map[string]any) // Don't overwrite any config that already exists @@ -688,15 +689,21 @@ func (c *Config) mergeInConfig(ctx context.Context) error { packageConfig["config"] = defaultCfg continue } - packageConfigSection := configSectionUntyped.(map[string]any) + // Sometimes the config section may be provided, but it's nil. + // We need to account for this fact. + if configSectionUntyped == nil { + configSectionUntyped = map[string]any{} + } + + configSectionTyped := configSectionUntyped.(map[string]any) for key, value := range defaultCfg { if contains([]string{"packages", "config"}, key) { continue } - _, keyExists := packageConfigSection[key] + _, keyExists := configSectionTyped[key] if !keyExists { - packageConfigSection[key] = value + configSectionTyped[key] = value } } interfaces, err := c.getInterfacesForPackage(ctx, pkgPath) @@ -728,7 +735,7 @@ func (c *Config) mergeInConfig(ctx context.Context) error { // Assume this interface's value in the map is nil. Just skip it. continue } - for key, value := range packageConfigSection { + for key, value := range configSectionTyped { if key == "packages" { continue } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 51550bf3..580c1fb7 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1172,6 +1172,33 @@ packages: recursive: true with-expecter: true with-expecter: false +`, + }, + { + name: "empty map for recursive package", + cfgYaml: ` +with-expecter: False +dir: foobar +recursive: True +all: True +packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs: +`, + wantCfgMap: `dir: foobar +packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2: + config: + all: true + dir: foobar + recursive: true + with-expecter: true + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2/subpkg3: + config: + all: true + dir: foobar + recursive: true + with-expecter: true +with-expecter: false `, }, } From 0310201c86af4a29e14b24107710dbfdffb9e53a Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sat, 4 Nov 2023 21:41:23 -0500 Subject: [PATCH 09/30] Add fix for showconfig command This commit adds a fix where we create a copy of the top-level config map, instead of using it directly. Previously this situation caused an infinite loop in the map structure, so creating a copy prevents that from happening. --- cmd/showconfig.go | 10 ++++++---- pkg/config/config.go | 39 +++++++++++++++++++++++++++++++++++---- pkg/config/config_test.go | 6 +++++- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/cmd/showconfig.go b/cmd/showconfig.go index 99c47b0c..d87844b2 100644 --- a/cmd/showconfig.go +++ b/cmd/showconfig.go @@ -37,6 +37,11 @@ func showConfig( if err != nil { return stackerr.NewStackErrf(err, "failed to unmarshal config") } + log, err := logging.GetLogger(config.LogLevel) + if err != nil { + return fmt.Errorf("getting logger: %w", err) + } + ctx = log.WithContext(ctx) if err := config.Initialize(ctx); err != nil { return err } @@ -48,10 +53,7 @@ func showConfig( if err != nil { return stackerr.NewStackErrf(err, "failed to marshal yaml") } - log, err := logging.GetLogger(config.LogLevel) - if err != nil { - panic(err) - } + log.Info().Msgf("Using config: %s", config.Config) fmt.Fprintf(outputter, "%s", string(out)) diff --git a/pkg/config/config.go b/pkg/config/config.go index 8206bcb1..7899fd9c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -210,7 +210,11 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m return configAsMap, nil } - return map[string]any{}, nil + // Package is something other than map, so set its value to an + // empty map. + emptyMap := map[string]any{} + packageSection[packageName] = emptyMap + return emptyMap, nil } func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Config, error) { @@ -659,6 +663,17 @@ func contains[T comparable](slice []T, elem T) bool { return false } +func deepCopyConfigMap(src map[string]any) map[string]any { + new := map[string]any{} + for key, val := range src { + if contains([]string{"packages", "config", "interfaces"}, key) { + continue + } + new[key] = val + } + return new +} + // mergeInConfig takes care of merging inheritable configuration // in the config map. For example, it merges default config, then // package-level config, then interface-level config. @@ -684,15 +699,28 @@ func (c *Config) mergeInConfig(ctx context.Context) error { pkgLog.Err(err).Msg("failed to get package config") return fmt.Errorf("failed to get package config: %w", err) } + pkgLog.Trace().Msg("got package config map") + configSectionUntyped, configExists := packageConfig["config"] if !configExists { - packageConfig["config"] = defaultCfg - continue + pkgLog.Trace().Msg("config section doesn't exist, setting it to a deepcopy of the top-level config") + packageConfig["config"] = deepCopyConfigMap(defaultCfg) } + + pkgLog.Trace().Msg("got config section for package") // Sometimes the config section may be provided, but it's nil. // We need to account for this fact. if configSectionUntyped == nil { - configSectionUntyped = map[string]any{} + pkgLog.Trace().Msg("config section is nil, converting to empty map") + emptyMap := map[string]any{} + + // We need to add this to the "global" config mapping so the change + // gets persisted, and also into configSectionUntyped for the logic + // further down. + packageConfig["config"] = emptyMap + configSectionUntyped = emptyMap + } else { + pkgLog.Trace().Msg("config section is not nil") } configSectionTyped := configSectionUntyped.(map[string]any) @@ -701,8 +729,11 @@ func (c *Config) mergeInConfig(ctx context.Context) error { if contains([]string{"packages", "config"}, key) { continue } + keyValLog := pkgLog.With().Str("key", key).Str("value", fmt.Sprintf("%v", value)).Logger() + _, keyExists := configSectionTyped[key] if !keyExists { + keyValLog.Trace().Msg("setting key to value") configSectionTyped[key] = value } } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 580c1fb7..5a7b654e 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1204,6 +1204,7 @@ with-expecter: false } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() tmpdir := pathlib.NewPath(t.TempDir()) cfg := tmpdir.Join("config.yaml") require.NoError(t, cfg.WriteFile([]byte(tt.cfgYaml))) @@ -1218,7 +1219,10 @@ with-expecter: false t.Errorf("Config.Initialize() error = %v, wantErr %v", err, tt.wantErr) } - cfgAsStr, err := yaml.Marshal(c._cfgAsMap) + cfgAsMap, err := c.CfgAsMap(ctx) + require.NoError(t, err) + + cfgAsStr, err := yaml.Marshal(cfgAsMap) require.NoError(t, err) if tt.wantCfgMap != "" && !reflect.DeepEqual(string(cfgAsStr), tt.wantCfgMap) { From 5978bc5b7f58ea3a3dd8c06d5271ee45cc15f739 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sun, 5 Nov 2023 20:24:16 -0600 Subject: [PATCH 10/30] Fix test with config initialization The test wasn't properly initializing the viper object so the config wasn't getting decoded into the struct properly. Also adding more comments to various places to better explain what the code was doing, as even I, the person who wrote it, was confused as to what I was doing. How can I expect other people to understand it if I can't?! --- pkg/config/config.go | 28 +++++++++++++++++++++------- pkg/config/config_test.go | 23 ++++++++++++++--------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 7899fd9c..1682231f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -218,7 +218,7 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m } func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Config, error) { - log := zerolog.Ctx(ctx) + log := zerolog.Ctx(ctx).With().Str("package-path", packageName).Logger() if c.pkgConfigCache == nil { log.Debug().Msg("package cache is nil") @@ -228,11 +228,13 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con return pkgConf, nil } - pkgConfig := reflect.New(reflect.ValueOf(c).Elem().Type()).Interface() + //pkgConfig := reflect.New(reflect.ValueOf(c).Elem().Type()).Interface() + pkgConfig := &Config{} if err := copier.Copy(pkgConfig, c); err != nil { return nil, fmt.Errorf("failed to copy config: %w", err) } - pkgConfigTyped := pkgConfig.(*Config) + //pkgConfigTyped := pkgConfig.(*Config) + pkgConfigTyped := pkgConfig configMap, err := c.getPackageConfigMap(ctx, packageName) if err != nil { @@ -242,6 +244,7 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con configSection, ok := configMap["config"] if !ok { log.Debug().Msg("config section not provided for package") + configMap["config"] = deepCopyConfigMap(c._cfgAsMap) return pkgConfigTyped, nil } @@ -444,13 +447,13 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP } log.Debug().Msg("getting config") - cfg, err := c.CfgAsMap(ctx) + topLevelConfig, err := c.CfgAsMap(ctx) if err != nil { return fmt.Errorf("failed to get configuration map: %w", err) } log.Debug().Msg("getting packages section") - packagesSection := cfg["packages"].(map[string]any) + packagesSection := topLevelConfig["packages"].(map[string]any) // Don't overwrite any config that already exists _, pkgExists := packagesSection[subPkgPath] @@ -600,6 +603,7 @@ func (c *Config) subPackages( // recursive and recurses the file tree to find all sub-packages. func (c *Config) discoverRecursivePackages(ctx context.Context) error { log := zerolog.Ctx(ctx) + log.Trace().Msg("discovering recursive packages") recursivePackages := map[string]*Config{} packageList, err := c.GetPackages(ctx) if err != nil { @@ -607,11 +611,17 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error { } for _, pkg := range packageList { pkgConfig, err := c.GetPackageConfig(ctx, pkg) + pkgLog := log.With().Str("package", pkg).Logger() + pkgLog.Trace().Msg("iterating over package") if err != nil { return fmt.Errorf("failed to get package config: %w", err) } if pkgConfig.Recursive { + pkgLog.Trace().Msg("package marked as recursive") recursivePackages[pkg] = pkgConfig + } else { + pkgLog.Trace().Msg("package not marked as recursive") + pkgLog.Trace().Msg(fmt.Sprintf("%+v", pkgConfig)) } } if len(recursivePackages) == 0 { @@ -703,8 +713,12 @@ func (c *Config) mergeInConfig(ctx context.Context) error { configSectionUntyped, configExists := packageConfig["config"] if !configExists { - pkgLog.Trace().Msg("config section doesn't exist, setting it to a deepcopy of the top-level config") - packageConfig["config"] = deepCopyConfigMap(defaultCfg) + // The reason why this should never happen is because getPackageConfigMap + // should be populating the config section with the top-level config if it + // wasn't defined in the yaml. + msg := "config section does not exist for package, this should never happen" + pkgLog.Error().Msg(msg) + return fmt.Errorf(msg) } pkgLog.Trace().Msg("got config section for package") diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 5a7b654e..bf8edd8e 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1182,22 +1182,24 @@ dir: foobar recursive: True all: True packages: - github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2: `, - wantCfgMap: `dir: foobar + wantCfgMap: `all: true +dir: foobar packages: github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2: config: all: true dir: foobar recursive: true - with-expecter: true + with-expecter: false github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2/subpkg3: config: all: true dir: foobar recursive: true - with-expecter: true + with-expecter: false +recursive: true with-expecter: false `, }, @@ -1209,19 +1211,22 @@ with-expecter: false cfg := tmpdir.Join("config.yaml") require.NoError(t, cfg.WriteFile([]byte(tt.cfgYaml))) - c := &Config{ - Config: cfg.String(), - } + viperObj := viper.New() + viperObj.SetConfigFile(cfg.String()) + require.NoError(t, viperObj.ReadInConfig()) + c, err := NewConfigFromViper(viperObj) + require.NoError(t, err) + log, err := logging.GetLogger("TRACE") require.NoError(t, err) - if err := c.Initialize(log.WithContext(context.Background())); !errors.Is(err, tt.wantErr) { + if err := c.Initialize(log.WithContext(ctx)); !errors.Is(err, tt.wantErr) { t.Errorf("Config.Initialize() error = %v, wantErr %v", err, tt.wantErr) } cfgAsMap, err := c.CfgAsMap(ctx) require.NoError(t, err) - + cfgAsStr, err := yaml.Marshal(cfgAsMap) require.NoError(t, err) From e86d2306fe183ef311845ec66fc46e5039575ed0 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sun, 5 Nov 2023 20:36:45 -0600 Subject: [PATCH 11/30] Simplifying some config in interface copying code fix usage of predeclared identifier --- pkg/config/config.go | 46 ++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 1682231f..57d68e73 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -217,6 +217,13 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m return emptyMap, nil } + +// GetPackageConfig returns a struct representation of the package's config +// as provided in yaml. If the package did not specify a config section, +// this method will inject the top-level config into the package's config. +// This is especially useful as it allows us to lazily evaluate a package's +// config. If the package does specify config, this method takes care to merge +// the top-level config with the values specified for this package. func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Config, error) { log := zerolog.Ctx(ctx).With().Str("package-path", packageName).Logger() @@ -228,13 +235,10 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con return pkgConf, nil } - //pkgConfig := reflect.New(reflect.ValueOf(c).Elem().Type()).Interface() pkgConfig := &Config{} if err := copier.Copy(pkgConfig, c); err != nil { return nil, fmt.Errorf("failed to copy config: %w", err) } - //pkgConfigTyped := pkgConfig.(*Config) - pkgConfigTyped := pkgConfig configMap, err := c.getPackageConfigMap(ctx, packageName) if err != nil { @@ -245,18 +249,23 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con if !ok { log.Debug().Msg("config section not provided for package") configMap["config"] = deepCopyConfigMap(c._cfgAsMap) - return pkgConfigTyped, nil + c.pkgConfigCache[packageName] = pkgConfig + return pkgConfig, nil } - decoder, err := c.getDecoder(pkgConfigTyped) + // We know that the package specified config that is overriding the top-level + // config. We use a mapstructure decoder to decode the values in the yaml + // into the pkgConfig struct. This has the effect of merging top-level + // config with package-level config. + decoder, err := c.getDecoder(pkgConfig) if err != nil { return nil, stackerr.NewStackErrf(err, "failed to get decoder") } if err := decoder.Decode(configSection); err != nil { return nil, err } - c.pkgConfigCache[packageName] = pkgConfigTyped - return pkgConfigTyped, nil + c.pkgConfigCache[packageName] = pkgConfig + return pkgConfig, nil } func (c *Config) ExcludePath(path string) bool { @@ -355,16 +364,15 @@ func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, int } // Copy the package-level config to our interface-level config - pkgConfigCopy := reflect.New(reflect.ValueOf(pkgConfig).Elem().Type()).Interface() + pkgConfigCopy := &Config{} if err := copier.Copy(pkgConfigCopy, pkgConfig); err != nil { return nil, stackerr.NewStackErrf(err, "failed to create a copy of package config") } - baseConfigTyped := pkgConfigCopy.(*Config) interfaceSection, ok := interfacesSection[interfaceName] if !ok { log.Debug().Msg("interface not defined in package configuration") - return []*Config{baseConfigTyped}, nil + return []*Config{pkgConfigCopy}, nil } interfaceSectionTyped, ok := interfaceSection.(map[string]any) @@ -373,7 +381,7 @@ func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, int // the interface but not provide any additional config beyond what // is provided at the package level if reflect.ValueOf(&interfaceSection).Elem().IsZero() { - return []*Config{baseConfigTyped}, nil + return []*Config{pkgConfigCopy}, nil } msgString := "bad type provided for interface config" log.Error().Msgf(msgString) @@ -384,11 +392,11 @@ func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, int if ok { log.Debug().Msg("config section exists for interface") // if `config` is provided, we'll overwrite the values in our - // baseConfigTyped struct to act as the "new" base config. + // pkgConfigCopy struct to act as the "new" base config. // This will allow us to set the default values for the interface // but override them further for each mock defined in the // `configs` section. - decoder, err := c.getDecoder(baseConfigTyped) + decoder, err := c.getDecoder(pkgConfigCopy) if err != nil { return nil, stackerr.NewStackErrf(err, "unable to create mapstructure decoder") } @@ -405,8 +413,8 @@ func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, int configsSectionTyped := configsSection.([]any) for _, configMap := range configsSectionTyped { // Create a copy of the package-level config - currentInterfaceConfig := reflect.New(reflect.ValueOf(baseConfigTyped).Elem().Type()).Interface() - if err := copier.Copy(currentInterfaceConfig, baseConfigTyped); err != nil { + currentInterfaceConfig := reflect.New(reflect.ValueOf(pkgConfigCopy).Elem().Type()).Interface() + if err := copier.Copy(currentInterfaceConfig, pkgConfigCopy); err != nil { return nil, stackerr.NewStackErrf(err, "failed to copy package config") } @@ -426,7 +434,7 @@ func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, int log.Debug().Msg("configs section doesn't exist for interface") if len(configs) == 0 { - configs = append(configs, baseConfigTyped) + configs = append(configs, pkgConfigCopy) } return configs, nil } @@ -674,14 +682,14 @@ func contains[T comparable](slice []T, elem T) bool { } func deepCopyConfigMap(src map[string]any) map[string]any { - new := map[string]any{} + newMap := map[string]any{} for key, val := range src { if contains([]string{"packages", "config", "interfaces"}, key) { continue } - new[key] = val + newMap[key] = val } - return new + return newMap } // mergeInConfig takes care of merging inheritable configuration From d3515d1dd03b02fac062856da8fdb58d4c0bef9a Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sun, 5 Nov 2023 21:43:58 -0600 Subject: [PATCH 12/30] Fix bug with sub-package inheritance Subpackages were not correctly inheriting their parent package configuration. --- pkg/config/config.go | 41 +++++++++++++++++++++++++++------------ pkg/config/config_test.go | 1 + 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 57d68e73..c709f4ba 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -195,7 +195,14 @@ func (c *Config) GetPackages(ctx context.Context) ([]string, error) { return packageList, nil } +// getPackageConfigMap returns the map for the particular package, which includes +// (but is not limited to) both the `configs` section and the `interfaces` section. +// Note this does NOT return the `configs` section for the package. It returns the +// entire mapping for the package. func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (map[string]any, error) { + log := zerolog.Ctx(ctx) + log.Trace().Msg("getting package config map") + cfgMap, err := c.CfgAsMap(ctx) if err != nil { return nil, err @@ -207,8 +214,10 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m } configAsMap, isMap := configUnmerged.(map[string]any) if isMap { + log.Trace().Msg("package's value is a map, returning") return configAsMap, nil } + log.Trace().Msg("package's value is not a map") // Package is something other than map, so set its value to an // empty map. @@ -248,7 +257,7 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con configSection, ok := configMap["config"] if !ok { log.Debug().Msg("config section not provided for package") - configMap["config"] = deepCopyConfigMap(c._cfgAsMap) + configMap["config"] = map[string]any{} c.pkgConfigCache[packageName] = pkgConfig return pkgConfig, nil } @@ -445,6 +454,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP log := zerolog.Ctx(ctx).With(). Str("parent-package", parentPkgPath). Str("sub-package", subPkgPath).Logger() + ctx = log.WithContext(ctx) log.Debug().Msg("adding sub-package to config map") parentPkgConfig, err := c.getPackageConfigMap(ctx, parentPkgPath) @@ -463,13 +473,14 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP log.Debug().Msg("getting packages section") packagesSection := topLevelConfig["packages"].(map[string]any) - // Don't overwrite any config that already exists _, pkgExists := packagesSection[subPkgPath] if !pkgExists { log.Trace().Msg("sub-package doesn't exist in config") + + // Copy the parent package directly into the subpackage config section packagesSection[subPkgPath] = map[string]any{} newPkgSection := packagesSection[subPkgPath].(map[string]any) - newPkgSection["config"] = parentPkgConfig["config"] + newPkgSection["config"] = deepCopyConfigMap(parentPkgConfig["config"].(map[string]any)) } else { log.Trace().Msg("sub-package exists in config") // The sub-package exists in config. Check if it has its @@ -481,10 +492,15 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP log.Err(err).Msg("could not get child package config") return fmt.Errorf("failed to get sub-package config: %w", err) } - - for key, val := range parentPkgConfig { - if _, keyInSubPkg := subPkgConfig[key]; !keyInSubPkg { - subPkgConfig[key] = val + log.Trace().Msgf("sub-package config: %v", subPkgConfig) + log.Trace().Msgf("parent-package config: %v", parentPkgConfig) + + // Merge the parent config with the sub-package config. + parentConfigSection := parentPkgConfig["config"].(map[string]any) + subPkgConfigSection := subPkgConfig["config"].(map[string]any) + for key, val := range parentConfigSection { + if _, keyInSubPkg := subPkgConfigSection[key]; !keyInSubPkg { + subPkgConfigSection[key] = val } } @@ -629,7 +645,6 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error { recursivePackages[pkg] = pkgConfig } else { pkgLog.Trace().Msg("package not marked as recursive") - pkgLog.Trace().Msg(fmt.Sprintf("%+v", pkgConfig)) } } if len(recursivePackages) == 0 { @@ -711,13 +726,15 @@ func (c *Config) mergeInConfig(ctx context.Context) error { } for _, pkgPath := range pkgs { pkgLog := log.With().Str("package-path", pkgPath).Logger() + pkgCtx := pkgLog.WithContext(ctx) + pkgLog.Trace().Msg("merging for package") - packageConfig, err := c.getPackageConfigMap(ctx, pkgPath) + packageConfig, err := c.getPackageConfigMap(pkgCtx, pkgPath) if err != nil { pkgLog.Err(err).Msg("failed to get package config") return fmt.Errorf("failed to get package config: %w", err) } - pkgLog.Trace().Msg("got package config map") + pkgLog.Trace().Msgf("got package config map: %v", packageConfig) configSectionUntyped, configExists := packageConfig["config"] if !configExists { @@ -759,12 +776,12 @@ func (c *Config) mergeInConfig(ctx context.Context) error { configSectionTyped[key] = value } } - interfaces, err := c.getInterfacesForPackage(ctx, pkgPath) + interfaces, err := c.getInterfacesForPackage(pkgCtx, pkgPath) if err != nil { return fmt.Errorf("failed to get interfaces for package: %w", err) } for _, interfaceName := range interfaces { - interfacesSection, err := c.getInterfacesSection(ctx, pkgPath) + interfacesSection, err := c.getInterfacesSection(pkgCtx, pkgPath) if err != nil { return err } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index bf8edd8e..4e981eb0 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1206,6 +1206,7 @@ with-expecter: false } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() tmpdir := pathlib.NewPath(t.TempDir()) cfg := tmpdir.Join("config.yaml") From 2dd8f00aa6d0d6b1cb5e3631d8225d9d30f14733 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sun, 5 Nov 2023 21:44:35 -0600 Subject: [PATCH 13/30] Use gotestsum for better testing output --- Taskfile.yml | 2 +- tools/go.mod | 18 +++++++++++------- tools/go.sum | 44 ++++++++++++++++++++++++++++++++++---------- tools/tools.go | 1 + 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index b78e8955..f9686331 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -52,7 +52,7 @@ tasks: test: cmds: - - go test -v -coverprofile=coverage.txt ./... + - go run gotest.tools/gotestsum --format testname -- -v -coverprofile=coverage.txt ./... desc: run unit tests sources: - "**/*.go" diff --git a/tools/go.mod b/tools/go.mod index 0468ca23..a5a931b2 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/go-task/task/v3 v3.24.0 github.com/golangci/golangci-lint v1.52.2 + gotest.tools/gotestsum v1.11.0 ) require ( @@ -24,6 +25,7 @@ require ( github.com/ashanbrown/forbidigo v1.5.1 // indirect github.com/ashanbrown/makezero v1.1.1 // indirect github.com/beorn7/perks v1.0.1 // indirect + github.com/bitfield/gotestdox v0.2.1 // indirect github.com/bkielbasa/cyclop v1.2.0 // indirect github.com/blizzy78/varnamelen v0.8.0 // indirect github.com/bombsimon/wsl/v3 v3.4.0 // indirect @@ -37,6 +39,7 @@ require ( github.com/daixiang0/gci v0.10.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/denis-tingaikin/go-header v0.4.3 // indirect + github.com/dnephin/pflag v1.0.7 // indirect github.com/esimonov/ifshort v1.0.4 // indirect github.com/ettle/strcase v0.1.1 // indirect github.com/fatih/color v1.15.0 // indirect @@ -67,6 +70,7 @@ require ( github.com/golangci/revgrep v0.0.0-20220804021717-745bb2f7c2e6 // indirect github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 // indirect github.com/google/go-cmp v0.5.9 // indirect + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/gordonklaus/ineffassign v0.0.0-20230107090616-13ace0543b28 // indirect github.com/gostaticanalysis/analysisutil v0.7.1 // indirect github.com/gostaticanalysis/comment v1.4.2 // indirect @@ -99,7 +103,7 @@ require ( github.com/maratori/testpackage v1.1.1 // indirect github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect github.com/mattn/go-zglob v0.0.4 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect @@ -171,12 +175,12 @@ require ( go.uber.org/zap v1.24.0 // indirect golang.org/x/exp v0.0.0-20230212135524-a684f29349b6 // indirect golang.org/x/exp/typeparams v0.0.0-20230224173230-c95f2b4c22f2 // indirect - golang.org/x/mod v0.9.0 // indirect - golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.6.0 // indirect - golang.org/x/term v0.5.0 // indirect - golang.org/x/text v0.7.0 // indirect - golang.org/x/tools v0.7.0 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/sync v0.3.0 // indirect + golang.org/x/sys v0.10.0 // indirect + golang.org/x/term v0.10.0 // indirect + golang.org/x/text v0.11.0 // indirect + golang.org/x/tools v0.11.0 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/tools/go.sum b/tools/go.sum index 2ae83f24..a08b263b 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -78,6 +78,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bitfield/gotestdox v0.2.1 h1:Zj8IMLAO5/oiAKoMmtN96eyFiPZraJRTH2p0zDgtxc0= +github.com/bitfield/gotestdox v0.2.1/go.mod h1:D+gwtS0urjBrzguAkTM2wodsTQYFHdpx8eqRJ3N+9pY= github.com/bkielbasa/cyclop v1.2.0 h1:7Jmnh0yL2DjKfw28p86YTd/B4lRGcNuu12sKE35sM7A= github.com/bkielbasa/cyclop v1.2.0/go.mod h1:qOI0yy6A7dYC4Zgsa72Ppm9kONl0RoIlPbzot9mhmeI= github.com/blizzy78/varnamelen v0.8.0 h1:oqSblyuQvFsW1hbBHh1zfwrKe3kcSj0rnXkKzsQ089M= @@ -116,6 +118,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denis-tingaikin/go-header v0.4.3 h1:tEaZKAlqql6SKCY++utLmkPLd6K8IBM20Ha7UVm+mtU= github.com/denis-tingaikin/go-header v0.4.3/go.mod h1:0wOCWuN71D5qIgE2nz9KrKmuYBAC2Mra5RassOIQ2/c= +github.com/dnephin/pflag v1.0.7 h1:oxONGlWxhmUct0YzKTgrpQv9AUA1wtPBn7zuSjJqptk= +github.com/dnephin/pflag v1.0.7/go.mod h1:uxE91IoWURlOiTUIA8Mq5ZZkAv3dPUfZNaT80Zm7OQE= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -257,6 +261,8 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= @@ -354,8 +360,9 @@ github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwM github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-zglob v0.0.4 h1:LQi2iOm0/fGgu80AioIJ/1j9w9Oh+9DZ39J4VAGzHQM= @@ -448,7 +455,8 @@ github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567/go.mod h1:DWNGW8 github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryancurrah/gomodguard v1.3.0 h1:q15RT/pd6UggBXVBuLps8BXRvl5GPBcwVA7BJHMLuTw= github.com/ryancurrah/gomodguard v1.3.0/go.mod h1:ggBxb3luypPEzqVtq33ee7YSN35V28XeGnid8dnni50= @@ -491,6 +499,7 @@ github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.12.0 h1:CZ7eSOd3kZoaYDLbXnmzgQI5RlciuXBMA+18HwHRfZQ= @@ -576,6 +585,7 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= +golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -622,8 +632,9 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91 golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -666,7 +677,9 @@ golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= +golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -689,8 +702,9 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -750,16 +764,20 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= -golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= +golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -771,8 +789,10 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -846,8 +866,8 @@ golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k= golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= -golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= +golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= +golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -962,6 +982,10 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/gotestsum v1.11.0 h1:A88/QWw7acMjZH1dMe6KZFhw32odUOIjCiAU/Q4n3mI= +gotest.tools/gotestsum v1.11.0/go.mod h1:cUOKgFEvWAP0twchmiOvdzX0SBZX0UI58bGRpRIu4xs= +gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo= +gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/tools/tools.go b/tools/tools.go index 3744723b..e6e237e6 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -5,4 +5,5 @@ package tools import ( _ "github.com/go-task/task/v3/cmd/task" _ "github.com/golangci/golangci-lint/cmd/golangci-lint" + _ "gotest.tools/gotestsum" ) From b648c231de36dd76e63da422ad2dd5041ab0b13e Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sun, 5 Nov 2023 22:02:34 -0600 Subject: [PATCH 14/30] Add additional test --- pkg/config/config_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 4e981eb0..ce5cad6b 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1201,6 +1201,36 @@ packages: with-expecter: false recursive: true with-expecter: false +`, + }, + { + name: "empty map for subpackage of recursive package", + cfgYaml: ` +with-expecter: False +dir: foobar +packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2: + config: + recursive: True + with-expecter: True + all: True + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2/subpkg3: {} +`, + wantCfgMap: `dir: foobar +packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2: + config: + all: true + dir: foobar + recursive: true + with-expecter: true + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2/subpkg3: + config: + all: true + dir: foobar + recursive: true + with-expecter: true +with-expecter: false `, }, } From fc10b9c1924e91f08d01380d08af70e60731c8aa Mon Sep 17 00:00:00 2001 From: Mark Nevill Date: Sun, 12 Nov 2023 12:02:48 +0000 Subject: [PATCH 15/30] Don't recurse into submodule Closes #706 --- pkg/config/config.go | 73 ++++++++++--------- pkg/config/config_test.go | 22 +++++- .../example_project/pkg_with_submod/foo.go | 1 + .../pkg_with_submod/submod/foo.go | 1 + .../pkg_with_submod/submod/go.mod | 3 + 5 files changed, 63 insertions(+), 37 deletions(-) create mode 100644 pkg/fixtures/example_project/pkg_with_submod/foo.go create mode 100644 pkg/fixtures/example_project/pkg_with_submod/submod/foo.go create mode 100644 pkg/fixtures/example_project/pkg_with_submod/submod/go.mod diff --git a/pkg/config/config.go b/pkg/config/config.go index c709f4ba..437f1c3d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,7 +5,9 @@ import ( "context" "errors" "fmt" + "io/fs" "os" + "path/filepath" "reflect" "regexp" "strings" @@ -224,7 +226,6 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m emptyMap := map[string]any{} packageSection[packageName] = emptyMap return emptyMap, nil - } // GetPackageConfig returns a struct representation of the package's config @@ -487,7 +488,6 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP // own `config` section and merge with the parent package // if so. subPkgConfig, err := c.getPackageConfigMap(ctx, subPkgPath) - if err != nil { log.Err(err).Msg("could not get child package config") return fmt.Errorf("failed to get sub-package config: %w", err) @@ -502,7 +502,6 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP if _, keyInSubPkg := subPkgConfigSection[key]; !keyInSubPkg { subPkgConfigSection[key] = val } - } } @@ -556,17 +555,6 @@ func (c *Config) subPackages( packageRootPath := searchRoot subPackages := []string{} - walker, err := pathlib.NewWalk( - searchRoot, - pathlib.WalkAlgorithm(pathlib.AlgorithmBasic), - pathlib.WalkFollowSymlinks(false), - pathlib.WalkVisitDirs(false), - pathlib.WalkVisitFiles(true), - ) - if err != nil { - return nil, fmt.Errorf("failed to create filesystem walker: %w", err) - } - visitedDirs := map[string]any{} subdirectoriesWithGoFiles := []*pathlib.Path{} @@ -577,31 +565,49 @@ func (c *Config) subPackages( // Walk the filesystem path, starting at the root of the package we've // been given. Note that this will always work because Golang downloads // the package when we call `packages.Load` - walkErr := walker.Walk(func(path *pathlib.Path, info os.FileInfo, err error) error { + walkErr := filepath.Walk(searchRoot.String(), func(pathStr string, info fs.FileInfo, err error) error { if err != nil { return err } - _, haveVisitedDir := visitedDirs[path.Parent().String()] - if !haveVisitedDir && strings.HasSuffix(path.Name(), ".go") { - - if !c.IncludeAutoGenerated { - autoGenerated, err := isAutoGenerated(path) - if err != nil { - log.Err(err).Stringer("path", path).Msg("failed to determine if file is auto-generated") - return err - } - if autoGenerated { - log.Debug().Stringer("path", path).Msg("skipping file as auto-generated") - return nil - } + path := pathlib.NewPath(pathStr) + if info.IsDir() { + gomodPath := path.Join("go.mod") + gomodExists, err := gomodPath.Exists() + if err != nil { + log.Err(err).Stringer("path", gomodPath).Msg("failed to determine if go.mod exists") + return err + } + if gomodExists { + log.Debug().Stringer("path", path).Msg("skipping directory as sub-module") + return filepath.SkipDir } + return nil + } + + if _, haveVisitedDir := visitedDirs[path.Parent().String()]; haveVisitedDir { + return nil + } - l := log.With().Stringer("path", path.Parent()).Logger() - l.Debug().Msg("subdirectory has a .go file, adding this path to packages config") - subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent()) - visitedDirs[path.Parent().String()] = nil + if !strings.HasSuffix(path.Name(), ".go") { + return nil } + + if !c.IncludeAutoGenerated { + autoGenerated, err := isAutoGenerated(path) + if err != nil { + log.Err(err).Stringer("path", path).Msg("failed to determine if file is auto-generated") + return err + } + if autoGenerated { + log.Debug().Stringer("path", path).Msg("skipping file as auto-generated") + return nil + } + } + + log.Debug().Stringer("path", path.Parent()).Msg("subdirectory has a .go file, adding this path to packages config") + subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent()) + visitedDirs[path.Parent().String()] = nil return nil }) if walkErr != nil { @@ -684,7 +690,6 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error { log.Trace().Msg("done discovering recursive packages") return nil - } func contains[T comparable](slice []T, elem T) bool { @@ -817,7 +822,6 @@ func (c *Config) mergeInConfig(ctx context.Context) error { } return nil - } func (c *Config) getInterfacesForPackage(ctx context.Context, pkgPath string) ([]string, error) { @@ -875,5 +879,4 @@ func (c *Config) LogUnsupportedPackagesConfig(ctx context.Context) { Str("url", logging.DocsURL("/configuration/#parameter-descriptions")). Logger() l.Error().Msg("use of unsupported options detected. mockery behavior is undefined.") - } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index ce5cad6b..0e419865 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1231,12 +1231,31 @@ packages: recursive: true with-expecter: true with-expecter: false +`, + }, + { + name: "test recursive with submodule", + cfgYaml: ` +with-expecter: False +packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submod: + config: + recursive: True + with-expecter: True + all: True +`, + wantCfgMap: `packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submod: + config: + all: true + recursive: true + with-expecter: true +with-expecter: false `, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctx := context.Background() tmpdir := pathlib.NewPath(t.TempDir()) cfg := tmpdir.Join("config.yaml") @@ -1271,7 +1290,6 @@ want ------ %v`, string(cfgAsStr), tt.wantCfgMap) } - }) } } diff --git a/pkg/fixtures/example_project/pkg_with_submod/foo.go b/pkg/fixtures/example_project/pkg_with_submod/foo.go new file mode 100644 index 00000000..480f9084 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submod/foo.go @@ -0,0 +1 @@ +package pkg_with_subpkgs diff --git a/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go b/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go new file mode 100644 index 00000000..7e353e3a --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go @@ -0,0 +1 @@ +package submod diff --git a/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod b/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod new file mode 100644 index 00000000..7d2e1ee1 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod @@ -0,0 +1,3 @@ +module submod + +go 1.21.3 From 0c5b6a44a9e21c3dc9f2401f2a39726693abd36b Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Tue, 14 Nov 2023 09:47:23 -0600 Subject: [PATCH 16/30] add note in docs about go.mod skipping --- docs/features.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/features.md b/docs/features.md index 979b03b6..9d320f98 100644 --- a/docs/features.md +++ b/docs/features.md @@ -178,6 +178,8 @@ packages: You can use the `showconfig` command to see the config mockery injects. The output of `showconfig` theoretically could be copy-pasted into your yaml file as it is semantically equivalent. +mockery will _not_ recurse into submodules, i.e. any subdirectory that contains a go.mod file. You must specify the submodule as a separate line item in the config if you would like mocks generated for it as well. + ??? note "performance characteristics" The performance when using `#!yaml recursive: true` may be worse than manually specifying all packages statically in the yaml file. This is because of the fact that mockery has to recursively walk the filesystem path that contains the package in question. It may unnecessarily walk down unrelated paths (for example, a Python virtual environment that is in the same path as your package). For this reason, it is recommended _not_ to use `#!yaml recursive: true` if it can be avoided. From fa5b6cb75d2a67dfa5b5d884c2d74f6bcdfd69c6 Mon Sep 17 00:00:00 2001 From: Landon Clipp Date: Wed, 15 Nov 2023 10:11:52 -0600 Subject: [PATCH 17/30] Revert "Don't recurse into submodules" --- pkg/config/config.go | 73 +++++++++---------- pkg/config/config_test.go | 22 +----- .../example_project/pkg_with_submod/foo.go | 1 - .../pkg_with_submod/submod/foo.go | 1 - .../pkg_with_submod/submod/go.mod | 3 - 5 files changed, 37 insertions(+), 63 deletions(-) delete mode 100644 pkg/fixtures/example_project/pkg_with_submod/foo.go delete mode 100644 pkg/fixtures/example_project/pkg_with_submod/submod/foo.go delete mode 100644 pkg/fixtures/example_project/pkg_with_submod/submod/go.mod diff --git a/pkg/config/config.go b/pkg/config/config.go index 437f1c3d..c709f4ba 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,9 +5,7 @@ import ( "context" "errors" "fmt" - "io/fs" "os" - "path/filepath" "reflect" "regexp" "strings" @@ -226,6 +224,7 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m emptyMap := map[string]any{} packageSection[packageName] = emptyMap return emptyMap, nil + } // GetPackageConfig returns a struct representation of the package's config @@ -488,6 +487,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP // own `config` section and merge with the parent package // if so. subPkgConfig, err := c.getPackageConfigMap(ctx, subPkgPath) + if err != nil { log.Err(err).Msg("could not get child package config") return fmt.Errorf("failed to get sub-package config: %w", err) @@ -502,6 +502,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP if _, keyInSubPkg := subPkgConfigSection[key]; !keyInSubPkg { subPkgConfigSection[key] = val } + } } @@ -555,6 +556,17 @@ func (c *Config) subPackages( packageRootPath := searchRoot subPackages := []string{} + walker, err := pathlib.NewWalk( + searchRoot, + pathlib.WalkAlgorithm(pathlib.AlgorithmBasic), + pathlib.WalkFollowSymlinks(false), + pathlib.WalkVisitDirs(false), + pathlib.WalkVisitFiles(true), + ) + if err != nil { + return nil, fmt.Errorf("failed to create filesystem walker: %w", err) + } + visitedDirs := map[string]any{} subdirectoriesWithGoFiles := []*pathlib.Path{} @@ -565,49 +577,31 @@ func (c *Config) subPackages( // Walk the filesystem path, starting at the root of the package we've // been given. Note that this will always work because Golang downloads // the package when we call `packages.Load` - walkErr := filepath.Walk(searchRoot.String(), func(pathStr string, info fs.FileInfo, err error) error { + walkErr := walker.Walk(func(path *pathlib.Path, info os.FileInfo, err error) error { if err != nil { return err } - path := pathlib.NewPath(pathStr) - if info.IsDir() { - gomodPath := path.Join("go.mod") - gomodExists, err := gomodPath.Exists() - if err != nil { - log.Err(err).Stringer("path", gomodPath).Msg("failed to determine if go.mod exists") - return err - } - if gomodExists { - log.Debug().Stringer("path", path).Msg("skipping directory as sub-module") - return filepath.SkipDir - } - return nil - } - - if _, haveVisitedDir := visitedDirs[path.Parent().String()]; haveVisitedDir { - return nil - } - - if !strings.HasSuffix(path.Name(), ".go") { - return nil - } + _, haveVisitedDir := visitedDirs[path.Parent().String()] + if !haveVisitedDir && strings.HasSuffix(path.Name(), ".go") { - if !c.IncludeAutoGenerated { - autoGenerated, err := isAutoGenerated(path) - if err != nil { - log.Err(err).Stringer("path", path).Msg("failed to determine if file is auto-generated") - return err - } - if autoGenerated { - log.Debug().Stringer("path", path).Msg("skipping file as auto-generated") - return nil + if !c.IncludeAutoGenerated { + autoGenerated, err := isAutoGenerated(path) + if err != nil { + log.Err(err).Stringer("path", path).Msg("failed to determine if file is auto-generated") + return err + } + if autoGenerated { + log.Debug().Stringer("path", path).Msg("skipping file as auto-generated") + return nil + } } - } - log.Debug().Stringer("path", path.Parent()).Msg("subdirectory has a .go file, adding this path to packages config") - subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent()) - visitedDirs[path.Parent().String()] = nil + l := log.With().Stringer("path", path.Parent()).Logger() + l.Debug().Msg("subdirectory has a .go file, adding this path to packages config") + subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent()) + visitedDirs[path.Parent().String()] = nil + } return nil }) if walkErr != nil { @@ -690,6 +684,7 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error { log.Trace().Msg("done discovering recursive packages") return nil + } func contains[T comparable](slice []T, elem T) bool { @@ -822,6 +817,7 @@ func (c *Config) mergeInConfig(ctx context.Context) error { } return nil + } func (c *Config) getInterfacesForPackage(ctx context.Context, pkgPath string) ([]string, error) { @@ -879,4 +875,5 @@ func (c *Config) LogUnsupportedPackagesConfig(ctx context.Context) { Str("url", logging.DocsURL("/configuration/#parameter-descriptions")). Logger() l.Error().Msg("use of unsupported options detected. mockery behavior is undefined.") + } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 0e419865..ce5cad6b 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1231,31 +1231,12 @@ packages: recursive: true with-expecter: true with-expecter: false -`, - }, - { - name: "test recursive with submodule", - cfgYaml: ` -with-expecter: False -packages: - github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submod: - config: - recursive: True - with-expecter: True - all: True -`, - wantCfgMap: `packages: - github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submod: - config: - all: true - recursive: true - with-expecter: true -with-expecter: false `, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() tmpdir := pathlib.NewPath(t.TempDir()) cfg := tmpdir.Join("config.yaml") @@ -1290,6 +1271,7 @@ want ------ %v`, string(cfgAsStr), tt.wantCfgMap) } + }) } } diff --git a/pkg/fixtures/example_project/pkg_with_submod/foo.go b/pkg/fixtures/example_project/pkg_with_submod/foo.go deleted file mode 100644 index 480f9084..00000000 --- a/pkg/fixtures/example_project/pkg_with_submod/foo.go +++ /dev/null @@ -1 +0,0 @@ -package pkg_with_subpkgs diff --git a/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go b/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go deleted file mode 100644 index 7e353e3a..00000000 --- a/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go +++ /dev/null @@ -1 +0,0 @@ -package submod diff --git a/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod b/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod deleted file mode 100644 index 7d2e1ee1..00000000 --- a/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module submod - -go 1.21.3 From 445f73c80b61ff771450c7ec0f0836b6062f3aa3 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 20 Nov 2023 08:46:28 -0300 Subject: [PATCH 18/30] added new check for no return values --- .../vektra/mockery/v2/pkg/TypesPackage.go | 8 ++ .../vektra/mockery/v2/pkg/fixtures/A.go | 4 + .../mockery/v2/pkg/fixtures/AsyncProducer.go | 12 +++ .../vektra/mockery/v2/pkg/fixtures/Blank.go | 4 + .../mockery/v2/pkg/fixtures/ConsulLock.go | 8 ++ .../mockery/v2/pkg/fixtures/EmbeddedGet.go | 4 + .../vektra/mockery/v2/pkg/fixtures/Example.go | 8 ++ .../mockery/v2/pkg/fixtures/Expecter.go | 16 ++++ .../pkg/fixtures/ExpecterAndRolledVariadic.go | 16 ++++ .../vektra/mockery/v2/pkg/fixtures/Fooer.go | 8 ++ .../v2/pkg/fixtures/FuncArgsCollision.go | 4 + .../mockery/v2/pkg/fixtures/GetGeneric.go | 4 + .../vektra/mockery/v2/pkg/fixtures/GetInt.go | 4 + .../fixtures/HasConflictingNestedImports.go | 8 ++ .../v2/pkg/fixtures/ImportsSameAsPackage.go | 8 ++ .../mockery/v2/pkg/fixtures/KeyManager.go | 4 + .../vektra/mockery/v2/pkg/fixtures/MapFunc.go | 4 + .../mockery/v2/pkg/fixtures/MyReader.go | 4 + .../v2/pkg/fixtures/PanicOnNoReturnValue.go | 77 +++++++++++++++++++ .../mockery/v2/pkg/fixtures/Requester.go | 4 + .../mockery/v2/pkg/fixtures/Requester2.go | 4 + .../mockery/v2/pkg/fixtures/Requester3.go | 4 + .../pkg/fixtures/RequesterArgSameAsImport.go | 4 + .../fixtures/RequesterArgSameAsNamedImport.go | 4 + .../mockery/v2/pkg/fixtures/RequesterArray.go | 4 + .../v2/pkg/fixtures/RequesterElided.go | 4 + .../v2/pkg/fixtures/RequesterGenerics.go | 12 +++ .../mockery/v2/pkg/fixtures/RequesterIface.go | 4 + .../mockery/v2/pkg/fixtures/RequesterNS.go | 4 + .../mockery/v2/pkg/fixtures/RequesterPtr.go | 4 + .../v2/pkg/fixtures/RequesterReturnElided.go | 8 ++ .../mockery/v2/pkg/fixtures/RequesterSlice.go | 4 + .../v2/pkg/fixtures/RequesterVariadic.go | 16 ++++ .../fixtures/RequesterVariadicOneArgument.go | 16 ++++ .../mockery/v2/pkg/fixtures/SendFunc.go | 4 + .../mockery/v2/pkg/fixtures/StructWithTag.go | 4 + .../mock_interfaceA_test.go | 12 +++ .../mock_interfaceB0_test.go | 4 + .../mock_interfaceB_test.go | 4 + pkg/fixtures/panic_err.go | 5 ++ pkg/fixtures/recursive_generation/Foo_mock.go | 4 + .../recursive_generation/subpkg1/Foo_mock.go | 4 + .../recursive_generation/subpkg2/Foo_mock.go | 4 + .../Foo_mock.go | 4 + pkg/generator.go | 4 + pkg/generator_test.go | 4 + 46 files changed, 358 insertions(+) create mode 100644 mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go create mode 100644 pkg/fixtures/panic_err.go diff --git a/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go b/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go index a1b85506..44f96296 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go @@ -21,6 +21,10 @@ func (_m *TypesPackage) EXPECT() *TypesPackage_Expecter { func (_m *TypesPackage) Name() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Name") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() @@ -62,6 +66,10 @@ func (_c *TypesPackage_Name_Call) RunAndReturn(run func() string) *TypesPackage_ func (_m *TypesPackage) Path() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Path") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go index 4d5a41f0..c7682dea 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go @@ -24,6 +24,10 @@ func (_m *A) EXPECT() *A_Expecter { func (_m *A) Call() (test.B, error) { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Call") + } + var r0 test.B var r1 error if rf, ok := ret.Get(0).(func() (test.B, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go index 9badf642..e2f21751 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go @@ -21,6 +21,10 @@ func (_m *AsyncProducer) EXPECT() *AsyncProducer_Expecter { func (_m *AsyncProducer) Input() chan<- bool { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Input") + } + var r0 chan<- bool if rf, ok := ret.Get(0).(func() chan<- bool); ok { r0 = rf() @@ -64,6 +68,10 @@ func (_c *AsyncProducer_Input_Call) RunAndReturn(run func() chan<- bool) *AsyncP func (_m *AsyncProducer) Output() <-chan bool { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Output") + } + var r0 <-chan bool if rf, ok := ret.Get(0).(func() <-chan bool); ok { r0 = rf() @@ -107,6 +115,10 @@ func (_c *AsyncProducer_Output_Call) RunAndReturn(run func() <-chan bool) *Async func (_m *AsyncProducer) Whatever() chan bool { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Whatever") + } + var r0 chan bool if rf, ok := ret.Get(0).(func() chan bool); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go index e25ed682..50a08902 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go @@ -21,6 +21,10 @@ func (_m *Blank) EXPECT() *Blank_Expecter { func (_m *Blank) Create(x interface{}) error { ret := _m.Called(x) + if len(ret) == 0 { + panic("Missing Return() function for Create") + } + var r0 error if rf, ok := ret.Get(0).(func(interface{}) error); ok { r0 = rf(x) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go index dc953f25..9bdeb62a 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go @@ -21,6 +21,10 @@ func (_m *ConsulLock) EXPECT() *ConsulLock_Expecter { func (_m *ConsulLock) Lock(_a0 <-chan struct{}) (<-chan struct{}, error) { ret := _m.Called(_a0) + if len(ret) == 0 { + panic("Missing Return() function for Lock") + } + var r0 <-chan struct{} var r1 error if rf, ok := ret.Get(0).(func(<-chan struct{}) (<-chan struct{}, error)); ok { @@ -75,6 +79,10 @@ func (_c *ConsulLock_Lock_Call) RunAndReturn(run func(<-chan struct{}) (<-chan s func (_m *ConsulLock) Unlock() error { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Unlock") + } + var r0 error if rf, ok := ret.Get(0).(func() error); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go index 19fa5584..1954d434 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go @@ -24,6 +24,10 @@ func (_m *EmbeddedGet[T]) EXPECT() *EmbeddedGet_Expecter[T] { func (_m *EmbeddedGet[T]) Get() T { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 T if rf, ok := ret.Get(0).(func() T); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go index 0a9ab4c2..a67b163d 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go @@ -27,6 +27,10 @@ func (_m *Example) EXPECT() *Example_Expecter { func (_m *Example) A() http.Flusher { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for A") + } + var r0 http.Flusher if rf, ok := ret.Get(0).(func() http.Flusher); ok { r0 = rf() @@ -70,6 +74,10 @@ func (_c *Example_A_Call) RunAndReturn(run func() http.Flusher) *Example_A_Call func (_m *Example) B(_a0 string) fixtureshttp.MyStruct { ret := _m.Called(_a0) + if len(ret) == 0 { + panic("Missing Return() function for B") + } + var r0 fixtureshttp.MyStruct if rf, ok := ret.Get(0).(func(string) fixtureshttp.MyStruct); ok { r0 = rf(_a0) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go index bd3c0f3a..69aec464 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go @@ -21,6 +21,10 @@ func (_m *Expecter) EXPECT() *Expecter_Expecter { func (_m *Expecter) ManyArgsReturns(str string, i int) ([]string, error) { ret := _m.Called(str, i) + if len(ret) == 0 { + panic("Missing Return() function for ManyArgsReturns") + } + var r0 []string var r1 error if rf, ok := ret.Get(0).(func(string, int) ([]string, error)); ok { @@ -76,6 +80,10 @@ func (_c *Expecter_ManyArgsReturns_Call) RunAndReturn(run func(string, int) ([]s func (_m *Expecter) NoArg() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for NoArg") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() @@ -156,6 +164,10 @@ func (_m *Expecter) Variadic(ints ...int) error { _ca = append(_ca, _va...) ret := _m.Called(_ca...) + if len(ret) == 0 { + panic("Missing Return() function for Variadic") + } + var r0 error if rf, ok := ret.Get(0).(func(...int) error); ok { r0 = rf(ints...) @@ -208,6 +220,10 @@ func (_m *Expecter) VariadicMany(i int, a string, intfs ...interface{}) error { _ca = append(_ca, intfs...) ret := _m.Called(_ca...) + if len(ret) == 0 { + panic("Missing Return() function for VariadicMany") + } + var r0 error if rf, ok := ret.Get(0).(func(int, string, ...interface{}) error); ok { r0 = rf(i, a, intfs...) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go index c0b83854..9060e407 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go @@ -21,6 +21,10 @@ func (_m *ExpecterAndRolledVariadic) EXPECT() *ExpecterAndRolledVariadic_Expecte func (_m *ExpecterAndRolledVariadic) ManyArgsReturns(str string, i int) ([]string, error) { ret := _m.Called(str, i) + if len(ret) == 0 { + panic("Missing Return() function for ManyArgsReturns") + } + var r0 []string var r1 error if rf, ok := ret.Get(0).(func(string, int) ([]string, error)); ok { @@ -76,6 +80,10 @@ func (_c *ExpecterAndRolledVariadic_ManyArgsReturns_Call) RunAndReturn(run func( func (_m *ExpecterAndRolledVariadic) NoArg() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for NoArg") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() @@ -156,6 +164,10 @@ func (_m *ExpecterAndRolledVariadic) Variadic(ints ...int) error { } ret := tmpRet + if len(ret) == 0 { + panic("Missing Return() function for Variadic") + } + var r0 error if rf, ok := ret.Get(0).(func(...int) error); ok { r0 = rf(ints...) @@ -211,6 +223,10 @@ func (_m *ExpecterAndRolledVariadic) VariadicMany(i int, a string, intfs ...inte } ret := tmpRet + if len(ret) == 0 { + panic("Missing Return() function for VariadicMany") + } + var r0 error if rf, ok := ret.Get(0).(func(int, string, ...interface{}) error); ok { r0 = rf(i, a, intfs...) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go index 6d3f1338..38fb0681 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go @@ -54,6 +54,10 @@ func (_c *Fooer_Bar_Call) RunAndReturn(run func(func([]int))) *Fooer_Bar_Call { func (_m *Fooer) Baz(path string) func(string) string { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Baz") + } + var r0 func(string) string if rf, ok := ret.Get(0).(func(string) func(string) string); ok { r0 = rf(path) @@ -98,6 +102,10 @@ func (_c *Fooer_Baz_Call) RunAndReturn(run func(string) func(string) string) *Fo func (_m *Fooer) Foo(f func(string) string) error { ret := _m.Called(f) + if len(ret) == 0 { + panic("Missing Return() function for Foo") + } + var r0 error if rf, ok := ret.Get(0).(func(func(string) string) error); ok { r0 = rf(f) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go index 376dde63..c0872d14 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go @@ -21,6 +21,10 @@ func (_m *FuncArgsCollision) EXPECT() *FuncArgsCollision_Expecter { func (_m *FuncArgsCollision) Foo(ret interface{}) error { ret_1 := _m.Called(ret) + if len(ret_1) == 0 { + panic("Missing Return() function for Foo") + } + var r0 error if rf, ok := ret_1.Get(0).(func(interface{}) error); ok { r0 = rf(ret) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go index fb4498c8..f943eaa1 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go @@ -24,6 +24,10 @@ func (_m *GetGeneric[T]) EXPECT() *GetGeneric_Expecter[T] { func (_m *GetGeneric[T]) Get() T { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 T if rf, ok := ret.Get(0).(func() T); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go index 5dc1d005..e8a73206 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go @@ -21,6 +21,10 @@ func (_m *GetInt) EXPECT() *GetInt_Expecter { func (_m *GetInt) Get() int { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 int if rf, ok := ret.Get(0).(func() int); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go index 6930277c..7785d1fd 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go @@ -27,6 +27,10 @@ func (_m *HasConflictingNestedImports) EXPECT() *HasConflictingNestedImports_Exp func (_m *HasConflictingNestedImports) Get(path string) (http.Response, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 http.Response var r1 error if rf, ok := ret.Get(0).(func(string) (http.Response, error)); ok { @@ -79,6 +83,10 @@ func (_c *HasConflictingNestedImports_Get_Call) RunAndReturn(run func(string) (h func (_m *HasConflictingNestedImports) Z() fixtureshttp.MyStruct { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Z") + } + var r0 fixtureshttp.MyStruct if rf, ok := ret.Get(0).(func() fixtureshttp.MyStruct); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go index cb77c712..a9e4899d 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go @@ -26,6 +26,10 @@ func (_m *ImportsSameAsPackage) EXPECT() *ImportsSameAsPackage_Expecter { func (_m *ImportsSameAsPackage) A() test.B { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for A") + } + var r0 test.B if rf, ok := ret.Get(0).(func() test.B); ok { r0 = rf() @@ -67,6 +71,10 @@ func (_c *ImportsSameAsPackage_A_Call) RunAndReturn(run func() test.B) *ImportsS func (_m *ImportsSameAsPackage) B() fixtures.KeyManager { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for B") + } + var r0 fixtures.KeyManager if rf, ok := ret.Get(0).(func() fixtures.KeyManager); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go index d8ac6934..cd2a4261 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go @@ -24,6 +24,10 @@ func (_m *KeyManager) EXPECT() *KeyManager_Expecter { func (_m *KeyManager) GetKey(_a0 string, _a1 uint16) ([]byte, *test.Err) { ret := _m.Called(_a0, _a1) + if len(ret) == 0 { + panic("Missing Return() function for GetKey") + } + var r0 []byte var r1 *test.Err if rf, ok := ret.Get(0).(func(string, uint16) ([]byte, *test.Err)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go index 89c0e2f1..9635f5e3 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go @@ -21,6 +21,10 @@ func (_m *MapFunc) EXPECT() *MapFunc_Expecter { func (_m *MapFunc) Get(m map[string]func(string) string) error { ret := _m.Called(m) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 error if rf, ok := ret.Get(0).(func(map[string]func(string) string) error); ok { r0 = rf(m) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go index e2caf1d4..eae82a0b 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go @@ -21,6 +21,10 @@ func (_m *MyReader) EXPECT() *MyReader_Expecter { func (_m *MyReader) Read(p []byte) (int, error) { ret := _m.Called(p) + if len(ret) == 0 { + panic("Missing Return() function for Read") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func([]byte) (int, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go new file mode 100644 index 00000000..165aaa03 --- /dev/null +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go @@ -0,0 +1,77 @@ +// Code generated by mockery. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// PanicOnNoReturnValue is an autogenerated mock type for the PanicOnNoReturnValue type +type PanicOnNoReturnValue struct { + mock.Mock +} + +type PanicOnNoReturnValue_Expecter struct { + mock *mock.Mock +} + +func (_m *PanicOnNoReturnValue) EXPECT() *PanicOnNoReturnValue_Expecter { + return &PanicOnNoReturnValue_Expecter{mock: &_m.Mock} +} + +// DoSomething provides a mock function with given fields: +func (_m *PanicOnNoReturnValue) DoSomething() string { + ret := _m.Called() + + if len(ret) == 0 { + panic("Missing Return() function for DoSomething") + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// PanicOnNoReturnValue_DoSomething_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DoSomething' +type PanicOnNoReturnValue_DoSomething_Call struct { + *mock.Call +} + +// DoSomething is a helper method to define mock.On call +func (_e *PanicOnNoReturnValue_Expecter) DoSomething() *PanicOnNoReturnValue_DoSomething_Call { + return &PanicOnNoReturnValue_DoSomething_Call{Call: _e.mock.On("DoSomething")} +} + +func (_c *PanicOnNoReturnValue_DoSomething_Call) Run(run func()) *PanicOnNoReturnValue_DoSomething_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *PanicOnNoReturnValue_DoSomething_Call) Return(_a0 string) *PanicOnNoReturnValue_DoSomething_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *PanicOnNoReturnValue_DoSomething_Call) RunAndReturn(run func() string) *PanicOnNoReturnValue_DoSomething_Call { + _c.Call.Return(run) + return _c +} + +// NewPanicOnNoReturnValue creates a new instance of PanicOnNoReturnValue. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewPanicOnNoReturnValue(t interface { + mock.TestingT + Cleanup(func()) +}) *PanicOnNoReturnValue { + mock := &PanicOnNoReturnValue{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go index 9e38a0d1..7b253fc3 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go @@ -21,6 +21,10 @@ func (_m *Requester) EXPECT() *Requester_Expecter { func (_m *Requester) Get(path string) (string, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 string var r1 error if rf, ok := ret.Get(0).(func(string) (string, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go index 24b0d7a4..a0dcea9b 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go @@ -21,6 +21,10 @@ func (_m *Requester2) EXPECT() *Requester2_Expecter { func (_m *Requester2) Get(path string) error { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 error if rf, ok := ret.Get(0).(func(string) error); ok { r0 = rf(path) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go index 81f1cf34..1eb09b54 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go @@ -21,6 +21,10 @@ func (_m *Requester3) EXPECT() *Requester3_Expecter { func (_m *Requester3) Get() error { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 error if rf, ok := ret.Get(0).(func() error); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go index 4eab525d..0717bc27 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go @@ -25,6 +25,10 @@ func (_m *RequesterArgSameAsImport) EXPECT() *RequesterArgSameAsImport_Expecter func (_m *RequesterArgSameAsImport) Get(_a0 string) *json.RawMessage { ret := _m.Called(_a0) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 *json.RawMessage if rf, ok := ret.Get(0).(func(string) *json.RawMessage); ok { r0 = rf(_a0) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go index 28baf9bf..2cee7fee 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go @@ -25,6 +25,10 @@ func (_m *RequesterArgSameAsNamedImport) EXPECT() *RequesterArgSameAsNamedImport func (_m *RequesterArgSameAsNamedImport) Get(_a0 string) *json.RawMessage { ret := _m.Called(_a0) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 *json.RawMessage if rf, ok := ret.Get(0).(func(string) *json.RawMessage); ok { r0 = rf(_a0) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go index dbd40479..f10aa651 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go @@ -21,6 +21,10 @@ func (_m *RequesterArray) EXPECT() *RequesterArray_Expecter { func (_m *RequesterArray) Get(path string) ([2]string, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 [2]string var r1 error if rf, ok := ret.Get(0).(func(string) ([2]string, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go index 9d9b9623..b9a78588 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go @@ -21,6 +21,10 @@ func (_m *RequesterElided) EXPECT() *RequesterElided_Expecter { func (_m *RequesterElided) Get(path string, url string) error { ret := _m.Called(path, url) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 error if rf, ok := ret.Get(0).(func(string, string) error); ok { r0 = rf(path, url) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go index bb31f67c..46351dd0 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go @@ -37,6 +37,10 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG } { ret := _m.Called(_a0) + if len(ret) == 0 { + panic("Missing Return() function for GenericAnonymousStructs") + } + var r0 struct { Type2 test.GenericType[string, test.EmbeddedGet[int]] } @@ -92,6 +96,10 @@ func (_c *RequesterGenerics_GenericAnonymousStructs_Call[TAny, TComparable, TSig func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TGenIntf, TInlineType, TInlineTypeGeneric]) GenericArguments(_a0 TAny, _a1 TComparable) (TSigned, TIntf) { ret := _m.Called(_a0, _a1) + if len(ret) == 0 { + panic("Missing Return() function for GenericArguments") + } + var r0 TSigned var r1 TIntf if rf, ok := ret.Get(0).(func(TAny, TComparable) (TSigned, TIntf)); ok { @@ -148,6 +156,10 @@ func (_c *RequesterGenerics_GenericArguments_Call[TAny, TComparable, TSigned, TI func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TGenIntf, TInlineType, TInlineTypeGeneric]) GenericStructs(_a0 test.GenericType[TAny, TIntf]) test.GenericType[TSigned, TIntf] { ret := _m.Called(_a0) + if len(ret) == 0 { + panic("Missing Return() function for GenericStructs") + } + var r0 test.GenericType[TSigned, TIntf] if rf, ok := ret.Get(0).(func(test.GenericType[TAny, TIntf]) test.GenericType[TSigned, TIntf]); ok { r0 = rf(_a0) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go index 66e87c14..64135c69 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go @@ -25,6 +25,10 @@ func (_m *RequesterIface) EXPECT() *RequesterIface_Expecter { func (_m *RequesterIface) Get() io.Reader { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 io.Reader if rf, ok := ret.Get(0).(func() io.Reader); ok { r0 = rf() diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go index bf7c66c7..403535b5 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go @@ -25,6 +25,10 @@ func (_m *RequesterNS) EXPECT() *RequesterNS_Expecter { func (_m *RequesterNS) Get(path string) (http.Response, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 http.Response var r1 error if rf, ok := ret.Get(0).(func(string) (http.Response, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go index 3d210460..4cef8d4a 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go @@ -21,6 +21,10 @@ func (_m *RequesterPtr) EXPECT() *RequesterPtr_Expecter { func (_m *RequesterPtr) Get(path string) (*string, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 *string var r1 error if rf, ok := ret.Get(0).(func(string) (*string, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go index ada3692c..af1e48cd 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go @@ -21,6 +21,10 @@ func (_m *RequesterReturnElided) EXPECT() *RequesterReturnElided_Expecter { func (_m *RequesterReturnElided) Get(path string) (int, int, int, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 int var r1 int var r2 int @@ -87,6 +91,10 @@ func (_c *RequesterReturnElided_Get_Call) RunAndReturn(run func(string) (int, in func (_m *RequesterReturnElided) Put(path string) (int, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Put") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func(string) (int, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go index 74ac52f3..a753bdce 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go @@ -21,6 +21,10 @@ func (_m *RequesterSlice) EXPECT() *RequesterSlice_Expecter { func (_m *RequesterSlice) Get(path string) ([]string, error) { ret := _m.Called(path) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 []string var r1 error if rf, ok := ret.Get(0).(func(string) ([]string, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go index 94c3b1e0..fa80c6f7 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go @@ -23,6 +23,10 @@ func (_m *RequesterVariadic) Get(values ...string) bool { _ca = append(_ca, _va...) ret := _m.Called(_ca...) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 bool if rf, ok := ret.Get(0).(func(...string) bool); ok { r0 = rf(values...) @@ -44,6 +48,10 @@ func (_m *RequesterVariadic) MultiWriteToFile(filename string, w ...io.Writer) s _ca = append(_ca, _va...) ret := _m.Called(_ca...) + if len(ret) == 0 { + panic("Missing Return() function for MultiWriteToFile") + } + var r0 string if rf, ok := ret.Get(0).(func(string, ...io.Writer) string); ok { r0 = rf(filename, w...) @@ -60,6 +68,10 @@ func (_m *RequesterVariadic) OneInterface(a ...interface{}) bool { _ca = append(_ca, a...) ret := _m.Called(_ca...) + if len(ret) == 0 { + panic("Missing Return() function for OneInterface") + } + var r0 bool if rf, ok := ret.Get(0).(func(...interface{}) bool); ok { r0 = rf(a...) @@ -77,6 +89,10 @@ func (_m *RequesterVariadic) Sprintf(format string, a ...interface{}) string { _ca = append(_ca, a...) ret := _m.Called(_ca...) + if len(ret) == 0 { + panic("Missing Return() function for Sprintf") + } + var r0 string if rf, ok := ret.Get(0).(func(string, ...interface{}) string); ok { r0 = rf(format, a...) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go index 84f4d352..c5473cc8 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go @@ -17,6 +17,10 @@ type RequesterVariadicOneArgument struct { func (_m *RequesterVariadicOneArgument) Get(values ...string) bool { ret := _m.Called(values) + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 bool if rf, ok := ret.Get(0).(func(...string) bool); ok { r0 = rf(values...) @@ -31,6 +35,10 @@ func (_m *RequesterVariadicOneArgument) Get(values ...string) bool { func (_m *RequesterVariadicOneArgument) MultiWriteToFile(filename string, w ...io.Writer) string { ret := _m.Called(filename, w) + if len(ret) == 0 { + panic("Missing Return() function for MultiWriteToFile") + } + var r0 string if rf, ok := ret.Get(0).(func(string, ...io.Writer) string); ok { r0 = rf(filename, w...) @@ -45,6 +53,10 @@ func (_m *RequesterVariadicOneArgument) MultiWriteToFile(filename string, w ...i func (_m *RequesterVariadicOneArgument) OneInterface(a ...interface{}) bool { ret := _m.Called(a) + if len(ret) == 0 { + panic("Missing Return() function for OneInterface") + } + var r0 bool if rf, ok := ret.Get(0).(func(...interface{}) bool); ok { r0 = rf(a...) @@ -59,6 +71,10 @@ func (_m *RequesterVariadicOneArgument) OneInterface(a ...interface{}) bool { func (_m *RequesterVariadicOneArgument) Sprintf(format string, a ...interface{}) string { ret := _m.Called(format, a) + if len(ret) == 0 { + panic("Missing Return() function for Sprintf") + } + var r0 string if rf, ok := ret.Get(0).(func(string, ...interface{}) string); ok { r0 = rf(format, a...) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go index 4db2e677..7ca04e5c 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go @@ -25,6 +25,10 @@ func (_m *SendFunc) EXPECT() *SendFunc_Expecter { func (_m *SendFunc) Execute(ctx context.Context, data string) (int, error) { ret := _m.Called(ctx, data) + if len(ret) == 0 { + panic("Missing Return() function for Execute") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func(context.Context, string) (int, error)); ok { diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go index 498115de..96e4c89d 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go @@ -27,6 +27,10 @@ func (_m *StructWithTag) MethodA(v *struct { } { ret := _m.Called(v) + if len(ret) == 0 { + panic("Missing Return() function for MethodA") + } + var r0 *struct { FieldC int `json:"field_c"` FieldD int `json:"field_d" xml:"field_d"` diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go index db1e5a0b..6b331a50 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go @@ -21,6 +21,10 @@ func (_m *interfaceAMock) EXPECT() *interfaceAMock_Expecter { func (_m *interfaceAMock) DoB(interfaceB0 interfaceB) interfaceB { ret := _m.Called(interfaceB0) + if len(ret) == 0 { + panic("Missing Return() function for DoB") + } + var r0 interfaceB if rf, ok := ret.Get(0).(func(interfaceB) interfaceB); ok { r0 = rf(interfaceB0) @@ -65,6 +69,10 @@ func (_c *interfaceAMock_DoB_Call) RunAndReturn(run func(interfaceB) interfaceB) func (_m *interfaceAMock) DoB0(interfaceB interfaceB0) interfaceB0 { ret := _m.Called(interfaceB) + if len(ret) == 0 { + panic("Missing Return() function for DoB0") + } + var r0 interfaceB0 if rf, ok := ret.Get(0).(func(interfaceB0) interfaceB0); ok { r0 = rf(interfaceB) @@ -109,6 +117,10 @@ func (_c *interfaceAMock_DoB0_Call) RunAndReturn(run func(interfaceB0) interface func (_m *interfaceAMock) DoB0v2(interfaceB00 interfaceB0) interfaceB0 { ret := _m.Called(interfaceB00) + if len(ret) == 0 { + panic("Missing Return() function for DoB0v2") + } + var r0 interfaceB0 if rf, ok := ret.Get(0).(func(interfaceB0) interfaceB0); ok { r0 = rf(interfaceB00) diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go index 65bdfd1d..f5e86573 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go @@ -21,6 +21,10 @@ func (_m *interfaceB0Mock) EXPECT() *interfaceB0Mock_Expecter { func (_m *interfaceB0Mock) DoB0(interfaceB00 interfaceB0) interfaceB0 { ret := _m.Called(interfaceB00) + if len(ret) == 0 { + panic("Missing Return() function for DoB0") + } + var r0 interfaceB0 if rf, ok := ret.Get(0).(func(interfaceB0) interfaceB0); ok { r0 = rf(interfaceB00) diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go index f01014df..9fbb63c8 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go @@ -21,6 +21,10 @@ func (_m *interfaceBMock) EXPECT() *interfaceBMock_Expecter { func (_m *interfaceBMock) GetData() int { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for GetData") + } + var r0 int if rf, ok := ret.Get(0).(func() int); ok { r0 = rf() diff --git a/pkg/fixtures/panic_err.go b/pkg/fixtures/panic_err.go new file mode 100644 index 00000000..ac646619 --- /dev/null +++ b/pkg/fixtures/panic_err.go @@ -0,0 +1,5 @@ +package test + +type PanicOnNoReturnValue interface { + DoSomething() string +} diff --git a/pkg/fixtures/recursive_generation/Foo_mock.go b/pkg/fixtures/recursive_generation/Foo_mock.go index b467ac42..8227b876 100644 --- a/pkg/fixtures/recursive_generation/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/Foo_mock.go @@ -21,6 +21,10 @@ func (_m *MockFoo) EXPECT() *MockFoo_Expecter { func (_m *MockFoo) Get() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() diff --git a/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go index ab666e61..0cc31d1a 100644 --- a/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go @@ -21,6 +21,10 @@ func (_m *MockFoo) EXPECT() *MockFoo_Expecter { func (_m *MockFoo) Get() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() diff --git a/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go index b74c986d..b9b1b4b2 100644 --- a/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go @@ -21,6 +21,10 @@ func (_m *MockFoo) EXPECT() *MockFoo_Expecter { func (_m *MockFoo) Get() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() diff --git a/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go index c7027e16..3b040618 100644 --- a/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go @@ -21,6 +21,10 @@ func (_m *MockFoo) EXPECT() *MockFoo_Expecter { func (_m *MockFoo) Get() string { ret := _m.Called() + if len(ret) == 0 { + panic("Missing Return() function for Get") + } + var r0 string if rf, ok := ret.Get(0).(func() string); ok { r0 = rf() diff --git a/pkg/generator.go b/pkg/generator.go index 8f5985a6..8a7702f0 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -752,6 +752,10 @@ func (_m *{{.MockName}}{{.InstantiatedTypeString}}) {{.FunctionName}}({{join .Pa {{- else}} {{- .RetVariableName}} := {{.Called}} + if len({{.RetVariableName}}) == 0 { + panic("Missing Return() function for {{.FunctionName}}") + } + {{range $idx, $name := .Returns.ReturnNames}} var {{$name}} {{index $.Returns.Types $idx -}} {{end}} diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 5ea54b5b..50a0f5ef 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -517,6 +517,10 @@ func (s *GeneratorSuite) TestGeneratorForEmptyInterface() { s.checkGeneration("empty_interface.go", "Blank", false, "", "") } +func (s *GeneratorSuite) TestGeneratorForPanicOnNoReturnValueInterface() { + s.checkGeneration("panic_err.go", "PanicOnNoReturnValue", false, "", "") +} + func (s *GeneratorSuite) TestGeneratorArgumentIsMapFunc() { s.checkGeneration("map_func.go", "MapFunc", false, "", "") } From 8e778e017d1ad930136cf6d2ae795e52c38bec76 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 20 Nov 2023 17:22:28 -0300 Subject: [PATCH 19/30] created a new test to cover the new condition when the test panics #729 --- .../vektra/mockery/v2/pkg/TypesPackage.go | 4 ++-- .../vektra/mockery/v2/pkg/fixtures/A.go | 2 +- .../mockery/v2/pkg/fixtures/AsyncProducer.go | 6 ++--- .../vektra/mockery/v2/pkg/fixtures/Blank.go | 2 +- .../mockery/v2/pkg/fixtures/ConsulLock.go | 4 ++-- .../mockery/v2/pkg/fixtures/EmbeddedGet.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/Example.go | 4 ++-- .../mockery/v2/pkg/fixtures/Expecter.go | 8 +++---- .../pkg/fixtures/ExpecterAndRolledVariadic.go | 8 +++---- .../vektra/mockery/v2/pkg/fixtures/Fooer.go | 4 ++-- .../v2/pkg/fixtures/FuncArgsCollision.go | 2 +- .../mockery/v2/pkg/fixtures/GetGeneric.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/GetInt.go | 2 +- .../fixtures/HasConflictingNestedImports.go | 4 ++-- .../v2/pkg/fixtures/ImportsSameAsPackage.go | 4 ++-- .../mockery/v2/pkg/fixtures/KeyManager.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/MapFunc.go | 2 +- .../mockery/v2/pkg/fixtures/MyReader.go | 2 +- .../v2/pkg/fixtures/PanicOnNoReturnValue.go | 2 +- .../mockery/v2/pkg/fixtures/Requester.go | 2 +- .../mockery/v2/pkg/fixtures/Requester2.go | 2 +- .../mockery/v2/pkg/fixtures/Requester3.go | 2 +- .../pkg/fixtures/RequesterArgSameAsImport.go | 2 +- .../fixtures/RequesterArgSameAsNamedImport.go | 2 +- .../mockery/v2/pkg/fixtures/RequesterArray.go | 2 +- .../v2/pkg/fixtures/RequesterElided.go | 2 +- .../v2/pkg/fixtures/RequesterGenerics.go | 6 ++--- .../mockery/v2/pkg/fixtures/RequesterIface.go | 2 +- .../mockery/v2/pkg/fixtures/RequesterNS.go | 2 +- .../mockery/v2/pkg/fixtures/RequesterPtr.go | 2 +- .../v2/pkg/fixtures/RequesterReturnElided.go | 4 ++-- .../mockery/v2/pkg/fixtures/RequesterSlice.go | 2 +- .../v2/pkg/fixtures/RequesterVariadic.go | 8 +++---- .../fixtures/RequesterVariadicOneArgument.go | 8 +++---- .../mockery/v2/pkg/fixtures/SendFunc.go | 2 +- .../mockery/v2/pkg/fixtures/StructWithTag.go | 2 +- .../mock_interfaceA_test.go | 6 ++--- .../mock_interfaceB0_test.go | 2 +- .../mock_interfaceB_test.go | 2 +- pkg/fixtures/recursive_generation/Foo_mock.go | 2 +- .../recursive_generation/subpkg1/Foo_mock.go | 2 +- .../recursive_generation/subpkg2/Foo_mock.go | 2 +- .../Foo_mock.go | 2 +- .../test/panic_on_no_return_value_test.go | 23 +++++++++++++++++++ pkg/generator.go | 2 +- pkg/generator_test.go | 4 ---- 46 files changed, 92 insertions(+), 73 deletions(-) create mode 100644 pkg/fixtures/test/panic_on_no_return_value_test.go diff --git a/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go b/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go index 44f96296..4acb2052 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go @@ -22,7 +22,7 @@ func (_m *TypesPackage) Name() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Name") + panic("Missing Return() function for Name()") } var r0 string @@ -67,7 +67,7 @@ func (_m *TypesPackage) Path() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Path") + panic("Missing Return() function for Path()") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go index c7682dea..90e69f03 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go @@ -25,7 +25,7 @@ func (_m *A) Call() (test.B, error) { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Call") + panic("Missing Return() function for Call()") } var r0 test.B diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go index e2f21751..a2750ff8 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go @@ -22,7 +22,7 @@ func (_m *AsyncProducer) Input() chan<- bool { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Input") + panic("Missing Return() function for Input()") } var r0 chan<- bool @@ -69,7 +69,7 @@ func (_m *AsyncProducer) Output() <-chan bool { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Output") + panic("Missing Return() function for Output()") } var r0 <-chan bool @@ -116,7 +116,7 @@ func (_m *AsyncProducer) Whatever() chan bool { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Whatever") + panic("Missing Return() function for Whatever()") } var r0 chan bool diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go index 50a08902..517b0c4f 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go @@ -22,7 +22,7 @@ func (_m *Blank) Create(x interface{}) error { ret := _m.Called(x) if len(ret) == 0 { - panic("Missing Return() function for Create") + panic("Missing Return() function for Create()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go index 9bdeb62a..0cff9230 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go @@ -22,7 +22,7 @@ func (_m *ConsulLock) Lock(_a0 <-chan struct{}) (<-chan struct{}, error) { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for Lock") + panic("Missing Return() function for Lock()") } var r0 <-chan struct{} @@ -80,7 +80,7 @@ func (_m *ConsulLock) Unlock() error { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Unlock") + panic("Missing Return() function for Unlock()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go index 1954d434..acd10c98 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go @@ -25,7 +25,7 @@ func (_m *EmbeddedGet[T]) Get() T { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 T diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go index a67b163d..deac5988 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go @@ -28,7 +28,7 @@ func (_m *Example) A() http.Flusher { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for A") + panic("Missing Return() function for A()") } var r0 http.Flusher @@ -75,7 +75,7 @@ func (_m *Example) B(_a0 string) fixtureshttp.MyStruct { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for B") + panic("Missing Return() function for B()") } var r0 fixtureshttp.MyStruct diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go index 69aec464..f39cb16e 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go @@ -22,7 +22,7 @@ func (_m *Expecter) ManyArgsReturns(str string, i int) ([]string, error) { ret := _m.Called(str, i) if len(ret) == 0 { - panic("Missing Return() function for ManyArgsReturns") + panic("Missing Return() function for ManyArgsReturns()") } var r0 []string @@ -81,7 +81,7 @@ func (_m *Expecter) NoArg() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for NoArg") + panic("Missing Return() function for NoArg()") } var r0 string @@ -165,7 +165,7 @@ func (_m *Expecter) Variadic(ints ...int) error { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for Variadic") + panic("Missing Return() function for Variadic()") } var r0 error @@ -221,7 +221,7 @@ func (_m *Expecter) VariadicMany(i int, a string, intfs ...interface{}) error { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for VariadicMany") + panic("Missing Return() function for VariadicMany()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go index 9060e407..9e32afb9 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go @@ -22,7 +22,7 @@ func (_m *ExpecterAndRolledVariadic) ManyArgsReturns(str string, i int) ([]strin ret := _m.Called(str, i) if len(ret) == 0 { - panic("Missing Return() function for ManyArgsReturns") + panic("Missing Return() function for ManyArgsReturns()") } var r0 []string @@ -81,7 +81,7 @@ func (_m *ExpecterAndRolledVariadic) NoArg() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for NoArg") + panic("Missing Return() function for NoArg()") } var r0 string @@ -165,7 +165,7 @@ func (_m *ExpecterAndRolledVariadic) Variadic(ints ...int) error { ret := tmpRet if len(ret) == 0 { - panic("Missing Return() function for Variadic") + panic("Missing Return() function for Variadic()") } var r0 error @@ -224,7 +224,7 @@ func (_m *ExpecterAndRolledVariadic) VariadicMany(i int, a string, intfs ...inte ret := tmpRet if len(ret) == 0 { - panic("Missing Return() function for VariadicMany") + panic("Missing Return() function for VariadicMany()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go index 38fb0681..17877aad 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go @@ -55,7 +55,7 @@ func (_m *Fooer) Baz(path string) func(string) string { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Baz") + panic("Missing Return() function for Baz()") } var r0 func(string) string @@ -103,7 +103,7 @@ func (_m *Fooer) Foo(f func(string) string) error { ret := _m.Called(f) if len(ret) == 0 { - panic("Missing Return() function for Foo") + panic("Missing Return() function for Foo()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go index c0872d14..83b5864b 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go @@ -22,7 +22,7 @@ func (_m *FuncArgsCollision) Foo(ret interface{}) error { ret_1 := _m.Called(ret) if len(ret_1) == 0 { - panic("Missing Return() function for Foo") + panic("Missing Return() function for Foo()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go index f943eaa1..4bec1a73 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go @@ -25,7 +25,7 @@ func (_m *GetGeneric[T]) Get() T { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 T diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go index e8a73206..7656765a 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go @@ -22,7 +22,7 @@ func (_m *GetInt) Get() int { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go index 7785d1fd..e9f11365 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go @@ -28,7 +28,7 @@ func (_m *HasConflictingNestedImports) Get(path string) (http.Response, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 http.Response @@ -84,7 +84,7 @@ func (_m *HasConflictingNestedImports) Z() fixtureshttp.MyStruct { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Z") + panic("Missing Return() function for Z()") } var r0 fixtureshttp.MyStruct diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go index a9e4899d..584582c8 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go @@ -27,7 +27,7 @@ func (_m *ImportsSameAsPackage) A() test.B { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for A") + panic("Missing Return() function for A()") } var r0 test.B @@ -72,7 +72,7 @@ func (_m *ImportsSameAsPackage) B() fixtures.KeyManager { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for B") + panic("Missing Return() function for B()") } var r0 fixtures.KeyManager diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go index cd2a4261..450e2cb4 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go @@ -25,7 +25,7 @@ func (_m *KeyManager) GetKey(_a0 string, _a1 uint16) ([]byte, *test.Err) { ret := _m.Called(_a0, _a1) if len(ret) == 0 { - panic("Missing Return() function for GetKey") + panic("Missing Return() function for GetKey()") } var r0 []byte diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go index 9635f5e3..ed8a6d82 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go @@ -22,7 +22,7 @@ func (_m *MapFunc) Get(m map[string]func(string) string) error { ret := _m.Called(m) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go index eae82a0b..1636433b 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go @@ -22,7 +22,7 @@ func (_m *MyReader) Read(p []byte) (int, error) { ret := _m.Called(p) if len(ret) == 0 { - panic("Missing Return() function for Read") + panic("Missing Return() function for Read()") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go index 165aaa03..06d86f96 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go @@ -22,7 +22,7 @@ func (_m *PanicOnNoReturnValue) DoSomething() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for DoSomething") + panic("Missing Return() function for DoSomething()") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go index 7b253fc3..7c585f68 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go @@ -22,7 +22,7 @@ func (_m *Requester) Get(path string) (string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go index a0dcea9b..f31860b4 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go @@ -22,7 +22,7 @@ func (_m *Requester2) Get(path string) error { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go index 1eb09b54..68e3ce6c 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go @@ -22,7 +22,7 @@ func (_m *Requester3) Get() error { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go index 0717bc27..032f9c9d 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go @@ -26,7 +26,7 @@ func (_m *RequesterArgSameAsImport) Get(_a0 string) *json.RawMessage { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 *json.RawMessage diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go index 2cee7fee..314a2415 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go @@ -26,7 +26,7 @@ func (_m *RequesterArgSameAsNamedImport) Get(_a0 string) *json.RawMessage { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 *json.RawMessage diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go index f10aa651..cff9952f 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go @@ -22,7 +22,7 @@ func (_m *RequesterArray) Get(path string) ([2]string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 [2]string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go index b9a78588..961c4e9b 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go @@ -22,7 +22,7 @@ func (_m *RequesterElided) Get(path string, url string) error { ret := _m.Called(path, url) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go index 46351dd0..2cbd4925 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go @@ -38,7 +38,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for GenericAnonymousStructs") + panic("Missing Return() function for GenericAnonymousStructs()") } var r0 struct { @@ -97,7 +97,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG ret := _m.Called(_a0, _a1) if len(ret) == 0 { - panic("Missing Return() function for GenericArguments") + panic("Missing Return() function for GenericArguments()") } var r0 TSigned @@ -157,7 +157,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for GenericStructs") + panic("Missing Return() function for GenericStructs()") } var r0 test.GenericType[TSigned, TIntf] diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go index 64135c69..97c7edc2 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go @@ -26,7 +26,7 @@ func (_m *RequesterIface) Get() io.Reader { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 io.Reader diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go index 403535b5..14b80948 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go @@ -26,7 +26,7 @@ func (_m *RequesterNS) Get(path string) (http.Response, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 http.Response diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go index 4cef8d4a..208b89d1 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go @@ -22,7 +22,7 @@ func (_m *RequesterPtr) Get(path string) (*string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 *string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go index af1e48cd..c311e288 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go @@ -22,7 +22,7 @@ func (_m *RequesterReturnElided) Get(path string) (int, int, int, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 int @@ -92,7 +92,7 @@ func (_m *RequesterReturnElided) Put(path string) (int, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Put") + panic("Missing Return() function for Put()") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go index a753bdce..86a0436d 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go @@ -22,7 +22,7 @@ func (_m *RequesterSlice) Get(path string) ([]string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 []string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go index fa80c6f7..13ae0385 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go @@ -24,7 +24,7 @@ func (_m *RequesterVariadic) Get(values ...string) bool { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 bool @@ -49,7 +49,7 @@ func (_m *RequesterVariadic) MultiWriteToFile(filename string, w ...io.Writer) s ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for MultiWriteToFile") + panic("Missing Return() function for MultiWriteToFile()") } var r0 string @@ -69,7 +69,7 @@ func (_m *RequesterVariadic) OneInterface(a ...interface{}) bool { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for OneInterface") + panic("Missing Return() function for OneInterface()") } var r0 bool @@ -90,7 +90,7 @@ func (_m *RequesterVariadic) Sprintf(format string, a ...interface{}) string { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for Sprintf") + panic("Missing Return() function for Sprintf()") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go index c5473cc8..d6bbd4a0 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go @@ -18,7 +18,7 @@ func (_m *RequesterVariadicOneArgument) Get(values ...string) bool { ret := _m.Called(values) if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 bool @@ -36,7 +36,7 @@ func (_m *RequesterVariadicOneArgument) MultiWriteToFile(filename string, w ...i ret := _m.Called(filename, w) if len(ret) == 0 { - panic("Missing Return() function for MultiWriteToFile") + panic("Missing Return() function for MultiWriteToFile()") } var r0 string @@ -54,7 +54,7 @@ func (_m *RequesterVariadicOneArgument) OneInterface(a ...interface{}) bool { ret := _m.Called(a) if len(ret) == 0 { - panic("Missing Return() function for OneInterface") + panic("Missing Return() function for OneInterface()") } var r0 bool @@ -72,7 +72,7 @@ func (_m *RequesterVariadicOneArgument) Sprintf(format string, a ...interface{}) ret := _m.Called(format, a) if len(ret) == 0 { - panic("Missing Return() function for Sprintf") + panic("Missing Return() function for Sprintf()") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go index 7ca04e5c..283f7c9f 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go @@ -26,7 +26,7 @@ func (_m *SendFunc) Execute(ctx context.Context, data string) (int, error) { ret := _m.Called(ctx, data) if len(ret) == 0 { - panic("Missing Return() function for Execute") + panic("Missing Return() function for Execute()") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go index 96e4c89d..db572ea1 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go @@ -28,7 +28,7 @@ func (_m *StructWithTag) MethodA(v *struct { ret := _m.Called(v) if len(ret) == 0 { - panic("Missing Return() function for MethodA") + panic("Missing Return() function for MethodA()") } var r0 *struct { diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go index 6b331a50..f0daa351 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go @@ -22,7 +22,7 @@ func (_m *interfaceAMock) DoB(interfaceB0 interfaceB) interfaceB { ret := _m.Called(interfaceB0) if len(ret) == 0 { - panic("Missing Return() function for DoB") + panic("Missing Return() function for DoB()") } var r0 interfaceB @@ -70,7 +70,7 @@ func (_m *interfaceAMock) DoB0(interfaceB interfaceB0) interfaceB0 { ret := _m.Called(interfaceB) if len(ret) == 0 { - panic("Missing Return() function for DoB0") + panic("Missing Return() function for DoB0()") } var r0 interfaceB0 @@ -118,7 +118,7 @@ func (_m *interfaceAMock) DoB0v2(interfaceB00 interfaceB0) interfaceB0 { ret := _m.Called(interfaceB00) if len(ret) == 0 { - panic("Missing Return() function for DoB0v2") + panic("Missing Return() function for DoB0v2()") } var r0 interfaceB0 diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go index f5e86573..22718121 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go @@ -22,7 +22,7 @@ func (_m *interfaceB0Mock) DoB0(interfaceB00 interfaceB0) interfaceB0 { ret := _m.Called(interfaceB00) if len(ret) == 0 { - panic("Missing Return() function for DoB0") + panic("Missing Return() function for DoB0()") } var r0 interfaceB0 diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go index 9fbb63c8..7bfad951 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go @@ -22,7 +22,7 @@ func (_m *interfaceBMock) GetData() int { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for GetData") + panic("Missing Return() function for GetData()") } var r0 int diff --git a/pkg/fixtures/recursive_generation/Foo_mock.go b/pkg/fixtures/recursive_generation/Foo_mock.go index 8227b876..d24cd151 100644 --- a/pkg/fixtures/recursive_generation/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 string diff --git a/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go index 0cc31d1a..99df0b9f 100644 --- a/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 string diff --git a/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go index b9b1b4b2..898ddbf6 100644 --- a/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 string diff --git a/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go index 3b040618..3e4a2879 100644 --- a/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get") + panic("Missing Return() function for Get()") } var r0 string diff --git a/pkg/fixtures/test/panic_on_no_return_value_test.go b/pkg/fixtures/test/panic_on_no_return_value_test.go new file mode 100644 index 00000000..bbc4638d --- /dev/null +++ b/pkg/fixtures/test/panic_on_no_return_value_test.go @@ -0,0 +1,23 @@ +package test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + mocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg/fixtures" +) + +func TestPanicOnNoReturnValue(t *testing.T) { + m := mocks.NewPanicOnNoReturnValue(t) + m.EXPECT().DoSomething() + + defer func() { + if r := recover(); r != nil { + assert.NotNil(t, r) + assert.Equal(t, "Missing Return() function for DoSomething()", r.(string)) + } + }() + + m.DoSomething() + +} diff --git a/pkg/generator.go b/pkg/generator.go index 8a7702f0..b13e78ed 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -753,7 +753,7 @@ func (_m *{{.MockName}}{{.InstantiatedTypeString}}) {{.FunctionName}}({{join .Pa {{- .RetVariableName}} := {{.Called}} if len({{.RetVariableName}}) == 0 { - panic("Missing Return() function for {{.FunctionName}}") + panic("Missing Return() function for {{.FunctionName}}()") } {{range $idx, $name := .Returns.ReturnNames}} diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 50a0f5ef..5ea54b5b 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -517,10 +517,6 @@ func (s *GeneratorSuite) TestGeneratorForEmptyInterface() { s.checkGeneration("empty_interface.go", "Blank", false, "", "") } -func (s *GeneratorSuite) TestGeneratorForPanicOnNoReturnValueInterface() { - s.checkGeneration("panic_err.go", "PanicOnNoReturnValue", false, "", "") -} - func (s *GeneratorSuite) TestGeneratorArgumentIsMapFunc() { s.checkGeneration("map_func.go", "MapFunc", false, "", "") } From 432134cf1d0b601c60f144c12ddc2dbbcda1f9ab Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 20 Nov 2023 19:07:43 -0300 Subject: [PATCH 20/30] improvements after code review #729 --- mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go | 4 ++-- mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/AsyncProducer.go | 6 +++--- .../github.com/vektra/mockery/v2/pkg/fixtures/Blank.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/ConsulLock.go | 4 ++-- .../vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/Example.go | 4 ++-- .../vektra/mockery/v2/pkg/fixtures/Expecter.go | 8 ++++---- .../v2/pkg/fixtures/ExpecterAndRolledVariadic.go | 8 ++++---- .../github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go | 4 ++-- .../mockery/v2/pkg/fixtures/FuncArgsCollision.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/GetGeneric.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/GetInt.go | 2 +- .../v2/pkg/fixtures/HasConflictingNestedImports.go | 4 ++-- .../mockery/v2/pkg/fixtures/ImportsSameAsPackage.go | 4 ++-- .../vektra/mockery/v2/pkg/fixtures/KeyManager.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/MapFunc.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/MyReader.go | 2 +- .../mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/Requester.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/Requester2.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/Requester3.go | 2 +- .../v2/pkg/fixtures/RequesterArgSameAsImport.go | 2 +- .../v2/pkg/fixtures/RequesterArgSameAsNamedImport.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/RequesterArray.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/RequesterElided.go | 2 +- .../mockery/v2/pkg/fixtures/RequesterGenerics.go | 6 +++--- .../vektra/mockery/v2/pkg/fixtures/RequesterIface.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/RequesterNS.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/RequesterPtr.go | 2 +- .../mockery/v2/pkg/fixtures/RequesterReturnElided.go | 4 ++-- .../vektra/mockery/v2/pkg/fixtures/RequesterSlice.go | 2 +- .../mockery/v2/pkg/fixtures/RequesterVariadic.go | 8 ++++---- .../v2/pkg/fixtures/RequesterVariadicOneArgument.go | 8 ++++---- .../vektra/mockery/v2/pkg/fixtures/SendFunc.go | 2 +- .../vektra/mockery/v2/pkg/fixtures/StructWithTag.go | 2 +- .../same_name_arg_and_type/mock_interfaceA_test.go | 6 +++--- .../same_name_arg_and_type/mock_interfaceB0_test.go | 2 +- .../same_name_arg_and_type/mock_interfaceB_test.go | 2 +- pkg/fixtures/recursive_generation/Foo_mock.go | 2 +- pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go | 2 +- pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go | 2 +- .../subpkg_with_only_autogenerated_files/Foo_mock.go | 2 +- pkg/fixtures/test/panic_on_no_return_value_test.go | 10 ++++++++-- pkg/generator.go | 2 +- 45 files changed, 77 insertions(+), 71 deletions(-) diff --git a/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go b/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go index 4acb2052..dc30f202 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/TypesPackage.go @@ -22,7 +22,7 @@ func (_m *TypesPackage) Name() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Name()") + panic("no return value specified for Name") } var r0 string @@ -67,7 +67,7 @@ func (_m *TypesPackage) Path() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Path()") + panic("no return value specified for Path") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go index 90e69f03..afbdf941 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/A.go @@ -25,7 +25,7 @@ func (_m *A) Call() (test.B, error) { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Call()") + panic("no return value specified for Call") } var r0 test.B diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go index a2750ff8..bef64eca 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/AsyncProducer.go @@ -22,7 +22,7 @@ func (_m *AsyncProducer) Input() chan<- bool { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Input()") + panic("no return value specified for Input") } var r0 chan<- bool @@ -69,7 +69,7 @@ func (_m *AsyncProducer) Output() <-chan bool { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Output()") + panic("no return value specified for Output") } var r0 <-chan bool @@ -116,7 +116,7 @@ func (_m *AsyncProducer) Whatever() chan bool { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Whatever()") + panic("no return value specified for Whatever") } var r0 chan bool diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go index 517b0c4f..f48ecb9c 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Blank.go @@ -22,7 +22,7 @@ func (_m *Blank) Create(x interface{}) error { ret := _m.Called(x) if len(ret) == 0 { - panic("Missing Return() function for Create()") + panic("no return value specified for Create") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go index 0cff9230..2dfe5dc6 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ConsulLock.go @@ -22,7 +22,7 @@ func (_m *ConsulLock) Lock(_a0 <-chan struct{}) (<-chan struct{}, error) { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for Lock()") + panic("no return value specified for Lock") } var r0 <-chan struct{} @@ -80,7 +80,7 @@ func (_m *ConsulLock) Unlock() error { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Unlock()") + panic("no return value specified for Unlock") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go index acd10c98..a87f878a 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/EmbeddedGet.go @@ -25,7 +25,7 @@ func (_m *EmbeddedGet[T]) Get() T { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 T diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go index deac5988..382004a6 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Example.go @@ -28,7 +28,7 @@ func (_m *Example) A() http.Flusher { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for A()") + panic("no return value specified for A") } var r0 http.Flusher @@ -75,7 +75,7 @@ func (_m *Example) B(_a0 string) fixtureshttp.MyStruct { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for B()") + panic("no return value specified for B") } var r0 fixtureshttp.MyStruct diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go index f39cb16e..2807c78d 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Expecter.go @@ -22,7 +22,7 @@ func (_m *Expecter) ManyArgsReturns(str string, i int) ([]string, error) { ret := _m.Called(str, i) if len(ret) == 0 { - panic("Missing Return() function for ManyArgsReturns()") + panic("no return value specified for ManyArgsReturns") } var r0 []string @@ -81,7 +81,7 @@ func (_m *Expecter) NoArg() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for NoArg()") + panic("no return value specified for NoArg") } var r0 string @@ -165,7 +165,7 @@ func (_m *Expecter) Variadic(ints ...int) error { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for Variadic()") + panic("no return value specified for Variadic") } var r0 error @@ -221,7 +221,7 @@ func (_m *Expecter) VariadicMany(i int, a string, intfs ...interface{}) error { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for VariadicMany()") + panic("no return value specified for VariadicMany") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go index 9e32afb9..6fd6ffa0 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ExpecterAndRolledVariadic.go @@ -22,7 +22,7 @@ func (_m *ExpecterAndRolledVariadic) ManyArgsReturns(str string, i int) ([]strin ret := _m.Called(str, i) if len(ret) == 0 { - panic("Missing Return() function for ManyArgsReturns()") + panic("no return value specified for ManyArgsReturns") } var r0 []string @@ -81,7 +81,7 @@ func (_m *ExpecterAndRolledVariadic) NoArg() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for NoArg()") + panic("no return value specified for NoArg") } var r0 string @@ -165,7 +165,7 @@ func (_m *ExpecterAndRolledVariadic) Variadic(ints ...int) error { ret := tmpRet if len(ret) == 0 { - panic("Missing Return() function for Variadic()") + panic("no return value specified for Variadic") } var r0 error @@ -224,7 +224,7 @@ func (_m *ExpecterAndRolledVariadic) VariadicMany(i int, a string, intfs ...inte ret := tmpRet if len(ret) == 0 { - panic("Missing Return() function for VariadicMany()") + panic("no return value specified for VariadicMany") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go index 17877aad..c172417b 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Fooer.go @@ -55,7 +55,7 @@ func (_m *Fooer) Baz(path string) func(string) string { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Baz()") + panic("no return value specified for Baz") } var r0 func(string) string @@ -103,7 +103,7 @@ func (_m *Fooer) Foo(f func(string) string) error { ret := _m.Called(f) if len(ret) == 0 { - panic("Missing Return() function for Foo()") + panic("no return value specified for Foo") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go index 83b5864b..493844c8 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/FuncArgsCollision.go @@ -22,7 +22,7 @@ func (_m *FuncArgsCollision) Foo(ret interface{}) error { ret_1 := _m.Called(ret) if len(ret_1) == 0 { - panic("Missing Return() function for Foo()") + panic("no return value specified for Foo") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go index 4bec1a73..fadb46f7 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetGeneric.go @@ -25,7 +25,7 @@ func (_m *GetGeneric[T]) Get() T { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 T diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go index 7656765a..0eeb3b4f 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/GetInt.go @@ -22,7 +22,7 @@ func (_m *GetInt) Get() int { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go index e9f11365..7249de4f 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/HasConflictingNestedImports.go @@ -28,7 +28,7 @@ func (_m *HasConflictingNestedImports) Get(path string) (http.Response, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 http.Response @@ -84,7 +84,7 @@ func (_m *HasConflictingNestedImports) Z() fixtureshttp.MyStruct { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Z()") + panic("no return value specified for Z") } var r0 fixtureshttp.MyStruct diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go index 584582c8..e8045f24 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/ImportsSameAsPackage.go @@ -27,7 +27,7 @@ func (_m *ImportsSameAsPackage) A() test.B { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for A()") + panic("no return value specified for A") } var r0 test.B @@ -72,7 +72,7 @@ func (_m *ImportsSameAsPackage) B() fixtures.KeyManager { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for B()") + panic("no return value specified for B") } var r0 fixtures.KeyManager diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go index 450e2cb4..e645861c 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/KeyManager.go @@ -25,7 +25,7 @@ func (_m *KeyManager) GetKey(_a0 string, _a1 uint16) ([]byte, *test.Err) { ret := _m.Called(_a0, _a1) if len(ret) == 0 { - panic("Missing Return() function for GetKey()") + panic("no return value specified for GetKey") } var r0 []byte diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go index ed8a6d82..6ca90bd6 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MapFunc.go @@ -22,7 +22,7 @@ func (_m *MapFunc) Get(m map[string]func(string) string) error { ret := _m.Called(m) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go index 1636433b..c2bf9c29 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/MyReader.go @@ -22,7 +22,7 @@ func (_m *MyReader) Read(p []byte) (int, error) { ret := _m.Called(p) if len(ret) == 0 { - panic("Missing Return() function for Read()") + panic("no return value specified for Read") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go index 06d86f96..eaa5ea6e 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/PanicOnNoReturnValue.go @@ -22,7 +22,7 @@ func (_m *PanicOnNoReturnValue) DoSomething() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for DoSomething()") + panic("no return value specified for DoSomething") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go index 7c585f68..ad20a1c2 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester.go @@ -22,7 +22,7 @@ func (_m *Requester) Get(path string) (string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go index f31860b4..d057dd32 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester2.go @@ -22,7 +22,7 @@ func (_m *Requester2) Get(path string) error { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go index 68e3ce6c..2c18c927 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/Requester3.go @@ -22,7 +22,7 @@ func (_m *Requester3) Get() error { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go index 032f9c9d..153d49c2 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsImport.go @@ -26,7 +26,7 @@ func (_m *RequesterArgSameAsImport) Get(_a0 string) *json.RawMessage { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 *json.RawMessage diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go index 314a2415..fd783552 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArgSameAsNamedImport.go @@ -26,7 +26,7 @@ func (_m *RequesterArgSameAsNamedImport) Get(_a0 string) *json.RawMessage { ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 *json.RawMessage diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go index cff9952f..00e8f1de 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterArray.go @@ -22,7 +22,7 @@ func (_m *RequesterArray) Get(path string) ([2]string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 [2]string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go index 961c4e9b..bfc921a1 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterElided.go @@ -22,7 +22,7 @@ func (_m *RequesterElided) Get(path string, url string) error { ret := _m.Called(path, url) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 error diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go index 2cbd4925..c813b557 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterGenerics.go @@ -38,7 +38,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for GenericAnonymousStructs()") + panic("no return value specified for GenericAnonymousStructs") } var r0 struct { @@ -97,7 +97,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG ret := _m.Called(_a0, _a1) if len(ret) == 0 { - panic("Missing Return() function for GenericArguments()") + panic("no return value specified for GenericArguments") } var r0 TSigned @@ -157,7 +157,7 @@ func (_m *RequesterGenerics[TAny, TComparable, TSigned, TIntf, TExternalIntf, TG ret := _m.Called(_a0) if len(ret) == 0 { - panic("Missing Return() function for GenericStructs()") + panic("no return value specified for GenericStructs") } var r0 test.GenericType[TSigned, TIntf] diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go index 97c7edc2..78c73113 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterIface.go @@ -26,7 +26,7 @@ func (_m *RequesterIface) Get() io.Reader { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 io.Reader diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go index 14b80948..3b05d94a 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterNS.go @@ -26,7 +26,7 @@ func (_m *RequesterNS) Get(path string) (http.Response, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 http.Response diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go index 208b89d1..d3c60f75 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterPtr.go @@ -22,7 +22,7 @@ func (_m *RequesterPtr) Get(path string) (*string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 *string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go index c311e288..7d7c7b30 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterReturnElided.go @@ -22,7 +22,7 @@ func (_m *RequesterReturnElided) Get(path string) (int, int, int, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 int @@ -92,7 +92,7 @@ func (_m *RequesterReturnElided) Put(path string) (int, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Put()") + panic("no return value specified for Put") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go index 86a0436d..afaa1169 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterSlice.go @@ -22,7 +22,7 @@ func (_m *RequesterSlice) Get(path string) ([]string, error) { ret := _m.Called(path) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 []string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go index 13ae0385..dd863d66 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadic.go @@ -24,7 +24,7 @@ func (_m *RequesterVariadic) Get(values ...string) bool { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 bool @@ -49,7 +49,7 @@ func (_m *RequesterVariadic) MultiWriteToFile(filename string, w ...io.Writer) s ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for MultiWriteToFile()") + panic("no return value specified for MultiWriteToFile") } var r0 string @@ -69,7 +69,7 @@ func (_m *RequesterVariadic) OneInterface(a ...interface{}) bool { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for OneInterface()") + panic("no return value specified for OneInterface") } var r0 bool @@ -90,7 +90,7 @@ func (_m *RequesterVariadic) Sprintf(format string, a ...interface{}) string { ret := _m.Called(_ca...) if len(ret) == 0 { - panic("Missing Return() function for Sprintf()") + panic("no return value specified for Sprintf") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go index d6bbd4a0..da4db6ee 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/RequesterVariadicOneArgument.go @@ -18,7 +18,7 @@ func (_m *RequesterVariadicOneArgument) Get(values ...string) bool { ret := _m.Called(values) if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 bool @@ -36,7 +36,7 @@ func (_m *RequesterVariadicOneArgument) MultiWriteToFile(filename string, w ...i ret := _m.Called(filename, w) if len(ret) == 0 { - panic("Missing Return() function for MultiWriteToFile()") + panic("no return value specified for MultiWriteToFile") } var r0 string @@ -54,7 +54,7 @@ func (_m *RequesterVariadicOneArgument) OneInterface(a ...interface{}) bool { ret := _m.Called(a) if len(ret) == 0 { - panic("Missing Return() function for OneInterface()") + panic("no return value specified for OneInterface") } var r0 bool @@ -72,7 +72,7 @@ func (_m *RequesterVariadicOneArgument) Sprintf(format string, a ...interface{}) ret := _m.Called(format, a) if len(ret) == 0 { - panic("Missing Return() function for Sprintf()") + panic("no return value specified for Sprintf") } var r0 string diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go index 283f7c9f..740fe462 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/SendFunc.go @@ -26,7 +26,7 @@ func (_m *SendFunc) Execute(ctx context.Context, data string) (int, error) { ret := _m.Called(ctx, data) if len(ret) == 0 { - panic("Missing Return() function for Execute()") + panic("no return value specified for Execute") } var r0 int diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go index db572ea1..2686a2c4 100644 --- a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/StructWithTag.go @@ -28,7 +28,7 @@ func (_m *StructWithTag) MethodA(v *struct { ret := _m.Called(v) if len(ret) == 0 { - panic("Missing Return() function for MethodA()") + panic("no return value specified for MethodA") } var r0 *struct { diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go index f0daa351..0a383805 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceA_test.go @@ -22,7 +22,7 @@ func (_m *interfaceAMock) DoB(interfaceB0 interfaceB) interfaceB { ret := _m.Called(interfaceB0) if len(ret) == 0 { - panic("Missing Return() function for DoB()") + panic("no return value specified for DoB") } var r0 interfaceB @@ -70,7 +70,7 @@ func (_m *interfaceAMock) DoB0(interfaceB interfaceB0) interfaceB0 { ret := _m.Called(interfaceB) if len(ret) == 0 { - panic("Missing Return() function for DoB0()") + panic("no return value specified for DoB0") } var r0 interfaceB0 @@ -118,7 +118,7 @@ func (_m *interfaceAMock) DoB0v2(interfaceB00 interfaceB0) interfaceB0 { ret := _m.Called(interfaceB00) if len(ret) == 0 { - panic("Missing Return() function for DoB0v2()") + panic("no return value specified for DoB0v2") } var r0 interfaceB0 diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go index 22718121..d5ac136c 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB0_test.go @@ -22,7 +22,7 @@ func (_m *interfaceB0Mock) DoB0(interfaceB00 interfaceB0) interfaceB0 { ret := _m.Called(interfaceB00) if len(ret) == 0 { - panic("Missing Return() function for DoB0()") + panic("no return value specified for DoB0") } var r0 interfaceB0 diff --git a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go index 7bfad951..f0538bd7 100644 --- a/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go +++ b/pkg/fixtures/method_args/same_name_arg_and_type/mock_interfaceB_test.go @@ -22,7 +22,7 @@ func (_m *interfaceBMock) GetData() int { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for GetData()") + panic("no return value specified for GetData") } var r0 int diff --git a/pkg/fixtures/recursive_generation/Foo_mock.go b/pkg/fixtures/recursive_generation/Foo_mock.go index d24cd151..1374b9fa 100644 --- a/pkg/fixtures/recursive_generation/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 string diff --git a/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go index 99df0b9f..50697c1d 100644 --- a/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg1/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 string diff --git a/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go index 898ddbf6..fe92d118 100644 --- a/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg2/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 string diff --git a/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go b/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go index 3e4a2879..9648bcfd 100644 --- a/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go +++ b/pkg/fixtures/recursive_generation/subpkg_with_only_autogenerated_files/Foo_mock.go @@ -22,7 +22,7 @@ func (_m *MockFoo) Get() string { ret := _m.Called() if len(ret) == 0 { - panic("Missing Return() function for Get()") + panic("no return value specified for Get") } var r0 string diff --git a/pkg/fixtures/test/panic_on_no_return_value_test.go b/pkg/fixtures/test/panic_on_no_return_value_test.go index bbc4638d..3257d0ac 100644 --- a/pkg/fixtures/test/panic_on_no_return_value_test.go +++ b/pkg/fixtures/test/panic_on_no_return_value_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" mocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg/fixtures" ) @@ -11,10 +12,15 @@ func TestPanicOnNoReturnValue(t *testing.T) { m := mocks.NewPanicOnNoReturnValue(t) m.EXPECT().DoSomething() + var panicOccurred bool defer func() { + assert.True(t, panicOccurred) + }() + defer func() { + panicOccurred = true if r := recover(); r != nil { - assert.NotNil(t, r) - assert.Equal(t, "Missing Return() function for DoSomething()", r.(string)) + require.NotNil(t, r) + assert.Equal(t, "no return value specified for DoSomething", r.(string)) } }() diff --git a/pkg/generator.go b/pkg/generator.go index b13e78ed..5cfacc73 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -753,7 +753,7 @@ func (_m *{{.MockName}}{{.InstantiatedTypeString}}) {{.FunctionName}}({{join .Pa {{- .RetVariableName}} := {{.Called}} if len({{.RetVariableName}}) == 0 { - panic("Missing Return() function for {{.FunctionName}}()") + panic("no return value specified for {{.FunctionName}}") } {{range $idx, $name := .Returns.ReturnNames}} From d0fa1f99e2a25663a6ecf427eb6860992ff2479c Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 20 Nov 2023 20:46:13 -0300 Subject: [PATCH 21/30] improvements after code review #729 --- ...c_on_no_return_value_test.go => panic_err_test.go} | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) rename pkg/fixtures/{test/panic_on_no_return_value_test.go => panic_err_test.go} (74%) diff --git a/pkg/fixtures/test/panic_on_no_return_value_test.go b/pkg/fixtures/panic_err_test.go similarity index 74% rename from pkg/fixtures/test/panic_on_no_return_value_test.go rename to pkg/fixtures/panic_err_test.go index 3257d0ac..89c0bbd7 100644 --- a/pkg/fixtures/test/panic_on_no_return_value_test.go +++ b/pkg/fixtures/panic_err_test.go @@ -1,10 +1,11 @@ -package test +package test_test import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + mocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg/fixtures" ) @@ -18,10 +19,10 @@ func TestPanicOnNoReturnValue(t *testing.T) { }() defer func() { panicOccurred = true - if r := recover(); r != nil { - require.NotNil(t, r) - assert.Equal(t, "no return value specified for DoSomething", r.(string)) - } + + r := recover() + require.NotNil(t, r) + assert.Equal(t, "no return value specified for DoSomething", r.(string)) }() m.DoSomething() From b2484925496a4146c484637882120b9aa70ca90d Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Tue, 19 Dec 2023 13:43:10 -0600 Subject: [PATCH 22/30] Don't recurse into submodules on `recursive: true` This PR changes mockery to not recurse into paths that contain a `go.mod` submodule. This is to help support monorepos that contain many modules. In this case, mockery should not be recursing into these because it causes issues, like in issue #706. fix deprecations in mkdocs remove explicit lint step --- .github/workflows/golangci-lint.yml | 27 ------------------- .github/workflows/testing.yml | 2 +- go.mod | 6 +++-- go.sum | 5 ++-- go.work | 5 +++- mkdocs.yml | 4 +-- pkg/config/config.go | 27 ++++++++++++++----- pkg/config/config_test.go | 21 +++++++++++++++ .../pkg_with_submodules/go.mod | 3 +++ .../pkg_with_submodules/string.go | 5 ++++ .../pkg_with_submodules/submodule/go.mod | 3 +++ .../pkg_with_submodules/submodule/string.go | 5 ++++ .../pkg_with_submodules/subpkg/string.go | 5 ++++ .../subpkg/submodule/go.mod | 3 +++ .../subpkg/submodule/string.go | 5 ++++ 15 files changed, 85 insertions(+), 41 deletions(-) delete mode 100644 .github/workflows/golangci-lint.yml create mode 100644 pkg/fixtures/example_project/pkg_with_submodules/go.mod create mode 100644 pkg/fixtures/example_project/pkg_with_submodules/string.go create mode 100644 pkg/fixtures/example_project/pkg_with_submodules/submodule/go.mod create mode 100644 pkg/fixtures/example_project/pkg_with_submodules/submodule/string.go create mode 100644 pkg/fixtures/example_project/pkg_with_submodules/subpkg/string.go create mode 100644 pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/go.mod create mode 100644 pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/string.go diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml deleted file mode 100644 index aaf774d9..00000000 --- a/.github/workflows/golangci-lint.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: golangci-lint -on: - push: - tags: - - v* - branches: - - master - - main - pull_request: -permissions: - contents: read - # Optional: allow read access to pull request. Use with `only-new-issues` option. - # pull-requests: read -jobs: - golangci: - name: lint - runs-on: ubuntu-latest - steps: - - uses: actions/setup-go@v4 - with: - go-version: '1.19' - cache: false - - uses: actions/checkout@v3 - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: v1.52.2 \ No newline at end of file diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 8941560a..0be67713 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: os: [ macos-latest, ubuntu-latest] - go_vers: ['1.20'] + go_vers: ['1.21'] steps: - uses: actions/checkout@v2 with: diff --git a/go.mod b/go.mod index 0a52d015..dfff32d1 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,11 @@ module github.com/vektra/mockery/v2 -go 1.19 +go 1.21 + +toolchain go1.21.0 require ( - github.com/chigopher/pathlib v0.15.0 + github.com/chigopher/pathlib v0.19.1 github.com/davecgh/go-spew v1.1.1 github.com/huandu/xstrings v1.4.0 github.com/iancoleman/strcase v0.2.0 diff --git a/go.sum b/go.sum index e6962530..83f27ae0 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/chigopher/pathlib v0.15.0 h1:1pg96WL3iC1/YyWV4UJSl3E0GBf4B+h5amBtsbAAieY= -github.com/chigopher/pathlib v0.15.0/go.mod h1:3+YPPV21mU9vyw8Mjp+F33CyCfE6iOzinpiqBcccv7I= +github.com/chigopher/pathlib v0.19.1 h1:RoLlUJc0CqBGwq239cilyhxPNLXTK+HXoASGyGznx5A= +github.com/chigopher/pathlib v0.19.1/go.mod h1:tzC1dZLW8o33UQpWkNkhvPwL5n4yyFRFm/jL1YGWFvY= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -310,6 +310,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/go.work b/go.work index 74637646..59b494ce 100644 --- a/go.work +++ b/go.work @@ -1,6 +1,9 @@ -go 1.19 +go 1.21 + +toolchain go1.21.0 use ( . + ./pkg/fixtures/example_project/pkg_with_submodules ./tools ) diff --git a/mkdocs.yml b/mkdocs.yml index b614a716..c63323e4 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -40,8 +40,8 @@ markdown_extensions: - attr_list - md_in_html - pymdownx.emoji: - emoji_index: !!python/name:materialx.emoji.twemoji - emoji_generator: !!python/name:materialx.emoji.to_svg + emoji_index: !!python/name:material.extensions.emoji.twemoji + emoji_generator: !!python/name:material.extensions.emoji.to_svg - pymdownx.details - pymdownx.highlight: anchor_linenums: true diff --git a/pkg/config/config.go b/pkg/config/config.go index c709f4ba..2152d662 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -558,7 +558,7 @@ func (c *Config) subPackages( walker, err := pathlib.NewWalk( searchRoot, - pathlib.WalkAlgorithm(pathlib.AlgorithmBasic), + pathlib.WalkAlgorithm(pathlib.AlgorithmPreOrderDepthFirst), pathlib.WalkFollowSymlinks(false), pathlib.WalkVisitDirs(false), pathlib.WalkVisitFiles(true), @@ -575,12 +575,28 @@ func (c *Config) subPackages( visitedDirs[searchRoot.String()] = nil // Walk the filesystem path, starting at the root of the package we've - // been given. Note that this will always work because Golang downloads + // been given. Note that this will always work because Go downloads // the package when we call `packages.Load` walkErr := walker.Walk(func(path *pathlib.Path, info os.FileInfo, err error) error { + pathLog := log.With().Stringer("path", path).Logger() if err != nil { return err } + if path.Name() == "go.mod" { + pathLog.Debug().Msg("path contains go.mod file") + // Check if our current depth is 0. We do this to skip sub-modules, but not + // the root module. + relative, err := path.RelativeTo(searchRoot) + if err != nil { + return stackerr.NewStackErrf(err, "determining distance from search root") + } + + if len(relative.Parts()) != 1 { + pathLog.Debug().Msg("skipping sub-module") + return pathlib.ErrWalkSkipSubtree + } + pathLog.Debug().Int("parts_len", len(relative.Parts())).Str("parts", fmt.Sprintf("%v", relative.Parts())).Msg("not skipping module as this is the root path") + } _, haveVisitedDir := visitedDirs[path.Parent().String()] if !haveVisitedDir && strings.HasSuffix(path.Name(), ".go") { @@ -588,17 +604,16 @@ func (c *Config) subPackages( if !c.IncludeAutoGenerated { autoGenerated, err := isAutoGenerated(path) if err != nil { - log.Err(err).Stringer("path", path).Msg("failed to determine if file is auto-generated") + pathLog.Err(err).Msg("failed to determine if file is auto-generated") return err } if autoGenerated { - log.Debug().Stringer("path", path).Msg("skipping file as auto-generated") + pathLog.Debug().Msg("skipping file as auto-generated") return nil } } - l := log.With().Stringer("path", path.Parent()).Logger() - l.Debug().Msg("subdirectory has a .go file, adding this path to packages config") + pathLog.Debug().Msg("subdirectory has a .go file, adding this path to packages config") subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent()) visitedDirs[path.Parent().String()] = nil } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index ce5cad6b..94ff3fa7 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1231,6 +1231,27 @@ packages: recursive: true with-expecter: true with-expecter: false +`, + }, + { + name: "package with submodule that should be excluded", + cfgYaml: ` +all: true +packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules: + config: + recursive: True +`, + wantCfgMap: `all: true +packages: + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules: + config: + all: true + recursive: true + github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules/subpkg: + config: + all: true + recursive: true `, }, } diff --git a/pkg/fixtures/example_project/pkg_with_submodules/go.mod b/pkg/fixtures/example_project/pkg_with_submodules/go.mod new file mode 100644 index 00000000..f4e7eb87 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submodules/go.mod @@ -0,0 +1,3 @@ +module github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules + +go 1.19 diff --git a/pkg/fixtures/example_project/pkg_with_submodules/string.go b/pkg/fixtures/example_project/pkg_with_submodules/string.go new file mode 100644 index 00000000..a8be0749 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submodules/string.go @@ -0,0 +1,5 @@ +package pkg_with_submodules + +type Stringer interface { + String() string +} diff --git a/pkg/fixtures/example_project/pkg_with_submodules/submodule/go.mod b/pkg/fixtures/example_project/pkg_with_submodules/submodule/go.mod new file mode 100644 index 00000000..6f511b21 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submodules/submodule/go.mod @@ -0,0 +1,3 @@ +module github.com/vektra/mockery/v2/pkg/fixtures/example_project/submodule + +go 1.21.0 diff --git a/pkg/fixtures/example_project/pkg_with_submodules/submodule/string.go b/pkg/fixtures/example_project/pkg_with_submodules/submodule/string.go new file mode 100644 index 00000000..97664f8e --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submodules/submodule/string.go @@ -0,0 +1,5 @@ +package submodule + +type Stringer interface { + String() string +} diff --git a/pkg/fixtures/example_project/pkg_with_submodules/subpkg/string.go b/pkg/fixtures/example_project/pkg_with_submodules/subpkg/string.go new file mode 100644 index 00000000..e9fcf3e0 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submodules/subpkg/string.go @@ -0,0 +1,5 @@ +package subpkg + +type Stringer interface { + String() string +} diff --git a/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/go.mod b/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/go.mod new file mode 100644 index 00000000..9d879cdb --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/go.mod @@ -0,0 +1,3 @@ +module github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule + +go 1.19 diff --git a/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/string.go b/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/string.go new file mode 100644 index 00000000..97664f8e --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule/string.go @@ -0,0 +1,5 @@ +package submodule + +type Stringer interface { + String() string +} From a199cfb18ebb9d9dde1e597df066d5936d9e64f4 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Wed, 20 Dec 2023 10:52:29 -0600 Subject: [PATCH 23/30] Add clarification on internal error --- docs/notes.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/notes.md b/docs/notes.md index 4cab1783..fe6e7c87 100644 --- a/docs/notes.md +++ b/docs/notes.md @@ -15,9 +15,13 @@ internal error: package without types was imported [https://github.com/vektra/mockery/issues/475](https://github.com/vektra/mockery/issues/475) -This issue indicates an incompatibility that exists with one of your cached Golang packages. The solution is to run `go clean -modcache`. +This issue indicates that you have attempted to use package in your dependency tree (whether direct or indirect) that uses Go language semantics that your currently-running Go version does not support. The solution: -This issue also happens when compiling from source, such as with `go install`. You would not encounter this issue if using one of the installation methods that install pre-built binaries, like downloading the `.tar.gz` binaries, or through `brew install`. +1. Update to the latest go version +2. Delete all cached packages with `go clean -modcache` +3. Reinstall mockery + +Additionally, this issue only happens when compiling mockery from source, such as with `go install`. Our docs [recommend not to use `go install`](installation/#go-install) as the success of your build depends on the compatibility of your Go version with the semantics in use. You would not encounter this issue if using one of the installation methods that install pre-built binaries, like downloading the `.tar.gz` binaries, or through `brew install`. Multiple Expectations With Identical Arguments ----------------------------------------------- From 5c62fda964b2aed97fb7e5ddc35634aa72ae96eb Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Wed, 20 Dec 2023 11:41:57 -0600 Subject: [PATCH 24/30] Add MongoDB as user of mockery --- docs/assets/images/logos/mongodb.svg | 11 +++++++++++ docs/index.md | 3 +++ 2 files changed, 14 insertions(+) create mode 100644 docs/assets/images/logos/mongodb.svg diff --git a/docs/assets/images/logos/mongodb.svg b/docs/assets/images/logos/mongodb.svg new file mode 100644 index 00000000..57321296 --- /dev/null +++ b/docs/assets/images/logos/mongodb.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/docs/index.md b/docs/index.md index 0b01cd26..bc30c14d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -124,6 +124,9 @@ Who uses mockery? [![Amazon logo](assets/images/logos/amazon.svg){ class="center" width="300" }](https://github.com/eksctl-io/eksctl)
[eksctl](https://github.com/eksctl-io/eksctl)
+-
+ [![MongoDB Logo](assets/images/logos/mongodb.svg){ class="center" width="300" }](https://github.com/search?q=org%3Amongodb%20mockery&type=code) +
From f5f680847aa61c5d7abdf43c6096763679625b3c Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Tue, 9 Jan 2024 16:14:25 -0600 Subject: [PATCH 25/30] add google analytics env to build step --- .github/workflows/documentation.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index d7bf3a67..494a6986 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -21,6 +21,7 @@ jobs: - run: pip install -r docs/requirements.txt env: GH_TOKEN: ${{ secrets.GH_TOKEN }} + GOOGLE_ANALYTICS_KEY: ${{ secrets.GOOGLE_ANALYTICS_KEY }} - name: Setup doc deploy run: | git config --global user.name vektra-bot From 05ac09bfade096fb9fe1a4e637488a8b4d4798ba Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Tue, 9 Jan 2024 16:20:19 -0600 Subject: [PATCH 26/30] fix GOOGLE_ANALYTICS_KEY --- .github/workflows/documentation.yml | 3 ++- mkdocs.yml | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index 494a6986..a517fd58 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -21,7 +21,6 @@ jobs: - run: pip install -r docs/requirements.txt env: GH_TOKEN: ${{ secrets.GH_TOKEN }} - GOOGLE_ANALYTICS_KEY: ${{ secrets.GOOGLE_ANALYTICS_KEY }} - name: Setup doc deploy run: | git config --global user.name vektra-bot @@ -29,3 +28,5 @@ jobs: git fetch origin gh-pages --depth=1 - name: Deploy docs run: "mike deploy --push --update-aliases $(echo ${{ github.ref_name }} | cut -d'.' -f1-2 ) latest" + env: + GOOGLE_ANALYTICS_KEY: ${{ secrets.GOOGLE_ANALYTICS_KEY }} diff --git a/mkdocs.yml b/mkdocs.yml index c63323e4..97434bed 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -78,6 +78,9 @@ extra_javascript: extra: version: provider: mike + analytics: + provider: google + property: !ENV GOOGLE_ANALYTICS_KEY plugins: - mike: From 6630b9eade2817c5497b640ced4843dff149c3bd Mon Sep 17 00:00:00 2001 From: Landon Clipp Date: Tue, 9 Jan 2024 16:34:06 -0600 Subject: [PATCH 27/30] Update mkdocs.yml --- mkdocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index 97434bed..b7a3d177 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -80,7 +80,7 @@ extra: provider: mike analytics: provider: google - property: !ENV GOOGLE_ANALYTICS_KEY + property: G-RH6WD53JET plugins: - mike: From 138d507e2b506420f49969570b816f31287b8bed Mon Sep 17 00:00:00 2001 From: Landon Clipp Date: Tue, 9 Jan 2024 16:37:25 -0600 Subject: [PATCH 28/30] Update mkdocs.yml --- mkdocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index b7a3d177..2c59e576 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -80,7 +80,7 @@ extra: provider: mike analytics: provider: google - property: G-RH6WD53JET + property: G-0ZGMQGZGRN plugins: - mike: From 1c18f4456d8a9638702a7aff669d88c2143498c1 Mon Sep 17 00:00:00 2001 From: Vitaliy Solodilov Date: Wed, 16 Aug 2023 13:44:38 +0300 Subject: [PATCH 29/30] Generate mock build constraints Supported only the new format of build tags https://pkg.go.dev/cmd/go#hdr-Build_constraints Fixes #691 --- .mockery.yaml | 7 + cmd/mockery.go | 4 +- docs/configuration.md | 3 +- .../IfaceWithCustomBuildTagInComment.go | 123 ++++++++++++++++++ pkg/config/config.go | 1 + .../buildtag/comment/custom2_iface.go | 1 + pkg/generator.go | 8 ++ pkg/generator_test.go | 11 ++ pkg/outputter.go | 1 + pkg/walker.go | 2 + 10 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 mocks/github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment/IfaceWithCustomBuildTagInComment.go diff --git a/.mockery.yaml b/.mockery.yaml index 25757954..2ae36ff0 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -4,7 +4,14 @@ with-expecter: True mockname: "{{.InterfaceName}}" filename: "{{.MockName}}.go" outpkg: mocks +tags: "custom2" packages: + github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment: + config: + mock-build-tags: "custom3 && (!windows || !darwin || !freebsd)" + disable-version-string: true + interfaces: + IfaceWithCustomBuildTagInComment: github.com/vektra/mockery/v2/pkg: interfaces: TypesPackage: diff --git a/cmd/mockery.go b/cmd/mockery.go index 922a16a8..da748314 100644 --- a/cmd/mockery.go +++ b/cmd/mockery.go @@ -70,7 +70,8 @@ func NewRootCmd() *cobra.Command { pFlags.Bool("version", false, "prints the installed version of mockery") pFlags.Bool("quiet", false, `suppresses logger output (equivalent to --log-level="")`) pFlags.Bool("keeptree", false, "keep the tree structure of the original interface files into a different repository. Must be used with XX") - pFlags.String("tags", "", "space-separated list of additional build tags to use") + pFlags.String("tags", "", "space-separated list of additional build tags to load packages") + pFlags.String("mock-build-tags", "", "set the build tags of the generated mocks. Read more about the format: https://pkg.go.dev/cmd/go#hdr-Build_constraints") pFlags.String("filename", "", "name of generated file (only works with -name and no regex)") pFlags.String("structname", "", "name of generated struct (only works with -name and no regex)") pFlags.String("log-level", "info", "Level of logging") @@ -376,6 +377,7 @@ func (r *RootApp) Run() error { InPackage: r.Config.InPackage, KeepTree: r.Config.KeepTree, Note: r.Config.Note, + MockBuildTags: r.Config.MockBuildTags, PackageName: r.Config.Outpkg, PackageNamePrefix: r.Config.Packageprefix, StructName: r.Config.StructName, diff --git a/docs/configuration.md b/docs/configuration.md index 473b9b02..8a54f859 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -78,7 +78,8 @@ Parameter Descriptions | [`packages`](features.md#packages-configuration) | :fontawesome-solid-x: | `#!yaml null` | A dictionary containing configuration describing the packages and interfaces to generate mocks for. | | `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. | -| `tags` | :fontawesome-solid-x: | `#!yaml ""` | Set the build tags of the generated mocks. | +| `tags` | :fontawesome-solid-x: | `#!yaml ""` | A space-separated list of additional build tags to load packages. | +| `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). | | [`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 setup your mocks. | | [`replace-type`](features.md#replace-types) | :fontawesome-solid-x: | `#!yaml null` | Replaces aliases, packages and/or types during generation. | diff --git a/mocks/github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment/IfaceWithCustomBuildTagInComment.go b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment/IfaceWithCustomBuildTagInComment.go new file mode 100644 index 00000000..73bc3023 --- /dev/null +++ b/mocks/github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment/IfaceWithCustomBuildTagInComment.go @@ -0,0 +1,123 @@ +// Code generated by mockery. DO NOT EDIT. + +//go:build custom3 && (!windows || !darwin || !freebsd) + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// IfaceWithCustomBuildTagInComment is an autogenerated mock type for the IfaceWithCustomBuildTagInComment type +type IfaceWithCustomBuildTagInComment struct { + mock.Mock +} + +type IfaceWithCustomBuildTagInComment_Expecter struct { + mock *mock.Mock +} + +func (_m *IfaceWithCustomBuildTagInComment) EXPECT() *IfaceWithCustomBuildTagInComment_Expecter { + return &IfaceWithCustomBuildTagInComment_Expecter{mock: &_m.Mock} +} + +// Custom2 provides a mock function with given fields: +func (_m *IfaceWithCustomBuildTagInComment) Custom2() { + _m.Called() +} + +// IfaceWithCustomBuildTagInComment_Custom2_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Custom2' +type IfaceWithCustomBuildTagInComment_Custom2_Call struct { + *mock.Call +} + +// Custom2 is a helper method to define mock.On call +func (_e *IfaceWithCustomBuildTagInComment_Expecter) Custom2() *IfaceWithCustomBuildTagInComment_Custom2_Call { + return &IfaceWithCustomBuildTagInComment_Custom2_Call{Call: _e.mock.On("Custom2")} +} + +func (_c *IfaceWithCustomBuildTagInComment_Custom2_Call) Run(run func()) *IfaceWithCustomBuildTagInComment_Custom2_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *IfaceWithCustomBuildTagInComment_Custom2_Call) Return() *IfaceWithCustomBuildTagInComment_Custom2_Call { + _c.Call.Return() + return _c +} + +func (_c *IfaceWithCustomBuildTagInComment_Custom2_Call) RunAndReturn(run func()) *IfaceWithCustomBuildTagInComment_Custom2_Call { + _c.Call.Return(run) + return _c +} + +// Sprintf provides a mock function with given fields: format, a +func (_m *IfaceWithCustomBuildTagInComment) Sprintf(format string, a ...interface{}) string { + var _ca []interface{} + _ca = append(_ca, format) + _ca = append(_ca, a...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for Sprintf") + } + + var r0 string + if rf, ok := ret.Get(0).(func(string, ...interface{}) string); ok { + r0 = rf(format, a...) + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// IfaceWithCustomBuildTagInComment_Sprintf_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Sprintf' +type IfaceWithCustomBuildTagInComment_Sprintf_Call struct { + *mock.Call +} + +// Sprintf is a helper method to define mock.On call +// - format string +// - a ...interface{} +func (_e *IfaceWithCustomBuildTagInComment_Expecter) Sprintf(format interface{}, a ...interface{}) *IfaceWithCustomBuildTagInComment_Sprintf_Call { + return &IfaceWithCustomBuildTagInComment_Sprintf_Call{Call: _e.mock.On("Sprintf", + append([]interface{}{format}, a...)...)} +} + +func (_c *IfaceWithCustomBuildTagInComment_Sprintf_Call) Run(run func(format string, a ...interface{})) *IfaceWithCustomBuildTagInComment_Sprintf_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]interface{}, len(args)-1) + for i, a := range args[1:] { + if a != nil { + variadicArgs[i] = a.(interface{}) + } + } + run(args[0].(string), variadicArgs...) + }) + return _c +} + +func (_c *IfaceWithCustomBuildTagInComment_Sprintf_Call) Return(_a0 string) *IfaceWithCustomBuildTagInComment_Sprintf_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *IfaceWithCustomBuildTagInComment_Sprintf_Call) RunAndReturn(run func(string, ...interface{}) string) *IfaceWithCustomBuildTagInComment_Sprintf_Call { + _c.Call.Return(run) + return _c +} + +// NewIfaceWithCustomBuildTagInComment creates a new instance of IfaceWithCustomBuildTagInComment. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewIfaceWithCustomBuildTagInComment(t interface { + mock.TestingT + Cleanup(func()) +}) *IfaceWithCustomBuildTagInComment { + mock := &IfaceWithCustomBuildTagInComment{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 2152d662..13d457f5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -33,6 +33,7 @@ type Interface struct { type Config struct { All bool `mapstructure:"all"` + MockBuildTags string `mapstructure:"mock-build-tags"` BuildTags string `mapstructure:"tags"` Case string `mapstructure:"case"` Config string `mapstructure:"config"` diff --git a/pkg/fixtures/buildtag/comment/custom2_iface.go b/pkg/fixtures/buildtag/comment/custom2_iface.go index 08bd30d6..02686a26 100644 --- a/pkg/fixtures/buildtag/comment/custom2_iface.go +++ b/pkg/fixtures/buildtag/comment/custom2_iface.go @@ -5,4 +5,5 @@ package comment type IfaceWithCustomBuildTagInComment interface { Sprintf(format string, a ...interface{}) string + Custom2() } diff --git a/pkg/generator.go b/pkg/generator.go index 5cfacc73..6a9d9034 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -57,6 +57,7 @@ type GeneratorConfig struct { InPackage bool KeepTree bool Note string + MockBuildTags string PackageName string PackageNamePrefix string StructName string @@ -111,6 +112,7 @@ func NewGenerator(ctx context.Context, c GeneratorConfig, iface *Interface, pkg func (g *Generator) GenerateAll(ctx context.Context) error { g.GenerateBoilerplate(g.config.Boilerplate) g.GeneratePrologueNote(g.config.Note) + g.GenerateBuildTags(g.config.MockBuildTags) g.GeneratePrologue(ctx, g.pkg) return g.Generate(ctx) } @@ -418,6 +420,12 @@ func (g *Generator) GenerateBoilerplate(boilerplate string) { } } +func (g *Generator) GenerateBuildTags(buildTags string) { + if buildTags != "" { + g.printf("//go:build %s\n\n", buildTags) + } +} + // ErrNotInterface is returned when the given type is not an interface // type. var ErrNotInterface = errors.New("expression not an interface") diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 5ea54b5b..283337fb 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -296,6 +296,17 @@ func (s *GeneratorSuite) TestGeneratorBoilerplate() { s.Equal(expected, generator.buf.String()) } +func (s *GeneratorSuite) TestGeneratorBuildTags() { + generator := s.getGenerator(testFile, "Requester", false, "") + generator.GenerateBuildTags("custom && (!windows || !linux)") + + expected := `//go:build custom && (!windows || !linux) + +` + + s.Equal(expected, generator.buf.String()) +} + func (s *GeneratorSuite) TestGeneratorPrologueNoteNoVersionString() { generator := s.getGenerator(testFile, "Requester", false, "") generator.config.DisableVersionString = true diff --git a/pkg/outputter.go b/pkg/outputter.go index 2f1a8192..1960be9b 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -336,6 +336,7 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error { InPackage: interfaceConfig.InPackage, KeepTree: interfaceConfig.KeepTree, Note: interfaceConfig.Note, + MockBuildTags: interfaceConfig.MockBuildTags, PackageName: interfaceConfig.Outpkg, PackageNamePrefix: interfaceConfig.Packageprefix, StructName: interfaceConfig.MockName, diff --git a/pkg/walker.go b/pkg/walker.go index 61d33c7f..34bab4e0 100644 --- a/pkg/walker.go +++ b/pkg/walker.go @@ -120,6 +120,7 @@ type GeneratorVisitorConfig struct { InPackage bool KeepTree bool Note string + MockBuildTags string // The name of the output package, if InPackage is false (defaults to "mocks") PackageName string PackageNamePrefix string @@ -181,6 +182,7 @@ func (v *GeneratorVisitor) VisitWalk(ctx context.Context, iface *Interface) erro InPackage: v.config.InPackage, KeepTree: v.config.KeepTree, Note: v.config.Note, + MockBuildTags: v.config.MockBuildTags, PackageName: v.config.PackageName, PackageNamePrefix: v.config.PackageNamePrefix, StructName: v.config.StructName, From 58b0c4ce7fe5389801ddf98d449e821b96d89031 Mon Sep 17 00:00:00 2001 From: Vitaliy Solodilov Date: Thu, 11 Jan 2024 01:20:43 +0300 Subject: [PATCH 30/30] code review fixes --- docs/configuration.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 8a54f859..c3bfcd6b 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -72,16 +72,16 @@ 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. | +| `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. | | `outpkg` | :fontawesome-solid-check: | `#!yaml "{{.PackageName}}"` | Use `outpkg` to specify the package name of the generated mocks. | -| `log-level` | :fontawesome-solid-x: | `#!yaml "info"` | Set the level of the logger | | [`packages`](features.md#packages-configuration) | :fontawesome-solid-x: | `#!yaml null` | A dictionary containing configuration describing the packages and interfaces to generate mocks for. | | `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. | | `tags` | :fontawesome-solid-x: | `#!yaml ""` | A space-separated list of additional build tags to load packages. | -| `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). | | [`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 setup your mocks. | -| [`replace-type`](features.md#replace-types) | :fontawesome-solid-x: | `#!yaml null` | Replaces aliases, packages and/or types during generation. | Layouts -------