From c4b6b29db7f8073b1c5adbb67a444b7c0168b858 Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Thu, 27 Jul 2017 21:02:36 +0200 Subject: [PATCH 1/5] [PATCH] Remove side effect caused by chdir from test - fixes #23 The TestDotImports changed the directory, so every test afterwards was executed from another directory and calls like New("testpackages/emptyinterface", "") failed. --- pkg/moq/moq_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index a0429b0..ed159b7 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -196,10 +196,20 @@ func TestVendoredPackages(t *testing.T) { // TestDotImports tests for https://github.com/matryer/moq/issues/21. func TestDotImports(t *testing.T) { - err := os.Chdir("testpackages/dotimport") + preDir, err := os.Getwd() + if err != nil { + t.Errorf("Getwd: %s", err) + } + err = os.Chdir("testpackages/dotimport") if err != nil { t.Errorf("Chdir: %s", err) } + defer func() { + err := os.Chdir(preDir) + if err != nil { + t.Errorf("Chdir back: %s", err) + } + }() m, err := New(".", "moqtest_test") if err != nil { t.Fatalf("moq.New: %s", err) From 10bc28350278b9c7cda654e84967f64f6f8f43cd Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Thu, 27 Jul 2017 21:05:01 +0200 Subject: [PATCH 2/5] [PATCH] Made tests independent from package path The test case checked for a whole package path, which should exist in a generated moq file, but that failed for obvious reasons in forks --- pkg/moq/moq_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index a0429b0..4ba323f 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -210,7 +210,7 @@ func TestDotImports(t *testing.T) { t.Errorf("mock error: %s", err) } s := buf.String() - if !strings.Contains(s, `"github.com/matryer/moq/pkg/moq/testpackages/dotimport"`) { + if !strings.Contains(s, `/moq/pkg/moq/testpackages/dotimport"`) { t.Error("contains invalid dot import") } } From fc157a141f8fbad9c2f40479329e6bfd751936b4 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Mon, 31 Jul 2017 15:09:54 +0100 Subject: [PATCH 3/5] added go fmt --- pkg/moq/moq.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index 3844e67..fc417e3 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -1,9 +1,11 @@ package moq import ( + "bytes" "errors" "fmt" "go/ast" + "go/format" "go/parser" "go/token" "go/types" @@ -109,10 +111,18 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { for pkgToImport := range m.imports { doc.Imports = append(doc.Imports, pkgToImport) } - err := m.tmpl.Execute(w, doc) + var buf bytes.Buffer + err := m.tmpl.Execute(&buf, doc) if err != nil { return err } + formatted, err := format.Source(buf.Bytes()) + if err != nil { + return fmt.Errorf("go/format: %s", err) + } + if _, err := w.Write(formatted); err != nil { + return err + } return nil } From bd3bf336133bdc4b29143db6ff1d15ea16f4b564 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Wed, 2 Aug 2017 16:05:40 +0100 Subject: [PATCH 4/5] golint fixes --- pkg/moq/importer.go | 2 +- pkg/moq/testpackages/example/example.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/moq/importer.go b/pkg/moq/importer.go index 1445459..07747b8 100644 --- a/pkg/moq/importer.go +++ b/pkg/moq/importer.go @@ -177,7 +177,7 @@ func newImporter(source string) types.Importer { // } // stripGopath teks the directory to a package and remove the gopath to get the -// cannonical package name +// canonical package name. func stripGopath(p string) string { for _, gopath := range gopaths() { p = strings.TrimPrefix(p, path.Join(gopath, "src")+"/") diff --git a/pkg/moq/testpackages/example/example.go b/pkg/moq/testpackages/example/example.go index c7a19d7..ee9a5d5 100644 --- a/pkg/moq/testpackages/example/example.go +++ b/pkg/moq/testpackages/example/example.go @@ -2,6 +2,7 @@ package example import "context" +// Person is a person. type Person struct { ID string Name string @@ -9,6 +10,7 @@ type Person struct { Website string } +// PersonStore stores people. type PersonStore interface { Get(ctx context.Context, id string) (*Person, error) Create(ctx context.Context, person *Person, confirm bool) error From 71d368f14f19a7f952ad0cb58b458ffe2e094efe Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Wed, 2 Aug 2017 16:06:35 +0100 Subject: [PATCH 5/5] golint fixes --- pkg/moq/testpackages/channels/example.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/moq/testpackages/channels/example.go b/pkg/moq/testpackages/channels/example.go index 5dfd0d0..cef8ce8 100644 --- a/pkg/moq/testpackages/channels/example.go +++ b/pkg/moq/testpackages/channels/example.go @@ -1,7 +1,9 @@ package channels +// Queue is a type to be sent down a channel. type Queue []string +// Queuer provides a channel example. type Queuer interface { Sub(topic string) (<-chan Queue, error) }