Skip to content

Commit

Permalink
feat: add support for moq
Browse files Browse the repository at this point in the history
  • Loading branch information
oNaiPs committed Oct 13, 2023
1 parent 3b115f1 commit 9af7d8e
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ following tools:
- [gqlgen](https://gqlgen.com/): Go code generation for GraphQL APIs
- [mockgen](https://github.com/uber-go/mock): Mock class generation for
interfaces
- [moq](https://github.com/matryer/moq): Interface mocking tool for go generate
- [esc](https://github.com/mjibson/esc): Embedding static files in Go binaries
- [go-bindata](https://github.com/go-bindata/go-bindata): Turn data file into go
code.
Expand Down
1 change: 1 addition & 0 deletions e2e/generated_hashes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
237 f0c4b165baa306042455028d7badad99 *gqlgen/graph/model/models_gen.go
1208 38c7e8eba5087fa9e0b1a38d723869ac *mockgen/a.mock.go
1269 f5217ea75d9d28aaf4ac4bc65cb8d2ad *mockgen/b.mock.go
2647 a180d391bef1807af3b457dddd0885a3 *moq/a_moq_test.go
4117 6bfc15a24e5ca32e58de5171563cf1ac *protobuf/testdata/example/gender.pb.go
5266 4a0784ead1d20bf1778973d202f9238b *protobuf/testdata/person/person.pb.go
572 42e0312c59e136c58ec934981c5ce7f5 *stringer/a_string.go
Expand Down
1 change: 1 addition & 0 deletions e2e/moq/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a_moq_test.go
8 changes: 8 additions & 0 deletions e2e/moq/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package moq

//go:generate moq -out a_moq_test.go . MyInterface

type MyInterface interface {
Method1() error
Method2(i int)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/go-bindata/go-bindata v3.1.2+incompatible
github.com/golangci/golangci-lint v1.54.2
github.com/jessevdk/go-flags v1.5.0
github.com/matryer/moq v0.2.7
github.com/mjibson/esc v0.2.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 h1:gWg6ZQ4JhDfJPqlo2
github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/matryer/moq v0.2.7 h1:RtpiPUM8L7ZSCbSwK+QcZH/E9tgqAkFjKQxsRs25b4w=
github.com/matryer/moq v0.2.7/go.mod h1:kITsx543GOENm48TUAQyJ9+SAvFSr7iGQXPoth/VUBk=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
1 change: 1 addition & 0 deletions src/plugin_factory/plugin_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "github.com/oNaiPs/go-generate-fast/src/plugins/go-bindata"
_ "github.com/oNaiPs/go-generate-fast/src/plugins/gqlgen"
_ "github.com/oNaiPs/go-generate-fast/src/plugins/mockgen"
_ "github.com/oNaiPs/go-generate-fast/src/plugins/moq"
_ "github.com/oNaiPs/go-generate-fast/src/plugins/protoc"
_ "github.com/oNaiPs/go-generate-fast/src/plugins/stringer"
)
Expand Down
87 changes: 87 additions & 0 deletions src/plugins/moq/moq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package plugin_moq

import (
"flag"

"github.com/oNaiPs/go-generate-fast/src/plugins"
"github.com/oNaiPs/go-generate-fast/src/utils/pkg"
"go.uber.org/zap"
)

type MoqPlugin struct {
plugins.Plugin
}

func (p *MoqPlugin) Name() string {
return "moq"
}

func (p *MoqPlugin) Matches(opts plugins.GenerateOpts) bool {
return opts.ExecutableName == "moq" ||
opts.GoPackage == "github.com/matryer/moq"
}

type userFlags struct {
outFile string
pkgName string
formatter string
stubImpl bool
skipEnsure bool
withResets bool
remove bool
args []string
}

func (p *MoqPlugin) ComputeInputOutputFiles(opts plugins.GenerateOpts) *plugins.InputOutputFiles {
flag := flag.NewFlagSet("mockgen", flag.ContinueOnError)

var flags userFlags
flag.StringVar(&flags.outFile, "out", "", "output file (default stdout)")
flag.StringVar(&flags.pkgName, "pkg", "", "package name (default will infer)")
flag.StringVar(&flags.formatter, "fmt", "", "go pretty-printer: gofmt, goimports or noop (default gofmt)")
flag.BoolVar(&flags.stubImpl, "stub", false,
"return zero values when no mock implementation is provided, do not panic")
printVersion := flag.Bool("version", false, "show the version for moq")
flag.BoolVar(&flags.skipEnsure, "skip-ensure", false,
"suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package")
flag.BoolVar(&flags.remove, "rm", false, "first remove output file, if it exists")
flag.BoolVar(&flags.withResets, "with-resets", false,
"generate functions to facilitate resetting calls made to a mock")

err := flag.Parse(opts.SanitizedArgs)
if err != nil {
zap.S().Debug("cannot parse args")
return nil
}
flags.args = flag.Args()

if *printVersion {
return nil
}

if flags.outFile == "" {
zap.S().Error("unsupported stdout mode")
return nil
}

ioFiles := plugins.InputOutputFiles{}

pkg := pkg.LoadPackages(flags.args[0], []string{}, []string{})
if pkg == nil {
//did not find package matches
return nil
} else if len(pkg.Errors) > 0 {
zap.S().Debug("Got errors parsing packages: %s", pkg.Errors)
return nil
}

ioFiles.InputFiles = append(ioFiles.InputFiles, pkg.CompiledGoFiles...)

ioFiles.OutputFiles = append(ioFiles.OutputFiles, flags.outFile)

return &ioFiles
}

func init() {
plugins.RegisterPlugin(&MoqPlugin{})
}
57 changes: 57 additions & 0 deletions src/plugins/moq/moq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package plugin_moq

import (
"os"
"path"
"testing"

"github.com/oNaiPs/go-generate-fast/src/plugins"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMoqPlugin_Name(t *testing.T) {
p := &MoqPlugin{}
assert.Equal(t, "moq", p.Name())
}

func TestMoqPlugin_Matches(t *testing.T) {
p := &MoqPlugin{}
assert.True(t, p.Matches(plugins.GenerateOpts{
ExecutableName: "moq",
}))
}

func TestMoqPlugin_ComputeInputOutputFiles(t *testing.T) {
p := &MoqPlugin{}
tempDir := t.TempDir()

err := os.WriteFile(path.Join(tempDir, "go.mod"), []byte("module example.com/mod"), 0644)
assert.NoError(t, err)
err = os.WriteFile(path.Join(tempDir, "input_file1.go"), []byte(`
package example
type Foo interface {
Bar(x int) int
}
`), 0644)
assert.NoError(t, err)

opts := plugins.GenerateOpts{
Path: path.Join(tempDir, "test.go"),
ExecutableName: "moq",
SanitizedArgs: []string{
"-out", "output_file.go",
"-pkg", "example",
".",
"Foo",
},
}

_ = os.Chdir(tempDir)
ioFiles := p.ComputeInputOutputFiles(opts)
require.NotNil(t, ioFiles)
assert.Equal(t, []string{
path.Join(tempDir, "input_file1.go"),
}, ioFiles.InputFiles)
assert.Equal(t, []string{"output_file.go"}, ioFiles.OutputFiles)
}
11 changes: 6 additions & 5 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
package main

import (
_ "gotest.tools/gotestsum"
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/99designs/gqlgen"
_ "github.com/cheekybits/genny"
_ "go.uber.org/mock/mockgen"
_ "github.com/go-bindata/go-bindata"
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/matryer/moq"
_ "github.com/mjibson/esc"
_ "github.com/99designs/gqlgen"
_ "go.uber.org/mock/mockgen"
_ "golang.org/x/tools/cmd/stringer"
)
_ "gotest.tools/gotestsum"
)

0 comments on commit 9af7d8e

Please sign in to comment.