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/moq.go b/pkg/moq/moq.go index d4846cc..f4b43c0 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" @@ -114,10 +116,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 } diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index badd1f7..4cd6fc9 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) @@ -210,7 +220,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") } } 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) } 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