Skip to content

Commit 04c4dc1

Browse files
committed
Fix outpkg not being respected when inpackage: True.
Fixes #845
1 parent f6ecb44 commit 04c4dc1

13 files changed

+302
-31
lines changed

.mockery.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mockname: "{{.InterfaceName}}"
55
filename: "{{.MockName}}.go"
66
outpkg: mocks
77
tags: "custom2"
8+
issue-845-fix: True
89
packages:
910
github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment:
1011
config:
@@ -78,3 +79,18 @@ packages:
7879
outpkg: "{{.PackageName}}_test"
7980
filename: "{{.InterfaceNameSnake}}_mock_test.go"
8081
keeptree: True
82+
github.com/vektra/mockery/v2/pkg/fixtures/issue845:
83+
config:
84+
all: True
85+
dir: "{{.InterfaceDir}}"
86+
mockname: "{{.InterfaceName}}"
87+
outpkg: "{{.PackageName}}_test"
88+
filename: "mock_{{.MockName}}_test.go"
89+
inpackage: True
90+
interfaces:
91+
Interface:
92+
configs:
93+
- issue-845-fix: False
94+
mockname: WithoutFix
95+
- issue-845-fix: True
96+
mockname: WithFix

cmd/mockery.go

+1-24
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/chigopher/pathlib"
1414
"github.com/mitchellh/go-homedir"
15-
"github.com/rs/zerolog"
1615
"github.com/rs/zerolog/log"
1716
"github.com/spf13/cobra"
1817
"github.com/spf13/viper"
@@ -297,7 +296,7 @@ func (r *RootApp) Run() error {
297296
log.Fatal().Msgf("Use --name to specify the name of the interface or --all for all interfaces found")
298297
}
299298

300-
warnDeprecated(
299+
logging.WarnDeprecated(
301300
ctx,
302301
"use of the packages config will be the only way to generate mocks in v3. Please migrate your config to use the packages feature.",
303302
map[string]any{
@@ -389,25 +388,3 @@ func (r *RootApp) Run() error {
389388

390389
return nil
391390
}
392-
393-
func warn(ctx context.Context, prefix string, message string, fields map[string]any) {
394-
log := zerolog.Ctx(ctx)
395-
event := log.Warn()
396-
if fields != nil {
397-
event = event.Fields(fields)
398-
}
399-
event.Msgf("%s: %s", prefix, message)
400-
}
401-
402-
func info(ctx context.Context, prefix string, message string, fields map[string]any) {
403-
log := zerolog.Ctx(ctx)
404-
event := log.Info()
405-
if fields != nil {
406-
event = event.Fields(fields)
407-
}
408-
event.Msgf("%s: %s", prefix, message)
409-
}
410-
411-
func warnDeprecated(ctx context.Context, message string, fields map[string]any) {
412-
warn(ctx, "DEPRECATION", message, fields)
413-
}

docs/deprecations.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Deprecations
2+
=============
3+
4+
`packages`
5+
----------
6+
7+
The [`packages`](features.md#packages-configuration) feature will be the only way to configure mockery in the future.
8+
9+
`issue-845-fix`
10+
---------------
11+
12+
This parameter fixes a somewhat uninteresting, but important issue found in [#845](https://github.com/vektra/mockery/issues/845).
13+
In short, mockery ignored the `#!yaml outpkg:` parameter if `#!yaml inpackage:` was set to `#!yaml True`. This prevents users
14+
from being able to set alternate package names for their mocks that are generated in the same directory
15+
as the mocked interface. For example, it's legal Go to append `_test` to the mock package name
16+
if the file is appended with `_test.go` as well. This parameter will be permanently
17+
enabled in mockery v3.
18+
19+
As an example, if you had configuration that looked like this:
20+
21+
```yaml
22+
all: True
23+
dir: "{{.InterfaceDir}}"
24+
mockname: "{{.InterfaceName}}Mock"
25+
outpkg: "{{.PackageName}}_test"
26+
filename: "mock_{{.InterfaceName}}_test.go"
27+
inpackage: True
28+
```
29+
30+
The `#!yaml outpkg` parameter would not be respected and instead would be forced to take on the value of `#!yaml "{{.PackageName}}"`.
31+
To remove the warning, you must set:
32+
33+
```yaml
34+
issue-845-fix: True
35+
```
36+
37+
After this is done, mocks generated in the old scheme will properly respect the `#!yaml outpkg:` parameter previously set
38+
if being generated with `#!yaml inpackage: True`.

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ nav:
6767
- FAQ: notes.md
6868
- Changelog: changelog.md
6969
- Migrating to Packages: migrating_to_packages.md
70+
- Deprecations: deprecations.md
7071

7172
extra_css:
7273
- stylesheets/extra.css

pkg/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Config struct {
5050
IncludeRegex string `mapstructure:"include-regex"`
5151
InPackage bool `mapstructure:"inpackage"`
5252
InPackageSuffix bool `mapstructure:"inpackage-suffix"`
53+
Issue845Fix bool `mapstructure:"issue-845-fix"`
5354
KeepTree bool `mapstructure:"keeptree"`
5455
LogLevel string `mapstructure:"log-level"`
5556
MockName string `mapstructure:"mockname"`

pkg/fixtures/issue845/interface.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package issue845
2+
3+
type Interface interface {
4+
Foo() string
5+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package issue845
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/chigopher/pathlib"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestFix(t *testing.T) {
13+
for _, tt := range []struct {
14+
name string
15+
filepath string
16+
expectedPackage string
17+
}{
18+
{
19+
name: "with fix",
20+
filepath: "./mock_WithFix_test.go",
21+
expectedPackage: "package issue845_test",
22+
},
23+
{
24+
name: "without fix",
25+
filepath: "./mock_WithoutFix_test.go",
26+
expectedPackage: "package issue845",
27+
},
28+
} {
29+
t.Run(
30+
tt.name,
31+
func(t *testing.T) {
32+
path := pathlib.NewPath(tt.filepath)
33+
bytes, err := path.ReadFile()
34+
require.NoError(t, err)
35+
fileString := string(bytes)
36+
assert.True(t, strings.Contains(fileString, tt.expectedPackage))
37+
},
38+
)
39+
}
40+
}

pkg/fixtures/issue845/mock_WithFix_test.go

+77
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/fixtures/issue845/mock_WithoutFix_test.go

+77
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generator.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ type GeneratorConfig struct {
5555
DisableVersionString bool
5656
Exported bool
5757
InPackage bool
58+
Issue845Fix bool
5859
KeepTree bool
5960
Note string
6061
MockBuildTags string
61-
PackageName string
62+
Outpkg string
6263
PackageNamePrefix string
6364
StructName string
6465
UnrollVariadic bool
@@ -84,12 +85,14 @@ type Generator struct {
8485

8586
// NewGenerator builds a Generator.
8687
func NewGenerator(ctx context.Context, c GeneratorConfig, iface *Interface, pkg string) *Generator {
87-
if pkg == "" {
88+
if c.Issue845Fix {
89+
pkg = c.Outpkg
90+
} else if pkg == "" {
8891
pkg = DetermineOutputPackageName(
8992
iface.FileName,
9093
iface.Pkg.Name(),
9194
c.PackageNamePrefix,
92-
c.PackageName,
95+
c.Outpkg,
9396
c.KeepTree,
9497
c.InPackage,
9598
)
@@ -418,8 +421,20 @@ func (g *Generator) generateImports(ctx context.Context) {
418421
// GeneratePrologue generates the prologue of the mock.
419422
func (g *Generator) GeneratePrologue(ctx context.Context, pkg string) {
420423
g.populateImports(ctx)
421-
if g.config.InPackage {
422-
g.printf("package %s\n\n", g.iface.Pkg.Name())
424+
425+
if !g.config.Issue845Fix {
426+
logging.WarnDeprecated(
427+
ctx,
428+
"issue-845-fix must be set to True to remove this warning. Visit the link for more details.",
429+
map[string]any{
430+
"url": logging.DocsURL("/deprecations/#issue-845-fix"),
431+
},
432+
)
433+
if g.config.InPackage {
434+
g.printf("package %s\n\n", g.iface.Pkg.Name())
435+
} else {
436+
g.printf("package %v\n\n", pkg)
437+
}
423438
} else {
424439
g.printf("package %v\n\n", pkg)
425440
}

0 commit comments

Comments
 (0)