From fc06e5c36a6730a39bde12d3fbcbb5297538bff1 Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Tue, 10 Jul 2018 16:22:49 +0200 Subject: [PATCH 1/4] [TEST] check missing package name for packages with the same name --- pkg/moq/moq_test.go | 17 +++++++++++++++++ pkg/moq/testpackages/samenameimport/samename.go | 8 ++++++++ .../samenameimport/samenameimport/samename.go | 4 ++++ 3 files changed, 29 insertions(+) create mode 100644 pkg/moq/testpackages/samenameimport/samename.go create mode 100644 pkg/moq/testpackages/samenameimport/samenameimport/samename.go diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 18c33d6..2fb49ea 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -271,3 +271,20 @@ func TestGoGenerateVendoredPackages(t *testing.T) { t.Error("contains vendor directory in import path") } } + +func TestImportedPackageWithSameName(t *testing.T) { + m, err := New("testpackages/samenameimport", "") + if err != nil { + t.Fatalf("moq.New: %s", err) + } + var buf bytes.Buffer + err = m.Mock(&buf, "Example") + if err != nil { + t.Errorf("mock error: %s", err) + } + s := buf.String() + if !strings.Contains(s, `"samenameimport.A"`) { + t.Error("missing samenameimport.A to address the struct A from the external package samenameimport") + } + t.Logf("\n\n%s\n\n", s) +} diff --git a/pkg/moq/testpackages/samenameimport/samename.go b/pkg/moq/testpackages/samenameimport/samename.go new file mode 100644 index 0000000..e6bf454 --- /dev/null +++ b/pkg/moq/testpackages/samenameimport/samename.go @@ -0,0 +1,8 @@ +package samename + +import samename "github.com/matryer/moq/pkg/moq/testpackages/samenameimport/samenameimport" + +// Example is used to test issues with packages, which import another package with the same name +type Example interface { + Do(a samename.A) error +} diff --git a/pkg/moq/testpackages/samenameimport/samenameimport/samename.go b/pkg/moq/testpackages/samenameimport/samenameimport/samename.go new file mode 100644 index 0000000..19b3ba5 --- /dev/null +++ b/pkg/moq/testpackages/samenameimport/samenameimport/samename.go @@ -0,0 +1,4 @@ +package samename + +// The A is used in the parent package as a dependency +type A struct{} From b60f573125e2e7905516c40b10a8c617e00b4ac7 Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Tue, 10 Jul 2018 16:47:25 +0200 Subject: [PATCH 2/4] [FIX] tests assertion --- pkg/moq/moq_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 2fb49ea..8775592 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -283,8 +283,7 @@ func TestImportedPackageWithSameName(t *testing.T) { t.Errorf("mock error: %s", err) } s := buf.String() - if !strings.Contains(s, `"samenameimport.A"`) { - t.Error("missing samenameimport.A to address the struct A from the external package samenameimport") + if !strings.Contains(s, `a samename.A`) { + t.Error("missing samename.A to address the struct A from the external package samename") } - t.Logf("\n\n%s\n\n", s) } From 80d25dbad567c29ff4a29e5864070fb08aa31a3b Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Wed, 11 Jul 2018 08:23:28 +0200 Subject: [PATCH 3/4] [FIX] import package with same name misses package qualifier --- pkg/moq/moq.go | 16 +++++++++++----- pkg/moq/moq_test.go | 2 +- pkg/moq/testpackages/samenameimport/samename.go | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index 952bce1..d2ad06e 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -69,6 +69,7 @@ type Mocker struct { fset *token.FileSet pkgs map[string]*ast.Package pkgName string + pkgPath string imports map[string]bool } @@ -79,13 +80,17 @@ func New(src, packageName string) (*Mocker, error) { noTestFiles := func(i os.FileInfo) bool { return !strings.HasSuffix(i.Name(), "_test.go") } + wd, err := os.Getwd() + if err != nil { + return nil, fmt.Errorf("failed to determin current working directory: %s", err) + } + packagePath := stripGopath(filepath.Join(wd, src, packageName)) pkgs, err := parser.ParseDir(fset, src, noTestFiles, parser.SpuriousErrors) if err != nil { return nil, err } if len(packageName) == 0 { - for pkgName := range pkgs { if strings.Contains(pkgName, "_test") { continue @@ -97,6 +102,7 @@ func New(src, packageName string) (*Mocker, error) { if len(packageName) == 0 { return nil, errors.New("failed to determine package name") } + tmpl, err := template.New("moq").Funcs(templateFuncs).Parse(moqTemplate) if err != nil { return nil, err @@ -107,6 +113,7 @@ func New(src, packageName string) (*Mocker, error) { fset: fset, pkgs: pkgs, pkgName: packageName, + pkgPath: packagePath, imports: make(map[string]bool), }, nil } @@ -117,7 +124,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { return errors.New("must specify one interface") } - pkgInfo, err := m.pkgInfoFromPath(m.src) + pkgInfo, err := pkgInfoFromPath(m.src) if err != nil { return err } @@ -180,7 +187,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { } func (m *Mocker) packageQualifier(pkg *types.Package) string { - if m.pkgName == pkg.Name() { + if m.pkgPath == pkg.Path() { return "" } path := pkg.Path() @@ -216,8 +223,7 @@ func (m *Mocker) extractArgs(sig *types.Signature, list *types.Tuple, nameFormat return params } -func (*Mocker) pkgInfoFromPath(src string) (*loader.PackageInfo, error) { - +func pkgInfoFromPath(src string) (*loader.PackageInfo, error) { abs, err := filepath.Abs(src) if err != nil { return nil, err diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 8775592..4322869 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -223,7 +223,7 @@ func TestDotImports(t *testing.T) { t.Errorf("mock error: %s", err) } s := buf.String() - if !strings.Contains(s, `/moq/pkg/moq/testpackages/dotimport"`) { + if strings.Contains(s, `"."`) { t.Error("contains invalid dot import") } } diff --git a/pkg/moq/testpackages/samenameimport/samename.go b/pkg/moq/testpackages/samenameimport/samename.go index e6bf454..e39f8f2 100644 --- a/pkg/moq/testpackages/samenameimport/samename.go +++ b/pkg/moq/testpackages/samenameimport/samename.go @@ -1,6 +1,6 @@ package samename -import samename "github.com/matryer/moq/pkg/moq/testpackages/samenameimport/samenameimport" +import samename "github.com/fvosberg/moq/pkg/moq/testpackages/samenameimport/samenameimport" // Example is used to test issues with packages, which import another package with the same name type Example interface { From c3b38fdc87402c46ac0d29b8696b75cd19ffa32d Mon Sep 17 00:00:00 2001 From: Frederik Vosberg Date: Wed, 11 Jul 2018 08:29:15 +0200 Subject: [PATCH 4/4] [FIX] import path of test package --- pkg/moq/testpackages/samenameimport/samename.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/moq/testpackages/samenameimport/samename.go b/pkg/moq/testpackages/samenameimport/samename.go index e39f8f2..e6bf454 100644 --- a/pkg/moq/testpackages/samenameimport/samename.go +++ b/pkg/moq/testpackages/samenameimport/samename.go @@ -1,6 +1,6 @@ package samename -import samename "github.com/fvosberg/moq/pkg/moq/testpackages/samenameimport/samenameimport" +import samename "github.com/matryer/moq/pkg/moq/testpackages/samenameimport/samenameimport" // Example is used to test issues with packages, which import another package with the same name type Example interface {