Fix moq for go 1.13 and avoid creating empty directory (#105)

* Fix moq for go 1.13 and avoid creating empty directory

This fixes #103 and also fixes #102

* Update .travis.yml
This commit is contained in:
Pedram Hajesmaeeli 2019-11-03 11:16:59 +03:30 committed by Mat Ryer
parent 6cfb0558e1
commit 5e2b3bdd91
3 changed files with 59 additions and 19 deletions

View File

@ -3,9 +3,9 @@ language: go
sudo: false
go:
- 1.9.x
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- tip
before_install:

View File

@ -73,22 +73,11 @@ type Mocker struct {
func New(src, packageName string) (*Mocker, error) {
srcPkg, err := pkgInfoFromPath(src, packages.LoadSyntax)
if err != nil {
return nil, fmt.Errorf("Couldn't load source package: %s", err)
return nil, fmt.Errorf("couldn't load source package: %s", err)
}
pkgPath := srcPkg.PkgPath
if len(packageName) == 0 {
packageName = srcPkg.Name
} else {
mockPkgPath := filepath.Join(src, packageName)
if _, err := os.Stat(mockPkgPath); os.IsNotExist(err) {
os.Mkdir(mockPkgPath, os.ModePerm)
}
mockPkg, err := pkgInfoFromPath(mockPkgPath, packages.LoadFiles)
pkgPath, err := findPkgPath(packageName, srcPkg)
if err != nil {
return nil, fmt.Errorf("Couldn't load mock package: %s", err)
}
pkgPath = mockPkg.PkgPath
return nil, fmt.Errorf("couldn't load mock package: %s", err)
}
tmpl, err := template.New("moq").Funcs(templateFuncs).Parse(moqTemplate)
@ -98,12 +87,44 @@ func New(src, packageName string) (*Mocker, error) {
return &Mocker{
tmpl: tmpl,
srcPkg: srcPkg,
pkgName: packageName,
pkgName: preventZeroStr(packageName, srcPkg.Name),
pkgPath: pkgPath,
imports: make(map[string]bool),
}, nil
}
func preventZeroStr(val, defaultVal string) string {
if val == "" {
return defaultVal
}
return val
}
func findPkgPath(pkgInputVal string, srcPkg *packages.Package) (string, error) {
if pkgInputVal == "" {
return srcPkg.PkgPath, nil
}
if pkgInDir(".", pkgInputVal) {
return ".", nil
}
if pkgInDir(srcPkg.PkgPath, pkgInputVal) {
return srcPkg.PkgPath, nil
}
subdirectoryPath := filepath.Join(srcPkg.PkgPath, pkgInputVal)
if pkgInDir(subdirectoryPath, pkgInputVal) {
return subdirectoryPath, nil
}
return "", nil
}
func pkgInDir(pkgName, dir string) bool {
currentPkg, err := pkgInfoFromPath(dir, packages.LoadFiles)
if err != nil {
return false
}
return currentPkg.Name == pkgName || currentPkg.Name+"_test" == pkgName
}
// Mock generates a mock for the specified interface name.
func (m *Mocker) Mock(w io.Writer, name ...string) error {
if len(name) == 0 {
@ -173,7 +194,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
}
func (m *Mocker) packageQualifier(pkg *types.Package) string {
if m.pkgPath == pkg.Path() {
if m.pkgPath != "" && m.pkgPath == pkg.Path() {
return ""
}
path := pkg.Path()

View File

@ -131,6 +131,25 @@ func TestMoqExplicitPackageWithStaticCheck(t *testing.T) {
}
}
func TestNotCreatingEmptyDirWhenPkgIsGiven(t *testing.T) {
m, err := New("testpackages/example", "different")
if err != nil {
t.Fatalf("moq.New: %s", err)
}
var buf bytes.Buffer
err = m.Mock(&buf, "PersonStore")
if err != nil {
t.Errorf("m.Mock: %s", err)
}
s := buf.String()
if len(s) == 0 {
t.Fatalf("mock should be generated")
}
if _, err := os.Stat("testpackages/example/different"); !os.IsNotExist(err) {
t.Fatalf("no empty dir should be created by moq")
}
}
// TestVeradicArguments tests to ensure variadic work as
// expected.
// see https://github.com/matryer/moq/issues/5