Handle target interface inside vendor with other pkg name (#122)

When the interface that we are trying to moq is present inside the vendor
directory and the output package name is different, the import needs to be
stripped of the vendor path.
This commit is contained in:
Suhas Karanth 2020-01-20 07:16:57 +05:30 committed by GitHub
parent 8f50fb6793
commit 072c0cd09b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -177,7 +177,7 @@ func (m *Mocker) Mock(w io.Writer, names ...string) error {
if tpkg.Name() != m.pkgName { if tpkg.Name() != m.pkgName {
doc.SourcePackagePrefix = tpkg.Name() + "." doc.SourcePackagePrefix = tpkg.Name() + "."
doc.Imports = append(doc.Imports, tpkg.Path()) doc.Imports = append(doc.Imports, stripVendorPath(tpkg.Path()))
} }
var buf bytes.Buffer var buf bytes.Buffer

View File

@ -311,6 +311,32 @@ func TestVendoredPackages(t *testing.T) {
} }
} }
func TestVendoredInterface(t *testing.T) {
m, err := New("testpackages/vendoring/vendor/github.com/matryer/somerepo", "someother")
if err != nil {
t.Fatalf("moq.New: %s", err)
}
var buf bytes.Buffer
err = m.Mock(&buf, "SomeService")
if err != nil {
t.Errorf("mock error: %s", err)
}
s := buf.String()
// assertions of things that should be mentioned
var strs = []string{
`"github.com/matryer/somerepo"`,
}
for _, str := range strs {
if !strings.Contains(s, str) {
t.Errorf("expected but missing: \"%s\"", str)
}
}
incorrectImport := `"github.com/matryer/moq/pkg/moq/testpackages/vendoring/vendor/github.com/matryer/somerepo"`
if strings.Contains(s, incorrectImport) {
t.Errorf("unexpected import: %s", incorrectImport)
}
}
func TestVendoredBuildConstraints(t *testing.T) { func TestVendoredBuildConstraints(t *testing.T) {
m, err := New("testpackages/buildconstraints/user", "") m, err := New("testpackages/buildconstraints/user", "")
if err != nil { if err != nil {

View File

@ -7,3 +7,7 @@ type SomeType struct {
// Truth indicates whether true is true or not. Computers. // Truth indicates whether true is true or not. Computers.
Truth bool Truth bool
} }
type SomeService interface {
Get() SomeType
}