-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a_moq_test.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters