From ede55fbbe72e1678185cae4336a16ca260f61d40 Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Thu, 27 Jul 2017 21:08:06 +0200 Subject: [PATCH 1/2] [PATCH] Omit sync package while mocking empty interface when just an empty interface is has beeing mocked, the import of the sync package caused an error. In production it isn't important, because mocking empty interfaces might be stupid, but while writing tests it might happen, that you create an interface, which is beeing filled later on. --- pkg/moq/moq.go | 5 +++++ pkg/moq/moq_test.go | 16 ++++++++++++++++ pkg/moq/template.go | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index 3844e67..d4846cc 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -69,6 +69,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { PackageName: m.pkgName, Imports: moqImports, } + mocksMethods := false for _, pkg := range m.pkgs { i := 0 files := make([]*ast.File, len(pkg.Files)) @@ -94,6 +95,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { InterfaceName: n, } for i := 0; i < iiface.NumMethods(); i++ { + mocksMethods = true meth := iiface.Method(i) sig := meth.Type().(*types.Signature) method := &method{ @@ -106,6 +108,9 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { doc.Objects = append(doc.Objects, obj) } } + if mocksMethods { + doc.Imports = append(doc.Imports, "sync") + } for pkgToImport := range m.imports { doc.Imports = append(doc.Imports, pkgToImport) } diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index a0429b0..badd1f7 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -214,3 +214,19 @@ func TestDotImports(t *testing.T) { t.Error("contains invalid dot import") } } + +func TestEmptyInterface(t *testing.T) { + m, err := New("testpackages/emptyinterface", "") + if err != nil { + t.Fatalf("moq.New: %s", err) + } + var buf bytes.Buffer + err = m.Mock(&buf, "Empty") + if err != nil { + t.Errorf("mock error: %s", err) + } + s := buf.String() + if strings.Contains(s, `"sync"`) { + t.Error("contains sync import, although this package isn't used") + } +} diff --git a/pkg/moq/template.go b/pkg/moq/template.go index a181507..4d70827 100644 --- a/pkg/moq/template.go +++ b/pkg/moq/template.go @@ -1,7 +1,7 @@ package moq // moqImports are the imports all moq files get. -var moqImports = []string{"sync"} +var moqImports = []string{} // moqTemplate is the template for mocked code. var moqTemplate = `// Code generated by moq; DO NOT EDIT @@ -35,7 +35,7 @@ var ( // // // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}} // // and then make assertions. -// +// // } type {{.InterfaceName}}Mock struct { {{- range .Methods }} @@ -43,7 +43,7 @@ type {{.InterfaceName}}Mock struct { {{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}} {{ end }} // calls tracks calls to the methods. - calls struct { + calls struct { {{- range .Methods }} // {{ .Name }} holds details about calls to the {{.Name}} method. {{ .Name }} []struct { From a2671b9bfc9ca57bf0c0fd32b89a2da95bb262d3 Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Wed, 2 Aug 2017 22:56:53 +0200 Subject: [PATCH 2/2] Added missing test package for empty interface mocking --- pkg/moq/testpackages/emptyinterface/empty.go | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 pkg/moq/testpackages/emptyinterface/empty.go diff --git a/pkg/moq/testpackages/emptyinterface/empty.go b/pkg/moq/testpackages/emptyinterface/empty.go new file mode 100644 index 0000000..0e05247 --- /dev/null +++ b/pkg/moq/testpackages/emptyinterface/empty.go @@ -0,0 +1,4 @@ +package emptyinterface + +// Empty is an empty interface +type Empty interface{}