Skip to content

Commit d01ed30

Browse files
tulzkeJacobOaks
andauthored
deprecated reflect mode has been replaced with package mode (#207)
It is impossible to create an mock for a generic interface via reflect mode, because it is impossible to compile a generic type without instantiation. This PR replaces the reflect mod for parsing using go/types. All exists mocks have been regenerated and the tests have been passed. But since this radically changes the behavior of reflect mode, I would be grateful if there are those who want to add additional test cases that I did not provide. We can also come up with another name instead of import mode. benefits: generation mocks for generic interfaces generation mocks for aliases to interfaces correct names for method arguments resolves #175 resolves #197 resolves #128 --------- Co-authored-by: Jacob Oaks <[email protected]>
1 parent 6d5eb71 commit d01ed30

File tree

30 files changed

+3749
-364
lines changed

30 files changed

+3749
-364
lines changed

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export PATH=$PATH:$(go env GOPATH)/bin
4040

4141
## Running mockgen
4242

43-
`mockgen` has two modes of operation: source and reflect.
43+
`mockgen` has two modes of operation: source and package.
4444

4545
### Source mode
4646

@@ -54,11 +54,10 @@ Example:
5454
mockgen -source=foo.go [other options]
5555
```
5656

57-
### Reflect mode
57+
### Package mode
5858

59-
Reflect mode generates mock interfaces by building a program
60-
that uses reflection to understand interfaces. It is enabled
61-
by passing two non-flag arguments: an import path, and a
59+
Package mode works by specifying the package and interface names.
60+
It is enabled by passing two non-flag arguments: an import path, and a
6261
comma-separated list of symbols.
6362

6463
You can use "." to refer to the current path's package.
@@ -98,7 +97,7 @@ It supports the following flags:
9897
`foo=bar/baz.go`, where `bar/baz.go` is the source file and `foo` is the
9998
package name of that file used by the -source file.
10099

101-
- `-build_flags`: (reflect mode only) Flags passed verbatim to `go build`.
100+
- `-build_flags`: (package mode only) Flags passed verbatim to `go list`.
102101

103102
- `-mock_names`: A list of custom names for generated mocks. This is specified
104103
as a comma-separated list of elements of the form
@@ -119,15 +118,11 @@ It supports the following flags:
119118

120119
- `-debug_parser`: Print out parser results only.
121120

122-
- `-exec_only`: (reflect mode) If set, execute this reflection program.
123-
124-
- `-prog_only`: (reflect mode) Only generate the reflection program; write it to stdout and exit.
125-
126121
- `-write_package_comment`: Writes package documentation comment (godoc) if true. (default true)
127122

128123
- `-write_generate_directive`: Add //go:generate directive to regenerate the mock. (default false)
129124

130-
- `-write_source_comment`: Writes original file (source mode) or interface names (reflect mode) comment if true. (default true)
125+
- `-write_source_comment`: Writes original file (source mode) or interface names (package mode) comment if true. (default true)
131126

132127
- `-typed`: Generate Type-safe 'Return', 'Do', 'DoAndReturn' function. (default false)
133128

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ module go.uber.org/mock
33
go 1.22
44

55
require (
6+
github.com/stretchr/testify v1.9.0
67
golang.org/x/mod v0.18.0
78
golang.org/x/tools v0.22.0
89
)
910

10-
require github.com/yuin/goldmark v1.4.13 // indirect
11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
github.com/yuin/goldmark v1.4.13 // indirect
15+
golang.org/x/sync v0.7.0 // indirect
16+
gopkg.in/yaml.v3 v3.0.1 // indirect
17+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
17
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
28
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
39
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
@@ -6,3 +12,7 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
612
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
713
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
814
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
18+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

gomock/internal/mock_gomock/mock_matcher.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mockgen/deprecated.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
"os"
7+
)
8+
9+
const (
10+
deprecatedFlagProgOnly = "prog_only"
11+
deprecatedFlagExecOnly = "exec_only"
12+
)
13+
14+
var (
15+
_ = flag.Bool("prog_only", false, "DEPRECATED (reflect mode) Only generate the reflection program; write it to stdout and exit.")
16+
_ = flag.String("exec_only", "", "DEPRECATED (reflect mode) If set, execute this reflection program.")
17+
)
18+
19+
// notifyAboutDeprecatedFlags prints a warning message for a deprecated flags if they are set.
20+
func notifyAboutDeprecatedFlags() {
21+
const resetColorPostfix = "\033[0m"
22+
logger := initWarningLogger()
23+
24+
flag.Visit(func(f *flag.Flag) {
25+
switch f.Name {
26+
case deprecatedFlagProgOnly:
27+
logger.Println("The -prog_only flag is deprecated and has no effect.", resetColorPostfix)
28+
case deprecatedFlagExecOnly:
29+
logger.Println("The -exec_only flag is deprecated and has no effect.", resetColorPostfix)
30+
}
31+
})
32+
}
33+
34+
func initWarningLogger() *log.Logger {
35+
const (
36+
yellowColor = "\033[33m"
37+
warningPrefix = yellowColor + "WARNING: "
38+
)
39+
40+
return log.New(os.Stdout, warningPrefix, log.Ldate|log.Ltime)
41+
}

mockgen/internal/tests/add_generate_directive/mock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package build_flags
2+
3+
// one build flag
4+
//go:generate mockgen -destination=mock1/interfaces_mock.go -build_flags=-tags=tag1 . Interface
5+
// multiple build flags
6+
//go:generate mockgen -destination=mock2/interfaces_mock.go "-build_flags=-race -tags=tag2" . Interface
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build tag1
2+
3+
package build_flags
4+
5+
type Interface interface {
6+
HelloWorld() string
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build tag2
2+
3+
package build_flags
4+
5+
type Interface interface {
6+
Foo()
7+
}

mockgen/internal/tests/build_flags/mock1/interfaces_mock.go

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mockgen/internal/tests/build_flags/mock2/interfaces_mock.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mockgen/internal/tests/extra_import/mock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mockgen/internal/tests/generics/external.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
//go:generate mockgen --source=external.go --destination=source/mock_external_mock.go --package source
12+
//go:generate mockgen --destination=package_mode/mock_external_mock.go -package=package_mode . ExternalConstraint,EmbeddingIface,Generator,Group
1213

1314
type ExternalConstraint[I constraints.Integer, F any] interface {
1415
One(string) string

mockgen/internal/tests/generics/generics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
//go:generate mockgen --source=generics.go --destination=source/mock_generics_mock.go --package source
9-
////go:generate mockgen --destination=reflect/mock_test.go --package reflect . Bar,Bar2
9+
//go:generate mockgen --destination=package_mode/mock_test.go --package=package_mode . Bar,Universe,MilkyWay,SolarSystem,Earth,Water
1010

1111
type Bar[T any, R any] interface {
1212
One(string) string

0 commit comments

Comments
 (0)