From d4f0d83ec59e6c7aabc549ecfa28b3fff4470ca9 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 24 Jun 2022 03:46:08 +0200 Subject: [PATCH] test: improve tests --- mocktail.go | 12 +++++------- mocktail_test.go | 27 ++++++++++++++++++++------ syrup.go | 2 +- testdata/src/a/b/b.go | 6 ++++-- testdata/src/a/c/c.go | 5 +++++ testdata/src/a/mock_gen_test.go | 11 ++++++----- testdata/src/a/mock_gen_test.go.golden | 11 ++++++----- 7 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 testdata/src/a/c/c.go diff --git a/mocktail.go b/mocktail.go index b560fe0..a3b5f39 100644 --- a/mocktail.go +++ b/mocktail.go @@ -27,8 +27,7 @@ const commentTagPattern = "// mocktail:" // PackageDesc represent a package. type PackageDesc struct { - PkgName string - PkgPath string + Pkg *types.Package Imports map[string]struct{} Interfaces []InterfaceDesc } @@ -136,9 +135,8 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) { continue } - if packageDesc.PkgName == "" { - packageDesc.PkgName = lookup.Pkg().Name() - packageDesc.PkgPath = lookup.Pkg().Path() + if packageDesc.Pkg == nil { + packageDesc.Pkg = lookup.Pkg() } interfaceDesc := InterfaceDesc{Name: interfaceName} @@ -150,7 +148,7 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) { interfaceDesc.Methods = append(interfaceDesc.Methods, method) - for _, imp := range getMethodImports(method, packageDesc.PkgPath) { + for _, imp := range getMethodImports(method, packageDesc.Pkg.Name()) { packageDesc.Imports[imp] = struct{}{} } } @@ -252,7 +250,7 @@ func generate(model map[string]PackageDesc) error { signature := method.Type().(*types.Signature) syrup := Syrup{ - PkgPath: pkgDesc.PkgPath, + PkgPath: pkgDesc.Pkg.Path(), InterfaceName: interfaceDesc.Name, Method: method, Signature: signature, diff --git a/mocktail_test.go b/mocktail_test.go index 842d024..341d605 100644 --- a/mocktail_test.go +++ b/mocktail_test.go @@ -15,15 +15,15 @@ import ( "github.com/stretchr/testify/require" ) +const testRoot = "./testdata/src" + func TestMocktail(t *testing.T) { if runtime.GOOS == "windows" { t.Skip(runtime.GOOS) } - testRoot := "./testdata/src" - - dir, err := os.ReadDir(testRoot) - require.NoError(t, err) + dir, errR := os.ReadDir(testRoot) + require.NoError(t, errR) for _, entry := range dir { if !entry.IsDir() { @@ -38,7 +38,7 @@ func TestMocktail(t *testing.T) { require.NoError(t, err) } - require.NoError(t, filepath.WalkDir(testRoot, func(path string, d fs.DirEntry, errW error) error { + errW := filepath.WalkDir(testRoot, func(path string, d fs.DirEntry, errW error) error { if errW != nil { return errW } @@ -61,5 +61,20 @@ func TestMocktail(t *testing.T) { } return nil - })) + }) + require.NoError(t, errW) + + for _, entry := range dir { + if !entry.IsDir() { + continue + } + + cmd := exec.Command("go", "test", "-v") + cmd.Dir = filepath.Join(testRoot, entry.Name()) + + output, err := cmd.CombinedOutput() + t.Log(string(output)) + + require.NoError(t, err) + } } diff --git a/syrup.go b/syrup.go index 8036bc0..dbb904e 100644 --- a/syrup.go +++ b/syrup.go @@ -660,7 +660,7 @@ func writeImports(writer io.Writer, descPkg PackageDesc) error { } data := map[string]interface{}{ - "Name": descPkg.PkgName, + "Name": descPkg.Pkg.Name(), "Imports": quickGoImports(descPkg), } return tmpl.Execute(writer, data) diff --git a/testdata/src/a/b/b.go b/testdata/src/a/b/b.go index afcdf1a..1181d07 100644 --- a/testdata/src/a/b/b.go +++ b/testdata/src/a/b/b.go @@ -1,10 +1,12 @@ package b -import "a" +import ( + "a/c" +) type Carrot interface { Bar(string) *Potato - Bur(string) *a.Coconut + Bur(string) *c.Cherry } type Potato struct { diff --git a/testdata/src/a/c/c.go b/testdata/src/a/c/c.go new file mode 100644 index 0000000..e4fed55 --- /dev/null +++ b/testdata/src/a/c/c.go @@ -0,0 +1,5 @@ +package c + +type Cherry struct { + Name string +} diff --git a/testdata/src/a/mock_gen_test.go b/testdata/src/a/mock_gen_test.go index 23b8966..23ee95b 100644 --- a/testdata/src/a/mock_gen_test.go +++ b/testdata/src/a/mock_gen_test.go @@ -4,6 +4,7 @@ package a import ( "a/b" + "a/c" "bytes" "context" "testing" @@ -3019,14 +3020,14 @@ func (_c *carrotBarCall) OnBurRaw(aParam interface{}) *carrotBurCall { return _c.Parent.OnBurRaw(aParam) } -func (_m *carrotMock) Bur(aParam string) *Coconut { +func (_m *carrotMock) Bur(aParam string) *c.Cherry { _ret := _m.Called(aParam) - if _rf, ok := _ret.Get(0).(func(string) *Coconut); ok { + if _rf, ok := _ret.Get(0).(func(string) *c.Cherry); ok { return _rf(aParam) } - _ra0, _ := _ret.Get(0).(*Coconut) + _ra0, _ := _ret.Get(0).(*c.Cherry) return _ra0 } @@ -3084,12 +3085,12 @@ func (_c *carrotBurCall) Maybe() *carrotBurCall { return _c } -func (_c *carrotBurCall) TypedReturns(a *Coconut) *carrotBurCall { +func (_c *carrotBurCall) TypedReturns(a *c.Cherry) *carrotBurCall { _c.Call = _c.Return(a) return _c } -func (_c *carrotBurCall) ReturnsFn(fn func(string) *Coconut) *carrotBurCall { +func (_c *carrotBurCall) ReturnsFn(fn func(string) *c.Cherry) *carrotBurCall { _c.Call = _c.Return(fn) return _c } diff --git a/testdata/src/a/mock_gen_test.go.golden b/testdata/src/a/mock_gen_test.go.golden index 23b8966..23ee95b 100644 --- a/testdata/src/a/mock_gen_test.go.golden +++ b/testdata/src/a/mock_gen_test.go.golden @@ -4,6 +4,7 @@ package a import ( "a/b" + "a/c" "bytes" "context" "testing" @@ -3019,14 +3020,14 @@ func (_c *carrotBarCall) OnBurRaw(aParam interface{}) *carrotBurCall { return _c.Parent.OnBurRaw(aParam) } -func (_m *carrotMock) Bur(aParam string) *Coconut { +func (_m *carrotMock) Bur(aParam string) *c.Cherry { _ret := _m.Called(aParam) - if _rf, ok := _ret.Get(0).(func(string) *Coconut); ok { + if _rf, ok := _ret.Get(0).(func(string) *c.Cherry); ok { return _rf(aParam) } - _ra0, _ := _ret.Get(0).(*Coconut) + _ra0, _ := _ret.Get(0).(*c.Cherry) return _ra0 } @@ -3084,12 +3085,12 @@ func (_c *carrotBurCall) Maybe() *carrotBurCall { return _c } -func (_c *carrotBurCall) TypedReturns(a *Coconut) *carrotBurCall { +func (_c *carrotBurCall) TypedReturns(a *c.Cherry) *carrotBurCall { _c.Call = _c.Return(a) return _c } -func (_c *carrotBurCall) ReturnsFn(fn func(string) *Coconut) *carrotBurCall { +func (_c *carrotBurCall) ReturnsFn(fn func(string) *c.Cherry) *carrotBurCall { _c.Call = _c.Return(fn) return _c }