[FIX] import package with same name misses package qualifier

This commit is contained in:
Frederik Vosberg 2018-07-11 08:23:28 +02:00
parent b60f573125
commit 80d25dbad5
3 changed files with 13 additions and 7 deletions

View File

@ -69,6 +69,7 @@ type Mocker struct {
fset *token.FileSet fset *token.FileSet
pkgs map[string]*ast.Package pkgs map[string]*ast.Package
pkgName string pkgName string
pkgPath string
imports map[string]bool imports map[string]bool
} }
@ -79,13 +80,17 @@ func New(src, packageName string) (*Mocker, error) {
noTestFiles := func(i os.FileInfo) bool { noTestFiles := func(i os.FileInfo) bool {
return !strings.HasSuffix(i.Name(), "_test.go") 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) pkgs, err := parser.ParseDir(fset, src, noTestFiles, parser.SpuriousErrors)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(packageName) == 0 { if len(packageName) == 0 {
for pkgName := range pkgs { for pkgName := range pkgs {
if strings.Contains(pkgName, "_test") { if strings.Contains(pkgName, "_test") {
continue continue
@ -97,6 +102,7 @@ func New(src, packageName string) (*Mocker, error) {
if len(packageName) == 0 { if len(packageName) == 0 {
return nil, errors.New("failed to determine package name") return nil, errors.New("failed to determine package name")
} }
tmpl, err := template.New("moq").Funcs(templateFuncs).Parse(moqTemplate) tmpl, err := template.New("moq").Funcs(templateFuncs).Parse(moqTemplate)
if err != nil { if err != nil {
return nil, err return nil, err
@ -107,6 +113,7 @@ func New(src, packageName string) (*Mocker, error) {
fset: fset, fset: fset,
pkgs: pkgs, pkgs: pkgs,
pkgName: packageName, pkgName: packageName,
pkgPath: packagePath,
imports: make(map[string]bool), imports: make(map[string]bool),
}, nil }, nil
} }
@ -117,7 +124,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
return errors.New("must specify one interface") return errors.New("must specify one interface")
} }
pkgInfo, err := m.pkgInfoFromPath(m.src) pkgInfo, err := pkgInfoFromPath(m.src)
if err != nil { if err != nil {
return err return err
} }
@ -180,7 +187,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
} }
func (m *Mocker) packageQualifier(pkg *types.Package) string { func (m *Mocker) packageQualifier(pkg *types.Package) string {
if m.pkgName == pkg.Name() { if m.pkgPath == pkg.Path() {
return "" return ""
} }
path := pkg.Path() path := pkg.Path()
@ -216,8 +223,7 @@ func (m *Mocker) extractArgs(sig *types.Signature, list *types.Tuple, nameFormat
return params return params
} }
func (*Mocker) pkgInfoFromPath(src string) (*loader.PackageInfo, error) { func pkgInfoFromPath(src string) (*loader.PackageInfo, error) {
abs, err := filepath.Abs(src) abs, err := filepath.Abs(src)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -223,7 +223,7 @@ func TestDotImports(t *testing.T) {
t.Errorf("mock error: %s", err) t.Errorf("mock error: %s", err)
} }
s := buf.String() s := buf.String()
if !strings.Contains(s, `/moq/pkg/moq/testpackages/dotimport"`) { if strings.Contains(s, `"."`) {
t.Error("contains invalid dot import") t.Error("contains invalid dot import")
} }
} }

View File

@ -1,6 +1,6 @@
package samename 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 // Example is used to test issues with packages, which import another package with the same name
type Example interface { type Example interface {