From 126a2612975707e6481838e38e183b07ef79d745 Mon Sep 17 00:00:00 2001 From: Corentin Debains Date: Mon, 6 Nov 2017 21:30:44 -0800 Subject: [PATCH] Adding new KeepTree flag - allows to preserve tree struct for mocks * 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. --- README.md | 12 +++++++++--- cmd/mockery/mockery.go | 16 ++++++++++++---- mockery/outputter.go | 24 ++++++++++++++++++------ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cc877f0b..8f0624a8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/cmd/mockery/mockery.go b/cmd/mockery/mockery.go index 9b264064..e08ab9aa 100644 --- a/cmd/mockery/mockery.go +++ b/cmd/mockery/mockery.go @@ -29,6 +29,7 @@ type Config struct { fProfile string fVersion bool quiet bool + fkeepTree bool } func main() { @@ -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 { @@ -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, } } @@ -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:]) diff --git a/mockery/outputter.go b/mockery/outputter.go index c96d7043..e0e90e10 100644 --- a/mockery/outputter.go +++ b/mockery/outputter.go @@ -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) { @@ -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)) @@ -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() }