Skip to content

Commit

Permalink
add skip-ensure flag to avoid import cycle (#140)
Browse files Browse the repository at this point in the history
For mocks generated outside of the tested package with tests lives inside the same package as the tested code (i.e. pkg_test not used) --skip-ensure suppresses import of the source pkg

#139

fix typo in readme
  • Loading branch information
umputun committed Sep 17, 2020
1 parent a69ca93 commit be64288
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 34 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ moq [flags] source-dir interface [interface2 [interface3 [...]]]
package name (default will infer)
-stub
return zero values when no mock implementation is provided, do not panic
-skip-ensure
suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package
Specifying an alias for the mock is also supported with the format 'interface:alias'
Ex: moq -pkg different . MyInterface:MyMock
```
Expand Down
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
var version string

type userFlags struct {
outFile string
pkgName string
formatter string
stubImpl bool
args []string
outFile string
pkgName string
formatter string
stubImpl bool
skipEnsure bool
args []string
}

func main() {
Expand All @@ -31,6 +32,8 @@ func main() {
flag.StringVar(&flags.formatter, "fmt", "", "go pretty-printer: gofmt, goimports or noop (default gofmt)")
flag.BoolVar(&flags.stubImpl, "stub", false,
"return zero values when no mock implementation is provided, do not panic")
flag.BoolVar(&flags.skipEnsure, "skip-ensure", false,
"suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package")

flag.Usage = func() {
fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`)
Expand Down Expand Up @@ -62,10 +65,11 @@ func run(flags userFlags) error {

srcDir, args := flags.args[0], flags.args[1:]
m, err := moq.New(moq.Config{
SrcDir: srcDir,
PkgName: flags.pkgName,
Formatter: flags.formatter,
StubImpl: flags.stubImpl,
SrcDir: srcDir,
PkgName: flags.pkgName,
Formatter: flags.formatter,
StubImpl: flags.stubImpl,
SkipEnsure: flags.skipEnsure,
})
if err != nil {
return err
Expand Down
39 changes: 22 additions & 17 deletions pkg/moq/moq.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,25 @@ import (

// Mocker can generate mock structs.
type Mocker struct {
srcPkg *packages.Package
tmpl *template.Template
pkgName string
pkgPath string
fmter func(src []byte) ([]byte, error)
stubImpl bool
srcPkg *packages.Package
tmpl *template.Template
pkgName string
pkgPath string
fmter func(src []byte) ([]byte, error)
stubImpl bool
skipEnsure bool

imports map[string]bool
}

// Config specifies details about how interfaces should be mocked.
// SrcDir is the only field which needs be specified.
type Config struct {
SrcDir string
PkgName string
Formatter string
StubImpl bool
SrcDir string
PkgName string
Formatter string
StubImpl bool
SkipEnsure bool
}

// New makes a new Mocker for the specified package directory.
Expand Down Expand Up @@ -69,13 +71,14 @@ func New(conf Config) (*Mocker, error) {
}

return &Mocker{
tmpl: tmpl,
srcPkg: srcPkg,
pkgName: pkgName,
pkgPath: pkgPath,
fmter: fmter,
stubImpl: conf.StubImpl,
imports: make(map[string]bool),
tmpl: tmpl,
srcPkg: srcPkg,
pkgName: pkgName,
pkgPath: pkgPath,
fmter: fmter,
stubImpl: conf.StubImpl,
skipEnsure: conf.SkipEnsure,
imports: make(map[string]bool),
}, nil
}

Expand Down Expand Up @@ -114,6 +117,7 @@ func (m *Mocker) Mock(w io.Writer, names ...string) error {
PackageName: m.pkgName,
Imports: moqImports,
StubImpl: m.stubImpl,
SkipEnsure: m.skipEnsure,
}

mocksMethods := false
Expand Down Expand Up @@ -254,6 +258,7 @@ type doc struct {
Objects []obj
Imports []string
StubImpl bool
SkipEnsure bool
}

type obj struct {
Expand Down
27 changes: 27 additions & 0 deletions pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,33 @@ func TestMoqExplicitPackageWithStaticCheck(t *testing.T) {
}
}

func TestMoqSkipEnsure(t *testing.T) {
m, err := New(Config{SrcDir: "testpackages/example", PkgName: "different", SkipEnsure: true})
if err != nil {
t.Fatalf("moq.New: %s", err)
}
var buf bytes.Buffer
err = m.Mock(&buf, "PersonStore")
if err != nil {
t.Errorf("m.Mock: %s", err)
}
s := buf.String()
// assertions of things that should be mentioned
var strs = []string{
"package different",
"type PersonStoreMock struct",
"CreateFunc func(ctx context.Context, person *example.Person, confirm bool) error",
"GetFunc func(ctx context.Context, id string) (*example.Person, error)",
"func (mock *PersonStoreMock) Create(ctx context.Context, person *example.Person, confirm bool) error",
"func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*example.Person, error)",
}
for _, str := range strs {
if !strings.Contains(s, str) {
t.Errorf("expected but missing: \"%s\"", str)
}
}
}

func TestNotCreatingEmptyDirWhenPkgIsGiven(t *testing.T) {
m, err := New(Config{SrcDir: "testpackages/example", PkgName: "different"})
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/moq/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var moqTemplate = `// Code generated by moq; DO NOT EDIT.
package {{.PackageName}}
{{- $sourcePackagePrefix := .SourcePackagePrefix}}
{{- $stubImpl := .StubImpl}}
{{- $skipEnsure := .SkipEnsure}}
import (
{{- range .Imports }}
Expand All @@ -20,9 +21,11 @@ import (
{{ range $i, $obj := .Objects -}}
{{- if not $skipEnsure -}}
// Ensure, that {{.MockName}} does implement {{$sourcePackagePrefix}}{{.InterfaceName}}.
// If this is not the case, regenerate this file with moq.
var _ {{$sourcePackagePrefix}}{{.InterfaceName}} = &{{.MockName}}{}
{{- end }}
// {{.MockName}} is a mock implementation of {{$sourcePackagePrefix}}{{.InterfaceName}}.
//
Expand Down
13 changes: 5 additions & 8 deletions pkg/moq/testpackages/gogenvendoring/user/user_moq_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be64288

Please sign in to comment.