Skip to content

Commit

Permalink
Merge pull request #317 from larryzhao/package-name-prefix
Browse files Browse the repository at this point in the history
Enable adding prefix for the generated package name
  • Loading branch information
LandonTClipp authored Jul 17, 2020
2 parents f1b7cf8 + 900f62b commit 20e1813
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 29 deletions.
14 changes: 8 additions & 6 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func init() {
pFlags.Bool("print", false, "print the generated mock to stdout")
pFlags.String("output", "./mocks", "directory to write mocks to")
pFlags.String("outpkg", "mocks", "name of generated package")
pFlags.String("packageprefix", "", "prefix for the generated package name, it is ignored if outpkg is also specified.")
pFlags.String("dir", ".", "directory to search for interfaces")
pFlags.BoolP("recursive", "r", false, "recurse search into sub-directories")
pFlags.Bool("all", false, "generates mocks for all found interfaces in all sub-directories")
Expand Down Expand Up @@ -233,12 +234,13 @@ func (r *RootApp) Run() error {
}

visitor := &pkg.GeneratorVisitor{
Config: r.Config,
InPackage: r.Config.InPackage,
Note: r.Config.Note,
Osp: osp,
PackageName: r.Config.Outpkg,
StructName: r.Config.StructName,
Config: r.Config,
InPackage: r.Config.InPackage,
Note: r.Config.Note,
Osp: osp,
PackageName: r.Config.Outpkg,
PackageNamePrefix: r.Config.Packageprefix,
StructName: r.Config.StructName,
}

walker := pkg.Walker{
Expand Down
41 changes: 21 additions & 20 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ package config
var SemVer = "0.0.0-dev"

type Config struct {
All bool
BuildTags string
Case string
Config string
Cpuprofile string
Dir string
DryRun bool `mapstructure:"dry-run"`
FileName string
InPackage bool
KeepTree bool
LogLevel string `mapstructure:"log-level"`
Name string
Note string
Outpkg string
Output string
Print bool
Profile string
Quiet bool
Recursive bool
SrcPkg string
All bool
BuildTags string
Case string
Config string
Cpuprofile string
Dir string
DryRun bool `mapstructure:"dry-run"`
FileName string
InPackage bool
KeepTree bool
LogLevel string `mapstructure:"log-level"`
Name string
Note string
Outpkg string
Packageprefix string
Output string
Print bool
Profile string
Quiet bool
Recursive bool
SrcPkg string
// StructName overrides the name given to the mock struct and should only be nonempty
// when generating for an exact match (non regex expression in -name).
StructName string
Expand Down
1 change: 0 additions & 1 deletion pkg/mockery.go
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package pkg

8 changes: 6 additions & 2 deletions pkg/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ type GeneratorVisitor struct {
Note string
Osp OutputStreamProvider
// The name of the output package, if InPackage is false (defaults to "mocks")
PackageName string
StructName string
PackageName string
PackageNamePrefix string
StructName string
}

func (this *GeneratorVisitor) VisitWalk(ctx context.Context, iface *Interface) error {
Expand All @@ -131,6 +132,9 @@ func (this *GeneratorVisitor) VisitWalk(ctx context.Context, iface *Interface) e

if this.InPackage {
pkg = filepath.Dir(iface.FileName)
} else if (this.PackageName == "" || this.PackageName == "mocks") && this.PackageNamePrefix != "" {
// go with package name prefix only when package name is empty or default and package name prefix is specified
pkg = fmt.Sprintf("%s%s", this.PackageNamePrefix, iface.Pkg.Name())
} else {
pkg = this.PackageName
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/walker_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pkg

import (
"bytes"
"context"
"io"
"os"
"regexp"
"testing"
Expand All @@ -24,6 +26,24 @@ func NewGatheringVisitor() *GatheringVisitor {
}
}

type BufferedProvider struct {
buf *bytes.Buffer
}

func NewBufferedProvider() *BufferedProvider {
return &BufferedProvider{
buf: new(bytes.Buffer),
}
}

func (bp *BufferedProvider) String() string {
return bp.buf.String()
}

func (bp *BufferedProvider) GetWriter(context.Context, *Interface) (io.Writer, error, Cleanup) {
return bp.buf, nil, func() error { return nil }
}

func TestWalkerHere(t *testing.T) {
if testing.Short() {
t.Skip("skipping recursive walker test")
Expand Down Expand Up @@ -73,3 +93,30 @@ func TestWalkerRegexp(t *testing.T) {
assert.Equal(t, getFixturePath("async.go"), first.FileName)
assert.Equal(t, "github.com/vektra/mockery/v2/pkg/fixtures", first.QualifiedName)
}

func TestPackagePrefix(t *testing.T) {
if testing.Short() {
t.Skip("skipping recursive walker test")
}

wd, err := os.Getwd()
assert.NoError(t, err)

w := Walker{
BaseDir: wd,
Recursive: true,
LimitOne: false,
Filter: regexp.MustCompile(".*AsyncProducer*."),
}

bufferedProvider := NewBufferedProvider()
gv := &GeneratorVisitor{
InPackage: false,
Osp: bufferedProvider,
PackageName: "mocks",
PackageNamePrefix: "prefix_test_",
}

w.Walk(context.Background(), gv)
assert.Regexp(t, regexp.MustCompile("package prefix_test_test"), bufferedProvider.String())
}

0 comments on commit 20e1813

Please sign in to comment.