Skip to content

Commit

Permalink
Adding new KeepTree flag - allows to preserve tree struct for mocks
Browse files Browse the repository at this point in the history
* Using the keeptree flag allows to generate mocks in a different
directory but keeping the original structure of the repo that is being mocked.

* Very useful for kubernetes client-go that has sometimes 3 versions of the
same interface name hence requires different folders. Easiest way is to keep the
same tree structure as if it was "inPlace".

* KeepTree flag is most useful when used with -all, -dir and -output.
  • Loading branch information
corentone committed Nov 8, 2017
1 parent b6f6c13 commit 126a261
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mockery
mockery provides the ability to easily generate mocks for golang interfaces. It removes
the boilerplate coding required to use mocks.

[![Linux Build Status](https://travis-ci.org/vektra/mockery.png)](https://travis-ci.org/vektra/mockery) [![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/vektra/mockery)](https://ci.appveyor.com/project/vektra/mockery) [![GoDoc](https://godoc.org/github.com/vektra/mockery/mockery?status.svg)](https://godoc.org/github.com/vektra/mockery/mockery) [![Go Report Card](https://goreportcard.com/badge/github.com/vektra/mockery)](https://goreportcard.com/report/github.com/vektra/mockery)
[![Linux Build Status](https://travis-ci.org/vektra/mockery.png)](https://travis-ci.org/vektra/mockery) [![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/vektra/mockery)](https://ci.appveyor.com/project/vektra/mockery) [![GoDoc](https://godoc.org/github.com/vektra/mockery/mockery?status.svg)](https://godoc.org/github.com/vektra/mockery/mockery) [![Go Report Card](https://goreportcard.com/badge/github.com/vektra/mockery)](https://goreportcard.com/report/github.com/vektra/mockery)

### Installation

Expand Down Expand Up @@ -144,8 +144,8 @@ For example, `panic: assert: arguments: Cannot call Get(0) because there are 0 a

#### Notes

This approach should be used judiciously, as return values should generally
not depend on arguments in mocks; however, this approach can be helpful for
This approach should be used judiciously, as return values should generally
not depend on arguments in mocks; however, this approach can be helpful for
situations like passthroughs or other test-only calculations.

### Name
Expand All @@ -170,6 +170,12 @@ This option is only compatible with `-name`. The `-all` option implies `-recursi
mockery always generates files with the package `mocks` to keep things clean and simple.
You can control which mocks directory is used by using `-output`, which defaults to `./mocks`.

### In Package (-inpkg) and KeepTree (-keeptree)

For some complex repositories, there could be multiple interfaces with the same name but in different packages. In that case, `-inpkg` allows generate the mocked interfaces directly in the package that it mocks.

In the case you don't want to generate the mocks into the package but want to keep a similar structure, use the option `-keeptree`.

## Caseing

mockery generates files using the caseing of the original interface name. This
Expand Down
16 changes: 12 additions & 4 deletions cmd/mockery/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
fProfile string
fVersion bool
quiet bool
fkeepTree bool
}

func main() {
Expand Down Expand Up @@ -69,6 +70,10 @@ func main() {
os.Exit(1)
}

if config.fkeepTree {
config.fIP = false
}

if config.fProfile != "" {
f, err := os.Create(config.fProfile)
if err != nil {
Expand All @@ -85,10 +90,12 @@ func main() {
osp = &mockery.StdoutStreamProvider{}
} else {
osp = &mockery.FileOutputStreamProvider{
BaseDir: config.fOutput,
InPackage: config.fIP,
TestOnly: config.fTO,
Case: config.fCase,
BaseDir: config.fOutput,
InPackage: config.fIP,
TestOnly: config.fTO,
Case: config.fCase,
KeepTree: config.fkeepTree,
KeepTreeOriginalDirectory: config.fDir,
}
}

Expand Down Expand Up @@ -133,6 +140,7 @@ func parseConfigFromArgs(args []string) Config {
flagSet.StringVar(&config.fProfile, "cpuprofile", "", "write cpu profile to file")
flagSet.BoolVar(&config.fVersion, "version", false, "prints the installed version of mockery")
flagSet.BoolVar(&config.quiet, "quiet", false, "suppress output to stdout")
flagSet.BoolVar(&config.fkeepTree, "keeptree", false, "keep the tree structure of the original interface files into a different repository. Must be used with XX")

flagSet.Parse(args[1:])

Expand Down
24 changes: 18 additions & 6 deletions mockery/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func (this *StdoutStreamProvider) GetWriter(iface *Interface, pkg string) (io.Wr
}

type FileOutputStreamProvider struct {
BaseDir string
InPackage bool
TestOnly bool
Case string
BaseDir string
InPackage bool
TestOnly bool
Case string
KeepTree bool
KeepTreeOriginalDirectory string
}

func (this *FileOutputStreamProvider) GetWriter(iface *Interface, pkg string) (io.Writer, error, Cleanup) {
Expand All @@ -37,7 +39,17 @@ func (this *FileOutputStreamProvider) GetWriter(iface *Interface, pkg string) (i
caseName = this.underscoreCaseName(caseName)
}

if this.InPackage {
if this.KeepTree {
absOriginalDir, err := filepath.Abs(this.KeepTreeOriginalDirectory)
if err != nil {
return nil, err, func() error { return nil }
}
relativePath := strings.TrimPrefix(
filepath.Join(filepath.Dir(iface.Path), this.filename(caseName)),
absOriginalDir)
path = filepath.Join(this.BaseDir, relativePath)
os.MkdirAll(filepath.Dir(path), 0755)
} else if this.InPackage {
path = filepath.Join(filepath.Dir(iface.Path), this.filename(caseName))
} else {
path = filepath.Join(this.BaseDir, this.filename(caseName))
Expand All @@ -50,7 +62,7 @@ func (this *FileOutputStreamProvider) GetWriter(iface *Interface, pkg string) (i
return nil, err, func() error { return nil }
}

fmt.Printf("Generating mock for: %s\n", iface.Name)
fmt.Printf("Generating mock for: %s in file: %s\n", iface.Name, path)
return f, nil, func() error {
return f.Close()
}
Expand Down

0 comments on commit 126a261

Please sign in to comment.