Skip to content

Commit

Permalink
Fix outpkg not being respected when inpackage: True.
Browse files Browse the repository at this point in the history
Fixes #845
  • Loading branch information
LandonTClipp committed Nov 19, 2024
1 parent f6ecb44 commit 04c4dc1
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 31 deletions.
16 changes: 16 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}.go"
outpkg: mocks
tags: "custom2"
issue-845-fix: True
packages:
github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment:
config:
Expand Down Expand Up @@ -78,3 +79,18 @@ packages:
outpkg: "{{.PackageName}}_test"
filename: "{{.InterfaceNameSnake}}_mock_test.go"
keeptree: True
github.com/vektra/mockery/v2/pkg/fixtures/issue845:
config:
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}"
outpkg: "{{.PackageName}}_test"
filename: "mock_{{.MockName}}_test.go"
inpackage: True
interfaces:
Interface:
configs:
- issue-845-fix: False
mockname: WithoutFix
- issue-845-fix: True
mockname: WithFix
25 changes: 1 addition & 24 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/chigopher/pathlib"
"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -297,7 +296,7 @@ func (r *RootApp) Run() error {
log.Fatal().Msgf("Use --name to specify the name of the interface or --all for all interfaces found")
}

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

return nil
}

func warn(ctx context.Context, prefix string, message string, fields map[string]any) {
log := zerolog.Ctx(ctx)
event := log.Warn()
if fields != nil {
event = event.Fields(fields)
}
event.Msgf("%s: %s", prefix, message)
}

func info(ctx context.Context, prefix string, message string, fields map[string]any) {
log := zerolog.Ctx(ctx)
event := log.Info()
if fields != nil {
event = event.Fields(fields)
}
event.Msgf("%s: %s", prefix, message)
}

func warnDeprecated(ctx context.Context, message string, fields map[string]any) {
warn(ctx, "DEPRECATION", message, fields)
}
38 changes: 38 additions & 0 deletions docs/deprecations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Deprecations
=============

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

The [`packages`](features.md#packages-configuration) feature will be the only way to configure mockery in the future.

`issue-845-fix`
---------------

This parameter fixes a somewhat uninteresting, but important issue found in [#845](https://github.com/vektra/mockery/issues/845).
In short, mockery ignored the `#!yaml outpkg:` parameter if `#!yaml inpackage:` was set to `#!yaml True`. This prevents users
from being able to set alternate package names for their mocks that are generated in the same directory
as the mocked interface. For example, it's legal Go to append `_test` to the mock package name
if the file is appended with `_test.go` as well. This parameter will be permanently
enabled in mockery v3.

As an example, if you had configuration that looked like this:

```yaml
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}Mock"
outpkg: "{{.PackageName}}_test"
filename: "mock_{{.InterfaceName}}_test.go"
inpackage: True
```
The `#!yaml outpkg` parameter would not be respected and instead would be forced to take on the value of `#!yaml "{{.PackageName}}"`.
To remove the warning, you must set:

```yaml
issue-845-fix: True
```

After this is done, mocks generated in the old scheme will properly respect the `#!yaml outpkg:` parameter previously set
if being generated with `#!yaml inpackage: True`.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ nav:
- FAQ: notes.md
- Changelog: changelog.md
- Migrating to Packages: migrating_to_packages.md
- Deprecations: deprecations.md

extra_css:
- stylesheets/extra.css
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
IncludeRegex string `mapstructure:"include-regex"`
InPackage bool `mapstructure:"inpackage"`
InPackageSuffix bool `mapstructure:"inpackage-suffix"`
Issue845Fix bool `mapstructure:"issue-845-fix"`
KeepTree bool `mapstructure:"keeptree"`
LogLevel string `mapstructure:"log-level"`
MockName string `mapstructure:"mockname"`
Expand Down
5 changes: 5 additions & 0 deletions pkg/fixtures/issue845/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package issue845

type Interface interface {
Foo() string
}
40 changes: 40 additions & 0 deletions pkg/fixtures/issue845/interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package issue845

import (
"strings"
"testing"

"github.com/chigopher/pathlib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFix(t *testing.T) {
for _, tt := range []struct {
name string
filepath string
expectedPackage string
}{
{
name: "with fix",
filepath: "./mock_WithFix_test.go",
expectedPackage: "package issue845_test",
},
{
name: "without fix",
filepath: "./mock_WithoutFix_test.go",
expectedPackage: "package issue845",
},
} {
t.Run(
tt.name,
func(t *testing.T) {
path := pathlib.NewPath(tt.filepath)
bytes, err := path.ReadFile()
require.NoError(t, err)
fileString := string(bytes)
assert.True(t, strings.Contains(fileString, tt.expectedPackage))
},
)
}
}
77 changes: 77 additions & 0 deletions pkg/fixtures/issue845/mock_WithFix_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions pkg/fixtures/issue845/mock_WithoutFix_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ type GeneratorConfig struct {
DisableVersionString bool
Exported bool
InPackage bool
Issue845Fix bool
KeepTree bool
Note string
MockBuildTags string
PackageName string
Outpkg string
PackageNamePrefix string
StructName string
UnrollVariadic bool
Expand All @@ -84,12 +85,14 @@ type Generator struct {

// NewGenerator builds a Generator.
func NewGenerator(ctx context.Context, c GeneratorConfig, iface *Interface, pkg string) *Generator {
if pkg == "" {
if c.Issue845Fix {
pkg = c.Outpkg
} else if pkg == "" {
pkg = DetermineOutputPackageName(
iface.FileName,
iface.Pkg.Name(),
c.PackageNamePrefix,
c.PackageName,
c.Outpkg,
c.KeepTree,
c.InPackage,
)
Expand Down Expand Up @@ -418,8 +421,20 @@ func (g *Generator) generateImports(ctx context.Context) {
// GeneratePrologue generates the prologue of the mock.
func (g *Generator) GeneratePrologue(ctx context.Context, pkg string) {
g.populateImports(ctx)
if g.config.InPackage {
g.printf("package %s\n\n", g.iface.Pkg.Name())

if !g.config.Issue845Fix {
logging.WarnDeprecated(
ctx,
"issue-845-fix must be set to True to remove this warning. Visit the link for more details.",
map[string]any{
"url": logging.DocsURL("/deprecations/#issue-845-fix"),
},
)
if g.config.InPackage {
g.printf("package %s\n\n", g.iface.Pkg.Name())
} else {
g.printf("package %v\n\n", pkg)
}
} else {
g.printf("package %v\n\n", pkg)
}
Expand Down
Loading

0 comments on commit 04c4dc1

Please sign in to comment.