Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit c2e99c4

Browse files
committed
mockgen: ensure package name is included when necessary
fixes #247
1 parent 3b08ada commit c2e99c4

File tree

7 files changed

+165
-0
lines changed

7 files changed

+165
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Test the case where the package has the `_test` suffix.
2+
3+
Prior to patch:
4+
5+
$ go generate
6+
$ go test
7+
# github.com/golang/mock/mockgen/internal/tests/mock_in_test_package_test [github.com/golang/mock/mockgen/internal/tests/mock_in_test_package.test]
8+
./mock_test.go:36:44: undefined: User
9+
./mock_test.go:38:21: undefined: User
10+
FAIL github.com/golang/mock/mockgen/internal/tests/mock_in_test_package [build failed]
11+
12+
With this patch applied:
13+
14+
$ go generate
15+
$ go test
16+
ok github.com/golang/mock/mockgen/internal/tests/mock_in_test_package 0.031s

mockgen/internal/tests/mock_in_test_package/mock_test.go

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package users
2+
3+
//go:generate mockgen --source=user.go --destination=mock_test.go --package=users_test
4+
5+
type User struct {
6+
Name string
7+
}
8+
9+
type Finder interface {
10+
FindUser(name string) User
11+
Add(u User)
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package users

mockgen/internal/tests/test_package/mock_test.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package users_test
2+
3+
//go:generate mockgen --source=user_test.go --destination=mock_test.go --package=users_test
4+
5+
type User struct {
6+
Name string
7+
}
8+
9+
type Finder interface {
10+
FindUser(name string) User
11+
Add(u User)
12+
}

mockgen/mockgen.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func main() {
134134
log.Fatalf("Failed writing to destination: %v", err)
135135
}
136136
}
137+
137138
func parseMockNames(names string) map[string]string {
138139
mocksMap := make(map[string]string)
139140
for _, kv := range strings.Split(names, ",") {
@@ -223,6 +224,10 @@ func sanitize(s string) string {
223224
}
224225

225226
func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePath string) error {
227+
if pkgName != pkg.Name {
228+
outputPackagePath = ""
229+
}
230+
226231
g.p("// Code generated by MockGen. DO NOT EDIT.")
227232
if g.filename != "" {
228233
g.p("// Source: %v", g.filename)

0 commit comments

Comments
 (0)