From d4c2d21252aaa47bd07198b43f0db62871398868 Mon Sep 17 00:00:00 2001 From: sleygin Date: Fri, 18 Oct 2024 00:19:17 +0300 Subject: [PATCH 1/2] testing go run behaviour --- cmd/minimock/minimock.go | 51 ++++++++++++++++++++++++++++------ go.mod | 8 +++--- go.sum | 18 ++++++------ internal/templates/header.tmpl | 4 +++ 4 files changed, 61 insertions(+), 20 deletions(-) diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index 801ab0e..b7cfe7b 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -8,6 +8,7 @@ import ( "go/token" "io" "io/ioutil" + "log" "os" "path/filepath" "runtime/debug" @@ -23,7 +24,11 @@ import ( "golang.org/x/tools/go/packages" ) -const devVersion = "dev" +const ( + devVersion = "dev" + runMode = "go-run" + directMode = "direct" +) var ( //do not modify the following vars @@ -51,11 +56,12 @@ var helpers = template.FuncMap{ type ( options struct { - interfaces []interfaceInfo - noGenerate bool - suffix string - mockNames []string - packageNames []string + interfaces []interfaceInfo + noGenerate bool + suffix string + mockNames []string + packageNames []string + goGenerateMode string } interfaceInfo struct { @@ -109,7 +115,7 @@ func getVersion(version string, buildInfo *debug.BuildInfo) string { } func main() { - opts, err := processArgs(os.Args[1:], os.Stdout, os.Stderr) + opts, err := processArgs(os.Args[0], os.Args[1:], os.Stdout, os.Stderr) if err != nil { if err == errInvalidArguments { os.Exit(2) @@ -162,6 +168,7 @@ func run(opts *options) (err error) { "GenerateInstruction": !opts.noGenerate, "Version": version, "PackageName": packageName, + "GoGenerateMode": opts.goGenerateMode, }, Vars: map[string]interface{}{}, Funcs: helpers, @@ -374,9 +381,27 @@ func processNames(names string, interfacesNum int, isInterfaceWildeCarded bool) return namesSplitted, nil } +func processGoGenerateMode(cmd string, args []string, goGenerateMode string) (string, error) { + log.Println(cmd) + log.Println(args) + log.Println(goGenerateMode) + if goGenerateMode != "" && goGenerateMode != directMode && goGenerateMode != runMode { + return "", fmt.Errorf("wrong mode value") + } + + if goGenerateMode != "" { + return goGenerateMode, nil + } + if cmd == "go" && len(args) > 0 && args[0] == "run" { + return runMode, nil + } + + return directMode, nil +} + var errInvalidArguments = errors.New("invalid arguments") -func processArgs(args []string, stdout, stderr io.Writer) (*options, error) { +func processArgs(cmd string, args []string, stdout, stderr io.Writer) (*options, error) { var opts options fs := flag.NewFlagSet("", flag.ContinueOnError) @@ -390,6 +415,10 @@ func processArgs(args []string, stdout, stderr io.Writer) (*options, error) { packageNames := fs.String("p", "", "comma-separated package names,\nby default the generated package names are taken from the destination directory names") help := fs.Bool("h", false, "show this help message") version := fs.Bool("version", false, "display version information and exit") + goGenerateMode := fs.String("go-generate-mode", "", `changes go:generate command mode: direct/go-run. +By default puts "go:generate minimock args..." instruction into generated code if mock was generated with minimock command. +If mock was generated with go run github.com/gojuno/minimock/v3/cmd/minimock then it puts "go:generate go run github.com/gojuno/minimock/v3/cmd/minimock args...". +This behaviour can be rewritten by explicitly specifying mode with flag.`) fs.Usage = func() { usage(fs, stderr) } @@ -407,6 +436,12 @@ func processArgs(args []string, stdout, stderr io.Writer) (*options, error) { return nil, nil } + cmdMode, err := processGoGenerateMode(cmd, args, *goGenerateMode) + if err != nil { + return nil, fmt.Errorf("processing -go-generate-mode flag arguments: %w", err) + } + opts.goGenerateMode = cmdMode + interfaces := strings.Split(*input, ",") interfacesLen := len(interfaces) isWildecarded := strings.Contains(*input, "*") diff --git a/go.mod b/go.mod index 0e6985b..0078d61 100644 --- a/go.mod +++ b/go.mod @@ -6,15 +6,15 @@ require ( github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 github.com/stretchr/testify v1.8.4 - golang.org/x/tools v0.1.12 + golang.org/x/tools v0.6.0 google.golang.org/protobuf v1.30.0 ) require ( github.com/kr/text v0.2.0 // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/sys v0.5.0 // indirect - golang.org/x/text v0.3.8 // indirect + golang.org/x/mod v0.8.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index ebe1077..4bc120e 100644 --- a/go.sum +++ b/go.sum @@ -21,14 +21,16 @@ github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XF github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U= golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/templates/header.tmpl b/internal/templates/header.tmpl index 4f265a8..734da1e 100644 --- a/internal/templates/header.tmpl +++ b/internal/templates/header.tmpl @@ -8,7 +8,11 @@ package {{$packageName}} {{if $.Options.HeaderVars.GenerateInstruction}} + {{if eq $.Options.HeaderVars.GoGenerateMode "go-run"}} +//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} + {{else}} //go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} + {{end}} {{end}} import ( From 42e3e464cd91de1cc2cfc9f039991f8b5bf228ac Mon Sep 17 00:00:00 2001 From: sleygin Date: Fri, 18 Oct 2024 01:53:38 +0300 Subject: [PATCH 2/2] bool flag for go run --- Makefile | 2 +- cmd/minimock/minimock.go | 57 ++++++++-------------------------- internal/templates/header.tmpl | 4 +-- tests/reader_mock.go | 2 +- 4 files changed, 17 insertions(+), 48 deletions(-) diff --git a/Makefile b/Makefile index 7a6d4df..a6dd335 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ generate: go run ./cmd/minimock/minimock.go -i ./tests.actor -o ./tests/actor_mock.go go run ./cmd/minimock/minimock.go -i ./tests.formatterAlias -o ./tests/formatter_alias_mock.go go run ./cmd/minimock/minimock.go -i ./tests.formatterType -o ./tests/formatter_type_mock.go - go run ./cmd/minimock/minimock.go -i ./tests.reader -o ./tests/reader_mock.go + go run ./cmd/minimock/minimock.go -i ./tests.reader -o ./tests/reader_mock.go -gr ./bin: mkdir ./bin diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index b7cfe7b..45fa916 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -8,7 +8,6 @@ import ( "go/token" "io" "io/ioutil" - "log" "os" "path/filepath" "runtime/debug" @@ -16,7 +15,7 @@ import ( "text/template" "time" - minimock "github.com/gojuno/minimock/v3" + "github.com/gojuno/minimock/v3" "github.com/gojuno/minimock/v3/internal/types" "github.com/hexdigest/gowrap/generator" "github.com/hexdigest/gowrap/pkg" @@ -24,11 +23,7 @@ import ( "golang.org/x/tools/go/packages" ) -const ( - devVersion = "dev" - runMode = "go-run" - directMode = "direct" -) +const devVersion = "dev" var ( //do not modify the following vars @@ -56,12 +51,12 @@ var helpers = template.FuncMap{ type ( options struct { - interfaces []interfaceInfo - noGenerate bool - suffix string - mockNames []string - packageNames []string - goGenerateMode string + interfaces []interfaceInfo + noGenerate bool + suffix string + mockNames []string + packageNames []string + goGenerateGoRun bool } interfaceInfo struct { @@ -115,7 +110,7 @@ func getVersion(version string, buildInfo *debug.BuildInfo) string { } func main() { - opts, err := processArgs(os.Args[0], os.Args[1:], os.Stdout, os.Stderr) + opts, err := processArgs(os.Args[1:], os.Stdout, os.Stderr) if err != nil { if err == errInvalidArguments { os.Exit(2) @@ -168,7 +163,7 @@ func run(opts *options) (err error) { "GenerateInstruction": !opts.noGenerate, "Version": version, "PackageName": packageName, - "GoGenerateMode": opts.goGenerateMode, + "GenerateGoRun": opts.goGenerateGoRun, }, Vars: map[string]interface{}{}, Funcs: helpers, @@ -381,32 +376,16 @@ func processNames(names string, interfacesNum int, isInterfaceWildeCarded bool) return namesSplitted, nil } -func processGoGenerateMode(cmd string, args []string, goGenerateMode string) (string, error) { - log.Println(cmd) - log.Println(args) - log.Println(goGenerateMode) - if goGenerateMode != "" && goGenerateMode != directMode && goGenerateMode != runMode { - return "", fmt.Errorf("wrong mode value") - } - - if goGenerateMode != "" { - return goGenerateMode, nil - } - if cmd == "go" && len(args) > 0 && args[0] == "run" { - return runMode, nil - } - - return directMode, nil -} - var errInvalidArguments = errors.New("invalid arguments") -func processArgs(cmd string, args []string, stdout, stderr io.Writer) (*options, error) { +func processArgs(args []string, stdout, stderr io.Writer) (*options, error) { var opts options fs := flag.NewFlagSet("", flag.ContinueOnError) fs.BoolVar(&opts.noGenerate, "g", false, "don't put go:generate instruction into the generated code") + fs.BoolVar(&opts.goGenerateGoRun, "gr", false, `changes go:generate line from "//go:generate minimock args..." to "//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock", +useful while controlling minimock version with go mod`) fs.StringVar(&opts.suffix, "s", "_mock_test.go", "mock file suffix") input := fs.String("i", "*", "comma-separated names of the interfaces to mock, i.e fmt.Stringer,io.Reader\nuse io.* notation to generate mocks for all interfaces in the \"io\" package") @@ -415,10 +394,6 @@ func processArgs(cmd string, args []string, stdout, stderr io.Writer) (*options, packageNames := fs.String("p", "", "comma-separated package names,\nby default the generated package names are taken from the destination directory names") help := fs.Bool("h", false, "show this help message") version := fs.Bool("version", false, "display version information and exit") - goGenerateMode := fs.String("go-generate-mode", "", `changes go:generate command mode: direct/go-run. -By default puts "go:generate minimock args..." instruction into generated code if mock was generated with minimock command. -If mock was generated with go run github.com/gojuno/minimock/v3/cmd/minimock then it puts "go:generate go run github.com/gojuno/minimock/v3/cmd/minimock args...". -This behaviour can be rewritten by explicitly specifying mode with flag.`) fs.Usage = func() { usage(fs, stderr) } @@ -436,12 +411,6 @@ This behaviour can be rewritten by explicitly specifying mode with flag.`) return nil, nil } - cmdMode, err := processGoGenerateMode(cmd, args, *goGenerateMode) - if err != nil { - return nil, fmt.Errorf("processing -go-generate-mode flag arguments: %w", err) - } - opts.goGenerateMode = cmdMode - interfaces := strings.Split(*input, ",") interfacesLen := len(interfaces) isWildecarded := strings.Contains(*input, "*") diff --git a/internal/templates/header.tmpl b/internal/templates/header.tmpl index 734da1e..77a9f57 100644 --- a/internal/templates/header.tmpl +++ b/internal/templates/header.tmpl @@ -8,8 +8,8 @@ package {{$packageName}} {{if $.Options.HeaderVars.GenerateInstruction}} - {{if eq $.Options.HeaderVars.GoGenerateMode "go-run"}} -//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} + {{if $.Options.HeaderVars.GenerateGoRun}} +//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} -gr {{else}} //go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} {{end}} diff --git a/tests/reader_mock.go b/tests/reader_mock.go index 04decdd..72c00e7 100644 --- a/tests/reader_mock.go +++ b/tests/reader_mock.go @@ -2,7 +2,7 @@ package tests -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.reader -o reader_mock.go -n ReaderMock -p tests +//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock -i github.com/gojuno/minimock/v3/tests.reader -o reader_mock.go -n ReaderMock -p tests -gr import ( "sync"