From df49857c18fa6a638f4ec8beaea7d2581b189fe4 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Tue, 11 Jul 2017 20:02:46 +0100 Subject: [PATCH 1/4] added vendor example --- main.go | 2 +- {package => pkg}/moq/importer.go | 0 {package => pkg}/moq/moq.go | 0 {package => pkg}/moq/moq_test.go | 16 +++++++++++++++- .../moq/testdata/channels/example.go | 0 {package => pkg}/moq/testdata/example/example.go | 0 {package => pkg}/moq/testdata/imports/one/one.go | 0 {package => pkg}/moq/testdata/imports/two/two.go | 2 +- .../moq/testdata/variadic/greeter.go | 0 pkg/moq/testdata/vendoring/user/user.go | 8 ++++++++ .../vendor/github.com/matryer/somerepo/code.go | 9 +++++++++ 11 files changed, 34 insertions(+), 3 deletions(-) rename {package => pkg}/moq/importer.go (100%) rename {package => pkg}/moq/moq.go (100%) rename {package => pkg}/moq/moq_test.go (92%) rename {package => pkg}/moq/testdata/channels/example.go (100%) rename {package => pkg}/moq/testdata/example/example.go (100%) rename {package => pkg}/moq/testdata/imports/one/one.go (100%) rename {package => pkg}/moq/testdata/imports/two/two.go (71%) rename {package => pkg}/moq/testdata/variadic/greeter.go (100%) create mode 100644 pkg/moq/testdata/vendoring/user/user.go create mode 100644 pkg/moq/testdata/vendoring/user/vendor/github.com/matryer/somerepo/code.go diff --git a/main.go b/main.go index d58d2fd..d525994 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( "io/ioutil" "os" - "github.com/matryer/moq/package/moq" + "github.com/matryer/moq/pkg/moq" ) func main() { diff --git a/package/moq/importer.go b/pkg/moq/importer.go similarity index 100% rename from package/moq/importer.go rename to pkg/moq/importer.go diff --git a/package/moq/moq.go b/pkg/moq/moq.go similarity index 100% rename from package/moq/moq.go rename to pkg/moq/moq.go diff --git a/package/moq/moq_test.go b/pkg/moq/moq_test.go similarity index 92% rename from package/moq/moq_test.go rename to pkg/moq/moq_test.go index 1fad8df..dee91f4 100644 --- a/package/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -2,6 +2,7 @@ package moq import ( "bytes" + "log" "strings" "testing" ) @@ -153,7 +154,7 @@ func TestImports(t *testing.T) { s := buf.String() var strs = []string{ ` "sync"`, - ` "github.com/matryer/moq/package/moq/testdata/imports/one"`, + ` "github.com/matryer/moq/pkg/moq/testdata/imports/one"`, } for _, str := range strs { if !strings.Contains(s, str) { @@ -171,3 +172,16 @@ func TestTemplateFuncs(t *testing.T) { t.Errorf("exported didn't work: %s", fn("var")) } } + +func TestVendoredPackages(t *testing.T) { + m, err := New("testdata/vendoring/user", "") + if err != nil { + t.Fatalf("moq.New: %s", err) + } + var buf bytes.Buffer + err = m.Mock(&buf, "Service") + if err != nil { + t.Errorf("mock error: %s", err) + } + log.Println(buf.String()) +} diff --git a/package/moq/testdata/channels/example.go b/pkg/moq/testdata/channels/example.go similarity index 100% rename from package/moq/testdata/channels/example.go rename to pkg/moq/testdata/channels/example.go diff --git a/package/moq/testdata/example/example.go b/pkg/moq/testdata/example/example.go similarity index 100% rename from package/moq/testdata/example/example.go rename to pkg/moq/testdata/example/example.go diff --git a/package/moq/testdata/imports/one/one.go b/pkg/moq/testdata/imports/one/one.go similarity index 100% rename from package/moq/testdata/imports/one/one.go rename to pkg/moq/testdata/imports/one/one.go diff --git a/package/moq/testdata/imports/two/two.go b/pkg/moq/testdata/imports/two/two.go similarity index 71% rename from package/moq/testdata/imports/two/two.go rename to pkg/moq/testdata/imports/two/two.go index a7635a6..1ebba15 100644 --- a/package/moq/testdata/imports/two/two.go +++ b/pkg/moq/testdata/imports/two/two.go @@ -1,7 +1,7 @@ package two import ( - "github.com/matryer/moq/package/moq/testdata/imports/one" + "github.com/matryer/moq/pkg/moq/testdata/imports/one" ) // DoSomething does something. diff --git a/package/moq/testdata/variadic/greeter.go b/pkg/moq/testdata/variadic/greeter.go similarity index 100% rename from package/moq/testdata/variadic/greeter.go rename to pkg/moq/testdata/variadic/greeter.go diff --git a/pkg/moq/testdata/vendoring/user/user.go b/pkg/moq/testdata/vendoring/user/user.go new file mode 100644 index 0000000..798f104 --- /dev/null +++ b/pkg/moq/testdata/vendoring/user/user.go @@ -0,0 +1,8 @@ +package user + +import "github.com/matryer/somerepo" + +// Service does something good with computers. +type Service interface { + DoSomething(somerepo.SomeType) error +} diff --git a/pkg/moq/testdata/vendoring/user/vendor/github.com/matryer/somerepo/code.go b/pkg/moq/testdata/vendoring/user/vendor/github.com/matryer/somerepo/code.go new file mode 100644 index 0000000..706bf42 --- /dev/null +++ b/pkg/moq/testdata/vendoring/user/vendor/github.com/matryer/somerepo/code.go @@ -0,0 +1,9 @@ +// Package somerepo is a vendored package to test how moq deals with +// packages in the vendor package. +package somerepo + +// SomeType is just some old type. +type SomeType struct { + // Truth indicates whether true is true or not. Computers. + Truth bool +} From 63b6f24493a26420a850db38ecfaede263cd898a Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Tue, 11 Jul 2017 20:21:07 +0100 Subject: [PATCH 2/4] test data -> testpackages --- pkg/moq/moq_test.go | 16 ++++++++-------- .../channels/example.go | 0 .../example/example.go | 0 .../imports/one/one.go | 0 .../imports/two/two.go | 2 +- .../variadic/greeter.go | 0 .../vendoring/user/user.go | 0 .../vendor/github.com/matryer/somerepo/code.go | 0 8 files changed, 9 insertions(+), 9 deletions(-) rename pkg/moq/{testdata => testpackages}/channels/example.go (100%) rename pkg/moq/{testdata => testpackages}/example/example.go (100%) rename pkg/moq/{testdata => testpackages}/imports/one/one.go (100%) rename pkg/moq/{testdata => testpackages}/imports/two/two.go (71%) rename pkg/moq/{testdata => testpackages}/variadic/greeter.go (100%) rename pkg/moq/{testdata => testpackages}/vendoring/user/user.go (100%) rename pkg/moq/{testdata => testpackages}/vendoring/user/vendor/github.com/matryer/somerepo/code.go (100%) diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index dee91f4..bfdab08 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -8,7 +8,7 @@ import ( ) func TestMoq(t *testing.T) { - m, err := New("testdata/example", "") + m, err := New("testpackages/example", "") if err != nil { t.Fatalf("moq.New: %s", err) } @@ -41,7 +41,7 @@ func TestMoq(t *testing.T) { } func TestMoqExplicitPackage(t *testing.T) { - m, err := New("testdata/example", "different") + m, err := New("testpackages/example", "different") if err != nil { t.Fatalf("moq.New: %s", err) } @@ -71,7 +71,7 @@ func TestMoqExplicitPackage(t *testing.T) { // expected. // see https://github.com/matryer/moq/issues/5 func TestVariadicArguments(t *testing.T) { - m, err := New("testdata/variadic", "") + m, err := New("testpackages/variadic", "") if err != nil { t.Fatalf("moq.New: %s", err) } @@ -96,7 +96,7 @@ func TestVariadicArguments(t *testing.T) { } func TestNothingToReturn(t *testing.T) { - m, err := New("testdata/example", "") + m, err := New("testpackages/example", "") if err != nil { t.Fatalf("moq.New: %s", err) } @@ -121,7 +121,7 @@ func TestNothingToReturn(t *testing.T) { } func TestChannelNames(t *testing.T) { - m, err := New("testdata/channels", "") + m, err := New("testpackages/channels", "") if err != nil { t.Fatalf("moq.New: %s", err) } @@ -142,7 +142,7 @@ func TestChannelNames(t *testing.T) { } func TestImports(t *testing.T) { - m, err := New("testdata/imports/two", "") + m, err := New("testpackages/imports/two", "") if err != nil { t.Fatalf("moq.New: %s", err) } @@ -154,7 +154,7 @@ func TestImports(t *testing.T) { s := buf.String() var strs = []string{ ` "sync"`, - ` "github.com/matryer/moq/pkg/moq/testdata/imports/one"`, + ` "github.com/matryer/moq/pkg/moq/testpackages/imports/one"`, } for _, str := range strs { if !strings.Contains(s, str) { @@ -174,7 +174,7 @@ func TestTemplateFuncs(t *testing.T) { } func TestVendoredPackages(t *testing.T) { - m, err := New("testdata/vendoring/user", "") + m, err := New("testpackages/vendoring/user", "") if err != nil { t.Fatalf("moq.New: %s", err) } diff --git a/pkg/moq/testdata/channels/example.go b/pkg/moq/testpackages/channels/example.go similarity index 100% rename from pkg/moq/testdata/channels/example.go rename to pkg/moq/testpackages/channels/example.go diff --git a/pkg/moq/testdata/example/example.go b/pkg/moq/testpackages/example/example.go similarity index 100% rename from pkg/moq/testdata/example/example.go rename to pkg/moq/testpackages/example/example.go diff --git a/pkg/moq/testdata/imports/one/one.go b/pkg/moq/testpackages/imports/one/one.go similarity index 100% rename from pkg/moq/testdata/imports/one/one.go rename to pkg/moq/testpackages/imports/one/one.go diff --git a/pkg/moq/testdata/imports/two/two.go b/pkg/moq/testpackages/imports/two/two.go similarity index 71% rename from pkg/moq/testdata/imports/two/two.go rename to pkg/moq/testpackages/imports/two/two.go index 1ebba15..d5929ee 100644 --- a/pkg/moq/testdata/imports/two/two.go +++ b/pkg/moq/testpackages/imports/two/two.go @@ -1,7 +1,7 @@ package two import ( - "github.com/matryer/moq/pkg/moq/testdata/imports/one" + "github.com/matryer/moq/pkg/moq/testpackages/imports/one" ) // DoSomething does something. diff --git a/pkg/moq/testdata/variadic/greeter.go b/pkg/moq/testpackages/variadic/greeter.go similarity index 100% rename from pkg/moq/testdata/variadic/greeter.go rename to pkg/moq/testpackages/variadic/greeter.go diff --git a/pkg/moq/testdata/vendoring/user/user.go b/pkg/moq/testpackages/vendoring/user/user.go similarity index 100% rename from pkg/moq/testdata/vendoring/user/user.go rename to pkg/moq/testpackages/vendoring/user/user.go diff --git a/pkg/moq/testdata/vendoring/user/vendor/github.com/matryer/somerepo/code.go b/pkg/moq/testpackages/vendoring/user/vendor/github.com/matryer/somerepo/code.go similarity index 100% rename from pkg/moq/testdata/vendoring/user/vendor/github.com/matryer/somerepo/code.go rename to pkg/moq/testpackages/vendoring/user/vendor/github.com/matryer/somerepo/code.go From 2abe0a029ae18cd42be7cfa83526e35cfb980377 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Tue, 11 Jul 2017 20:42:36 +0100 Subject: [PATCH 3/4] added code that checks vendor folders --- pkg/moq/importer.go | 55 ++++++++++++++++--- pkg/moq/moq.go | 2 +- pkg/moq/moq_test.go | 12 +++- .../github.com/matryer/somerepo/code.go | 0 4 files changed, 58 insertions(+), 11 deletions(-) rename pkg/moq/testpackages/vendoring/{user => }/vendor/github.com/matryer/somerepo/code.go (100%) diff --git a/pkg/moq/importer.go b/pkg/moq/importer.go index 66c6109..acf1225 100644 --- a/pkg/moq/importer.go +++ b/pkg/moq/importer.go @@ -18,6 +18,7 @@ import ( ) type customImporter struct { + source string imported map[string]*types.Package base types.Importer skipTestFiles bool @@ -43,8 +44,13 @@ func (i *customImporter) Import(path string) (*types.Package, error) { return pkg, nil } -func gopathDir(pkg string) (string, error) { - for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") { +func gopathDir(source, pkg string) (string, error) { + // check vendor directory + vendorPath, found := vendorPath(source, pkg) + if found { + return vendorPath, nil + } + for _, gopath := range gopaths() { absPath, err := filepath.Abs(path.Join(gopath, "src", pkg)) if err != nil { return "", err @@ -53,18 +59,50 @@ func gopathDir(pkg string) (string, error) { return absPath, nil } } - return "", fmt.Errorf("%s not in $GOPATH", pkg) + return "", fmt.Errorf("%s not in $GOPATH or %s", pkg, path.Join(source, "vendor")) +} + +func vendorPath(source, pkg string) (string, bool) { + for { + if isGopath(source) { + return "", false + } + vendorPath, err := filepath.Abs(path.Join(source, "vendor", pkg)) + if err != nil { + return "", false + } + if dir, err := os.Stat(vendorPath); err == nil && dir.IsDir() { + return vendorPath, true + } + source = filepath.Dir(source) + if source == "." { + return "", false + } + } } func removeGopath(p string) string { - for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") { + for _, gopath := range gopaths() { p = strings.Replace(p, path.Join(gopath, "src")+"/", "", 1) } return p } +func gopaths() []string { + return strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator)) +} + +func isGopath(path string) bool { + for _, p := range gopaths() { + if p == path { + return true + } + } + return false +} + func (i *customImporter) fsPkg(pkg string) (*types.Package, error) { - dir, err := gopathDir(pkg) + dir, err := gopathDir(i.source, pkg) if err != nil { return importOrErr(i.base, pkg, err) } @@ -118,8 +156,9 @@ func importOrErr(base types.Importer, pkg string, err error) (*types.Package, er } // newImporter returns an importer that will try to import code from gopath before using go/importer.Default and skipping test files -func newImporter() types.Importer { +func newImporter(source string) types.Importer { return &customImporter{ + source: source, imported: make(map[string]*types.Package), base: importer.Default(), skipTestFiles: true, @@ -138,8 +177,8 @@ func newImporter() types.Importer { // stripGopath teks the directory to a package and remove the gopath to get the // cannonical package name func stripGopath(p string) string { - for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") { - p = strings.Replace(p, path.Join(gopath, "src")+"/", "", 1) + for _, gopath := range gopaths() { + p = strings.TrimPrefix(p, path.Join(gopath, "src")+"/") } return p } diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index c2339ce..a006121 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -76,7 +76,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { files[i] = file i++ } - conf := types.Config{Importer: newImporter()} + conf := types.Config{Importer: newImporter(m.src)} tpkg, err := conf.Check(m.src, m.fset, files, nil) if err != nil { return err diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index bfdab08..d54d70b 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -2,7 +2,6 @@ package moq import ( "bytes" - "log" "strings" "testing" ) @@ -183,5 +182,14 @@ func TestVendoredPackages(t *testing.T) { if err != nil { t.Errorf("mock error: %s", err) } - log.Println(buf.String()) + 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) + } + } } diff --git a/pkg/moq/testpackages/vendoring/user/vendor/github.com/matryer/somerepo/code.go b/pkg/moq/testpackages/vendoring/vendor/github.com/matryer/somerepo/code.go similarity index 100% rename from pkg/moq/testpackages/vendoring/user/vendor/github.com/matryer/somerepo/code.go rename to pkg/moq/testpackages/vendoring/vendor/github.com/matryer/somerepo/code.go From 2b8f74f90c43d9bb1f70a86bb62680ac75ddc7e9 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Tue, 11 Jul 2017 20:45:51 +0100 Subject: [PATCH 4/4] better path parsing --- pkg/moq/importer.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/moq/importer.go b/pkg/moq/importer.go index acf1225..1445459 100644 --- a/pkg/moq/importer.go +++ b/pkg/moq/importer.go @@ -67,6 +67,11 @@ func vendorPath(source, pkg string) (string, bool) { if isGopath(source) { return "", false } + var err error + source, err = filepath.Abs(source) + if err != nil { + return "", false + } vendorPath, err := filepath.Abs(path.Join(source, "vendor", pkg)) if err != nil { return "", false @@ -75,9 +80,6 @@ func vendorPath(source, pkg string) (string, bool) { return vendorPath, true } source = filepath.Dir(source) - if source == "." { - return "", false - } } }