Single dot imports explicitly not-included - fixes #21

This commit is contained in:
Mat Ryer 2017-07-18 09:45:29 +01:00
parent 1b59660688
commit 879118f880
4 changed files with 49 additions and 0 deletions

View File

@ -120,6 +120,9 @@ func (m *Mocker) packageQualifier(pkg *types.Package) string {
if m.pkgName == pkg.Name() { if m.pkgName == pkg.Name() {
return "" return ""
} }
if pkg.Path() == "." {
return ""
}
m.imports[pkg.Path()] = true m.imports[pkg.Path()] = true
return pkg.Name() return pkg.Name()
} }

View File

@ -2,6 +2,7 @@ package moq
import ( import (
"bytes" "bytes"
"os"
"strings" "strings"
"testing" "testing"
) )
@ -192,3 +193,24 @@ func TestVendoredPackages(t *testing.T) {
} }
} }
} }
// TestDotImports tests for https://github.com/matryer/moq/issues/21.
func TestDotImports(t *testing.T) {
err := os.Chdir("testpackages/issue-21")
if err != nil {
t.Errorf("Chdir: %s", err)
}
m, err := New(".", "moqtest_test")
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)
}
s := buf.String()
if strings.Contains(s, `"."`) {
t.Error("contains invalid dot import")
}
}

View File

@ -0,0 +1,14 @@
// Package moqtest is just a package to try getting started with moq without the overhead of a real package
package moqtest
//go:generate moq -out service_moq_test.go -pkg moqtest_test . Service
// Service is the interface which should be mocked by moq
type Service interface {
User(ID string) (User, error)
}
// User is just a struct for testing
type User struct {
Name string
}

View File

@ -0,0 +1,10 @@
package moqtest_test
import "testing"
func TestServiceMock(t *testing.T) {
// Here I want to use the ServiceMock
service := ServiceMock{}
}