From 879118f880c84c49b90f48eb1d02ead73e4ad8c5 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Tue, 18 Jul 2017 09:45:29 +0100 Subject: [PATCH] Single dot imports explicitly not-included - fixes #21 --- pkg/moq/moq.go | 3 +++ pkg/moq/moq_test.go | 22 +++++++++++++++++++ pkg/moq/testpackages/issue-21/service.go | 14 ++++++++++++ pkg/moq/testpackages/issue-21/service_test.go | 10 +++++++++ 4 files changed, 49 insertions(+) create mode 100644 pkg/moq/testpackages/issue-21/service.go create mode 100644 pkg/moq/testpackages/issue-21/service_test.go diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index c4d4b8e..d64a4ed 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -120,6 +120,9 @@ func (m *Mocker) packageQualifier(pkg *types.Package) string { if m.pkgName == pkg.Name() { return "" } + if pkg.Path() == "." { + return "" + } m.imports[pkg.Path()] = true return pkg.Name() } diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 4188440..0759e86 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -2,6 +2,7 @@ package moq import ( "bytes" + "os" "strings" "testing" ) @@ -192,3 +193,24 @@ func TestVendoredPackages(t *testing.T) { } } } + +// TestDotImports tests for https://github.com/matryer/moq/issues/21. +func TestDotImports(t *testing.T) { + err := os.Chdir("testpackages/issue-21") + if err != nil { + t.Errorf("Chdir: %s", err) + } + m, err := New(".", "moqtest_test") + if err != nil { + t.Fatalf("moq.New: %s", err) + } + var buf bytes.Buffer + err = m.Mock(&buf, "Service") + if err != nil { + t.Errorf("mock error: %s", err) + } + s := buf.String() + if strings.Contains(s, `"."`) { + t.Error("contains invalid dot import") + } +} diff --git a/pkg/moq/testpackages/issue-21/service.go b/pkg/moq/testpackages/issue-21/service.go new file mode 100644 index 0000000..f289863 --- /dev/null +++ b/pkg/moq/testpackages/issue-21/service.go @@ -0,0 +1,14 @@ +// Package moqtest is just a package to try getting started with moq without the overhead of a real package +package moqtest + +//go:generate moq -out service_moq_test.go -pkg moqtest_test . Service + +// Service is the interface which should be mocked by moq +type Service interface { + User(ID string) (User, error) +} + +// User is just a struct for testing +type User struct { + Name string +} diff --git a/pkg/moq/testpackages/issue-21/service_test.go b/pkg/moq/testpackages/issue-21/service_test.go new file mode 100644 index 0000000..5014b9b --- /dev/null +++ b/pkg/moq/testpackages/issue-21/service_test.go @@ -0,0 +1,10 @@ +package moqtest_test + +import "testing" + +func TestServiceMock(t *testing.T) { + // Here I want to use the ServiceMock + + service := ServiceMock{} + +}