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:
parent
6cfb0558e1
commit
5e2b3bdd91
@ -3,9 +3,9 @@ language: go
|
|||||||
sudo: false
|
sudo: false
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.9.x
|
|
||||||
- 1.10.x
|
|
||||||
- 1.11.x
|
- 1.11.x
|
||||||
|
- 1.12.x
|
||||||
|
- 1.13.x
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
|
@ -73,22 +73,11 @@ type Mocker struct {
|
|||||||
func New(src, packageName string) (*Mocker, error) {
|
func New(src, packageName string) (*Mocker, error) {
|
||||||
srcPkg, err := pkgInfoFromPath(src, packages.LoadSyntax)
|
srcPkg, err := pkgInfoFromPath(src, packages.LoadSyntax)
|
||||||
if err != nil {
|
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
|
pkgPath, err := findPkgPath(packageName, srcPkg)
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Couldn't load mock package: %s", err)
|
return nil, fmt.Errorf("couldn't load mock package: %s", err)
|
||||||
}
|
|
||||||
pkgPath = mockPkg.PkgPath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := template.New("moq").Funcs(templateFuncs).Parse(moqTemplate)
|
tmpl, err := template.New("moq").Funcs(templateFuncs).Parse(moqTemplate)
|
||||||
@ -98,12 +87,44 @@ func New(src, packageName string) (*Mocker, error) {
|
|||||||
return &Mocker{
|
return &Mocker{
|
||||||
tmpl: tmpl,
|
tmpl: tmpl,
|
||||||
srcPkg: srcPkg,
|
srcPkg: srcPkg,
|
||||||
pkgName: packageName,
|
pkgName: preventZeroStr(packageName, srcPkg.Name),
|
||||||
pkgPath: pkgPath,
|
pkgPath: pkgPath,
|
||||||
imports: make(map[string]bool),
|
imports: make(map[string]bool),
|
||||||
}, nil
|
}, 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.
|
// Mock generates a mock for the specified interface name.
|
||||||
func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
||||||
if len(name) == 0 {
|
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 {
|
func (m *Mocker) packageQualifier(pkg *types.Package) string {
|
||||||
if m.pkgPath == pkg.Path() {
|
if m.pkgPath != "" && m.pkgPath == pkg.Path() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
path := pkg.Path()
|
path := pkg.Path()
|
||||||
|
@ -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
|
// TestVeradicArguments tests to ensure variadic work as
|
||||||
// expected.
|
// expected.
|
||||||
// see https://github.com/matryer/moq/issues/5
|
// see https://github.com/matryer/moq/issues/5
|
||||||
|
Loading…
Reference in New Issue
Block a user