Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

support mocking vendored dependencies #28

Merged
merged 2 commits into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mockgen/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the referenced bug says, PkgPath is not very well-defined. If we're going to address this, we should cope with /internal/ dirs too.

I wonder if this transformation should be happening later. Or if this should be in a library somewhere else. Surely gomock isn't the only program around that wants to handle vendored import paths?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the internal/ directory problem--could you elaborate? Is it necessary to address that in this PR?

I did some searches and didn't find a library for this. IMO it's such a small piece of logic a library feels like overkill.

Copy link

@kaedys kaedys May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal/ directories can only be accessed by packages in or descending from the directory immediately above the internal/ directory. It was a predecessor to the vendoring experiment started in Go 1.5. Here's the details:

https://golang.org/doc/go1.4#internalpackages
https://docs.google.com/document/d/1e8kOo3r51b2BWtTs_1uADIA5djfXhPT36s6eHVRIvaU/edit?pref=2&pli=1

Mechanically, it works similar, except without the import path eliding that vendoring added. Where package github.com/foo, when vendored, is imported as github.com/foo, if placed in the internal directory instead, it would have to be imported as, for example, github.com/bar/internal/github.com/foo. However, like the vendored situation, it would only be accessible to that bar package and its subordinates. Main difference is that, if github.com/foo isn't present in the project tree, instead of falling back to the $GOPATH/src location to check for an alternate version (like vendoring does), the internal one will throw a compile time error about the package being missing.

It also differs slightly on scope. If you have this:

└── src
    ├── A
    ├── B
    │   ├── internal
    │   │   └── bar
    │   └── vendor
    │       └── foo
    ├── bar
    └── foo

Then importing "foo" in A would import src/foo and importing it in B would import src/B/internal/foo. However, importing "bar" in either A or B would import src/bar. Importing src/B/internal/bar would import exactly that in B, but if that same import line were in A, the compiler would instead throw an error use of internal package not allowed, since it is trying to access an internal package while outside the scope allowed for that internal package (which is B and its descendants)

imp = imp[i+len("/vendor/"):]
}
return &NamedType{
Package: imp,
Type: t.Name(),
Expand Down
2 changes: 2 additions & 0 deletions mockgen/tests/vendor_dep/README.md
Original file line number Diff line number Diff line change
@@ -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).
3 changes: 3 additions & 0 deletions mockgen/tests/vendor_dep/doc.go
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions mockgen/tests/vendor_dep/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions mockgen/tests/vendor_dep/vendor/a/a.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions mockgen/tests/vendor_dep/vendor_dep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package vendor_dep

import "a"

type VendorsDep interface {
Foo() a.Ifc
}