diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index bfa7fe1..626033a 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -2,7 +2,9 @@ package moq import ( "bytes" + "io" "os" + "os/exec" "strings" "testing" ) @@ -241,3 +243,31 @@ func TestEmptyInterface(t *testing.T) { t.Error("contains sync import, although this package isn't used") } } + +func TestGoGenerateVendoredPackages(t *testing.T) { + cmd := exec.Command("go", "generate", "./...") + cmd.Dir = "testpackages/gogenvendoring" + stdout, err := cmd.StdoutPipe() + if err != nil { + t.Errorf("StdoutPipe: %s", err) + } + defer stdout.Close() + err = cmd.Start() + if err != nil { + t.Errorf("Start: %s", err) + } + buf := bytes.NewBuffer(nil) + io.Copy(buf, stdout) + err = cmd.Wait() + if err != nil { + if exitErr, ok := err.(*exec.ExitError); ok { + t.Errorf("Wait: %s %s", exitErr, string(exitErr.Stderr)) + } else { + t.Errorf("Wait: %s", err) + } + } + s := buf.String() + if strings.Contains(s, `vendor/`) { + t.Error("contains vendor directory in import path") + } +} 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..debc0fa --- /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 ( + "github.com/matryer/moq/pkg/moq/testpackages/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/gogenvendoring/user/user.go b/pkg/moq/testpackages/gogenvendoring/user/user.go new file mode 100644 index 0000000..6391d05 --- /dev/null +++ b/pkg/moq/testpackages/gogenvendoring/user/user.go @@ -0,0 +1,10 @@ +package user + +import "github.com/matryer/somerepo" + +//go:generate moq . Service + +// Service does something good with computers. +type Service interface { + DoSomething(somerepo.SomeType) error +} diff --git a/pkg/vendor/github.com/matryer/somerepo/yep.go b/pkg/vendor/github.com/matryer/somerepo/yep.go new file mode 100644 index 0000000..8f4ff9a --- /dev/null +++ b/pkg/vendor/github.com/matryer/somerepo/yep.go @@ -0,0 +1,3 @@ +package somerepo + +type SomeType string