Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Nov 19, 2024
1 parent 43c1c50 commit cfe5c1e
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 6 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
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
}
32 changes: 32 additions & 0 deletions pkg/fixtures/issue845/interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 {
filepath string
expectedPackage string
}{
{
filepath: "./mock_WithFix_test.go",
expectedPackage: "package issue845_test",
},
{
filepath: "./mock_WithoutFix_test.go",
expectedPackage: "package issue845",
},
} {
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.

10 changes: 6 additions & 4 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type GeneratorConfig struct {
KeepTree bool
Note string
MockBuildTags string
PackageName string
Outpkg string
PackageNamePrefix string
StructName string
UnrollVariadic bool
Expand All @@ -85,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 @@ -428,7 +430,7 @@ func (g *Generator) GeneratePrologue(ctx context.Context, pkg string) {
"url": logging.DocsURL("/deprecations/#issue-845-fix"),
},
)
if !g.config.Issue845Fix && g.config.InPackage {
if g.config.InPackage {
g.printf("package %s\n\n", g.iface.Pkg.Name())
} else {
g.printf("package %v\n\n", pkg)
Expand Down
3 changes: 2 additions & 1 deletion pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,11 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error {
DisableVersionString: interfaceConfig.DisableVersionString,
Exported: interfaceConfig.Exported,
InPackage: interfaceConfig.InPackage,
Issue845Fix: interfaceConfig.Issue845Fix,
KeepTree: interfaceConfig.KeepTree,
Note: interfaceConfig.Note,
MockBuildTags: interfaceConfig.MockBuildTags,
PackageName: interfaceConfig.Outpkg,
Outpkg: interfaceConfig.Outpkg,
PackageNamePrefix: interfaceConfig.Packageprefix,
StructName: interfaceConfig.MockName,
UnrollVariadic: interfaceConfig.UnrollVariadic,
Expand Down
2 changes: 1 addition & 1 deletion pkg/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (v *GeneratorVisitor) VisitWalk(ctx context.Context, iface *Interface) erro
KeepTree: v.config.KeepTree,
Note: v.config.Note,
MockBuildTags: v.config.MockBuildTags,
PackageName: v.config.PackageName,
Outpkg: v.config.PackageName,
PackageNamePrefix: v.config.PackageNamePrefix,
StructName: v.config.StructName,
UnrollVariadic: v.config.UnrollVariadic,
Expand Down

0 comments on commit cfe5c1e

Please sign in to comment.