From e71b1c1ebe5a557643ac2367a31c897d831b8e13 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Tue, 18 Jul 2017 10:00:26 +0100 Subject: [PATCH] Better solution for #21 --- pkg/moq/moq.go | 8 +- pkg/moq/moq_test.go | 4 +- pkg/moq/testpackages/dotimport/service.go | 14 ++++ .../dotimport/service_moq_test.go | 73 +++++++++++++++++++ .../{issue-21 => dotimport}/service_test.go | 2 +- pkg/moq/testpackages/issue-21/service.go | 14 ---- 6 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 pkg/moq/testpackages/dotimport/service.go create mode 100755 pkg/moq/testpackages/dotimport/service_moq_test.go rename pkg/moq/testpackages/{issue-21 => dotimport}/service_test.go (84%) delete mode 100644 pkg/moq/testpackages/issue-21/service.go diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index d64a4ed..3844e67 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -120,10 +120,14 @@ func (m *Mocker) packageQualifier(pkg *types.Package) string { if m.pkgName == pkg.Name() { return "" } + path := pkg.Path() if pkg.Path() == "." { - return "" + wd, err := os.Getwd() + if err == nil { + path = stripGopath(wd) + } } - m.imports[pkg.Path()] = true + m.imports[path] = true return pkg.Name() } diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 0759e86..a0429b0 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -196,7 +196,7 @@ 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") + err := os.Chdir("testpackages/dotimport") if err != nil { t.Errorf("Chdir: %s", err) } @@ -210,7 +210,7 @@ func TestDotImports(t *testing.T) { t.Errorf("mock error: %s", err) } s := buf.String() - if strings.Contains(s, `"."`) { + if !strings.Contains(s, `"github.com/matryer/moq/pkg/moq/testpackages/dotimport"`) { t.Error("contains invalid dot import") } } diff --git a/pkg/moq/testpackages/dotimport/service.go b/pkg/moq/testpackages/dotimport/service.go new file mode 100644 index 0000000..1a9a2f5 --- /dev/null +++ b/pkg/moq/testpackages/dotimport/service.go @@ -0,0 +1,14 @@ +// Package dotimport addresses issue 21. +package dotimport + +//go:generate moq -out service_moq_test.go -pkg dotimport_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/dotimport/service_moq_test.go b/pkg/moq/testpackages/dotimport/service_moq_test.go new file mode 100755 index 0000000..90a010a --- /dev/null +++ b/pkg/moq/testpackages/dotimport/service_moq_test.go @@ -0,0 +1,73 @@ +// Code generated by moq; DO NOT EDIT +// github.com/matryer/moq + +package dotimport_test + +import ( + "dotimport" + "sync" +) + +var ( + lockServiceMockUser sync.RWMutex +) + +// ServiceMock is a mock implementation of Service. +// +// func TestSomethingThatUsesService(t *testing.T) { +// +// // make and configure a mocked Service +// mockedService := &ServiceMock{ +// UserFunc: func(ID string) (dotimport.User, error) { +// panic("TODO: mock out the User method") +// }, +// } +// +// // TODO: use mockedService in code that requires Service +// // and then make assertions. +// +// } +type ServiceMock struct { + // UserFunc mocks the User method. + UserFunc func(ID string) (dotimport.User, error) + + // calls tracks calls to the methods. + calls struct { + // User holds details about calls to the User method. + User []struct { + // ID is the ID argument value. + ID string + } + } +} + +// User calls UserFunc. +func (mock *ServiceMock) User(ID string) (dotimport.User, error) { + if mock.UserFunc == nil { + panic("moq: ServiceMock.UserFunc is nil but Service.User was just called") + } + callInfo := struct { + ID string + }{ + ID: ID, + } + lockServiceMockUser.Lock() + mock.calls.User = append(mock.calls.User, callInfo) + lockServiceMockUser.Unlock() + return mock.UserFunc(ID) +} + +// UserCalls gets all the calls that were made to User. +// Check the length with: +// len(mockedService.UserCalls()) +func (mock *ServiceMock) UserCalls() []struct { + ID string +} { + var calls []struct { + ID string + } + lockServiceMockUser.RLock() + calls = mock.calls.User + lockServiceMockUser.RUnlock() + return calls +} diff --git a/pkg/moq/testpackages/issue-21/service_test.go b/pkg/moq/testpackages/dotimport/service_test.go similarity index 84% rename from pkg/moq/testpackages/issue-21/service_test.go rename to pkg/moq/testpackages/dotimport/service_test.go index 5014b9b..2e017ab 100644 --- a/pkg/moq/testpackages/issue-21/service_test.go +++ b/pkg/moq/testpackages/dotimport/service_test.go @@ -1,4 +1,4 @@ -package moqtest_test +package dotimport_test import "testing" diff --git a/pkg/moq/testpackages/issue-21/service.go b/pkg/moq/testpackages/issue-21/service.go deleted file mode 100644 index f289863..0000000 --- a/pkg/moq/testpackages/issue-21/service.go +++ /dev/null @@ -1,14 +0,0 @@ -// 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 -}