Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to export the mock struct #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions mocktail.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"path"
"path/filepath"
"strings"

"github.com/ettle/strcase"
)

const (
Expand Down Expand Up @@ -47,8 +49,9 @@ func main() {
log.Fatal("get module path", err)
}

var exported bool
var exported, exportMockTypes bool
flag.BoolVar(&exported, "e", false, "generate exported mocks")
flag.BoolVar(&exportMockTypes, "t", false, "generate exported mock types")
flag.Parse()

root := info.Dir
Expand All @@ -67,7 +70,7 @@ func main() {
return
}

err = generate(model, exported)
err = generate(model, exported, exportMockTypes)
if err != nil {
log.Fatalf("generate: %v", err)
}
Expand Down Expand Up @@ -242,7 +245,7 @@ func getTypeImports(t types.Type) []string {
}
}

func generate(model map[string]PackageDesc, exported bool) error {
func generate(model map[string]PackageDesc, exported bool, exportMockTypes bool) error {
for fp, pkgDesc := range model {
buffer := bytes.NewBufferString("")

Expand All @@ -252,7 +255,12 @@ func generate(model map[string]PackageDesc, exported bool) error {
}

for _, interfaceDesc := range pkgDesc.Interfaces {
err = writeMockBase(buffer, interfaceDesc.Name, exported)
interfaceName := strcase.ToGoCamel(interfaceDesc.Name)
if exportMockTypes {
interfaceName = strcase.ToGoPascal(interfaceDesc.Name)
}

err = writeMockBase(buffer, interfaceDesc.Name, interfaceName, exported)
if err != nil {
return err
}
Expand All @@ -263,10 +271,11 @@ func generate(model map[string]PackageDesc, exported bool) error {
signature := method.Type().(*types.Signature)

syrup := Syrup{
PkgPath: pkgDesc.Pkg.Path(),
InterfaceName: interfaceDesc.Name,
Method: method,
Signature: signature,
PkgPath: pkgDesc.Pkg.Path(),
OriginalInterfaceName: interfaceDesc.Name,
Method: method,
Signature: signature,
InterfaceName: interfaceName,
}

err = syrup.MockMethod(buffer)
Expand Down
59 changes: 59 additions & 0 deletions mocktail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,62 @@ func TestMocktail_exported(t *testing.T) {
require.NoError(t, err)
}
}

func TestMocktail_exported_types(t *testing.T) {
const testRoot = "./testdata/exported_types"

if runtime.GOOS == "windows" {
t.Skip(runtime.GOOS)
}

dir, errR := os.ReadDir(testRoot)
require.NoError(t, errR)

for _, entry := range dir {
if !entry.IsDir() {
continue
}

t.Setenv("MOCKTAIL_TEST_PATH", filepath.Join(testRoot, entry.Name()))

output, err := exec.Command("go", "run", ".", "-e", "-t").CombinedOutput()
t.Log(string(output))

require.NoError(t, err)
}

errW := filepath.WalkDir(testRoot, func(path string, d fs.DirEntry, errW error) error {
if errW != nil {
return errW
}

if d.IsDir() || d.Name() != outputMockFile {
return nil
}

genBytes, err := os.ReadFile(path)
require.NoError(t, err)

goldenBytes, err := os.ReadFile(path + ".golden")
require.NoError(t, err)

assert.Equal(t, string(goldenBytes), string(genBytes))

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)
}
}
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ mocktail -e

In this case, mock will be created in the same package but in the file `mock_gen.go`.

If you need to exported mock types add flag `-t`:

```shell
mocktail -t
```

<!--

Replacement pattern:
Expand Down
Loading