From 5e2b3bdd91dd299438c344bc2172ab1a75ff4a88 Mon Sep 17 00:00:00 2001 From: Pedram Hajesmaeeli Date: Sun, 3 Nov 2019 11:16:59 +0330 Subject: [PATCH] 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 --- .travis.yml | 4 ++-- pkg/moq/moq.go | 55 +++++++++++++++++++++++++++++++-------------- pkg/moq/moq_test.go | 19 ++++++++++++++++ 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index ebf53b3..6ea6fdd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index a29d6ab..f57880a 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -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) - if err != nil { - return nil, fmt.Errorf("Couldn't load mock package: %s", err) - } - pkgPath = mockPkg.PkgPath + pkgPath, err := findPkgPath(packageName, srcPkg) + if err != nil { + 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() diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index fcdc281..fe149e6 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -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