Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new KeepTree flag - allows to preserve tree struct for mocks #175

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
buildTags string
}

Expand Down Expand Up @@ -70,6 +71,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 @@ -86,10 +91,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 @@ -135,6 +142,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.StringVar(&config.buildTags, "tags", "", "space-separated list of additional build tags to use")

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