Merge pull request #62 from fvosberg/fix/samenameimport
[TEST] check missing package name for packages with the same name
This commit is contained in:
commit
fbe042a544
@ -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
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
@ -271,3 +271,19 @@ 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, `a samename.A`) {
|
||||
t.Error("missing samename.A to address the struct A from the external package samename")
|
||||
}
|
||||
}
|
||||
|
8
pkg/moq/testpackages/samenameimport/samename.go
Normal file
8
pkg/moq/testpackages/samenameimport/samename.go
Normal file
@ -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
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package samename
|
||||
|
||||
// The A is used in the parent package as a dependency
|
||||
type A struct{}
|
Loading…
Reference in New Issue
Block a user