From 208c7723bbb3f5a0bd7689e6e7e88f9e52987f1b Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Mon, 14 Mar 2016 21:09:55 +0000 Subject: [PATCH 1/2] support mocking vendored dependencies - Generate temporary program in current directory tree to pick up vendored dependencies during build - Clean up the return of `PkgPath` according to the advice of rsc here https://github.com/golang/go/issues/12019#issuecomment-148747953 --- mockgen/model/model.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mockgen/model/model.go b/mockgen/model/model.go index 1df8ef3b..c8af9a34 100644 --- a/mockgen/model/model.go +++ b/mockgen/model/model.go @@ -354,6 +354,13 @@ func typeFromType(t reflect.Type) (Type, error) { } if imp := t.PkgPath(); imp != "" { + // PkgPath might return a path that includes "vendor" + // These paths do not compile, so we need to remove everything + // up to and including "/vendor/" + // see https://github.com/golang/go/issues/12019 + if i := strings.LastIndex(imp, "/vendor/"); i != -1 { + imp = imp[i+len("/vendor/"):] + } return &NamedType{ Package: imp, Type: t.Name(), From 77f22c2b84e452a6b76fb0d1b09c9cfb44746aa4 Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Wed, 7 Feb 2018 18:26:40 +0000 Subject: [PATCH 2/2] add test --- mockgen/tests/vendor_dep/README.md | 2 ++ mockgen/tests/vendor_dep/doc.go | 3 ++ mockgen/tests/vendor_dep/mock.go | 46 ++++++++++++++++++++++++++ mockgen/tests/vendor_dep/vendor/a/a.go | 10 ++++++ mockgen/tests/vendor_dep/vendor_dep.go | 7 ++++ 5 files changed, 68 insertions(+) create mode 100644 mockgen/tests/vendor_dep/README.md create mode 100644 mockgen/tests/vendor_dep/doc.go create mode 100644 mockgen/tests/vendor_dep/mock.go create mode 100644 mockgen/tests/vendor_dep/vendor/a/a.go create mode 100644 mockgen/tests/vendor_dep/vendor_dep.go diff --git a/mockgen/tests/vendor_dep/README.md b/mockgen/tests/vendor_dep/README.md new file mode 100644 index 00000000..9f9217d2 --- /dev/null +++ b/mockgen/tests/vendor_dep/README.md @@ -0,0 +1,2 @@ +Test for [Issue#4](https://github.com/golang/mock/issues/4). +Also see discussion on [#28](https://github.com/golang/mock/pull/28). diff --git a/mockgen/tests/vendor_dep/doc.go b/mockgen/tests/vendor_dep/doc.go new file mode 100644 index 00000000..595f679b --- /dev/null +++ b/mockgen/tests/vendor_dep/doc.go @@ -0,0 +1,3 @@ +package vendor_dep + +//go:generate mockgen -package vendor_dep -destination mock.go github.com/golang/mock/mockgen/tests/vendor_dep VendorsDep diff --git a/mockgen/tests/vendor_dep/mock.go b/mockgen/tests/vendor_dep/mock.go new file mode 100644 index 00000000..aace06e6 --- /dev/null +++ b/mockgen/tests/vendor_dep/mock.go @@ -0,0 +1,46 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/golang/mock/mockgen/tests/vendor_dep (interfaces: VendorsDep) + +// Package vendor_dep is a generated GoMock package. +package vendor_dep + +import ( + a "a" + gomock "github.com/golang/mock/gomock" + reflect "reflect" +) + +// MockVendorsDep is a mock of VendorsDep interface +type MockVendorsDep struct { + ctrl *gomock.Controller + recorder *MockVendorsDepMockRecorder +} + +// MockVendorsDepMockRecorder is the mock recorder for MockVendorsDep +type MockVendorsDepMockRecorder struct { + mock *MockVendorsDep +} + +// NewMockVendorsDep creates a new mock instance +func NewMockVendorsDep(ctrl *gomock.Controller) *MockVendorsDep { + mock := &MockVendorsDep{ctrl: ctrl} + mock.recorder = &MockVendorsDepMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockVendorsDep) EXPECT() *MockVendorsDepMockRecorder { + return m.recorder +} + +// Foo mocks base method +func (m *MockVendorsDep) Foo() a.Ifc { + ret := m.ctrl.Call(m, "Foo") + ret0, _ := ret[0].(a.Ifc) + return ret0 +} + +// Foo indicates an expected call of Foo +func (mr *MockVendorsDepMockRecorder) Foo() *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockVendorsDep)(nil).Foo)) +} diff --git a/mockgen/tests/vendor_dep/vendor/a/a.go b/mockgen/tests/vendor_dep/vendor/a/a.go new file mode 100644 index 00000000..156f70b1 --- /dev/null +++ b/mockgen/tests/vendor_dep/vendor/a/a.go @@ -0,0 +1,10 @@ +package a + +type Ifc interface { + A(string) string + B(int) int + C(chan int) chan int + D(interface{}) + E(map[string]interface{}) + F([]float64) +} diff --git a/mockgen/tests/vendor_dep/vendor_dep.go b/mockgen/tests/vendor_dep/vendor_dep.go new file mode 100644 index 00000000..412636a2 --- /dev/null +++ b/mockgen/tests/vendor_dep/vendor_dep.go @@ -0,0 +1,7 @@ +package vendor_dep + +import "a" + +type VendorsDep interface { + Foo() a.Ifc +}