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

mockgen: package _test prepend package name #248

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mockgen/internal/tests/mock_in_test_package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Test the case where the package has the `_test` suffix.

Prior to patch:

$ go generate
$ go test
# github.com/golang/mock/mockgen/internal/tests/mock_in_test_package_test [github.com/golang/mock/mockgen/internal/tests/mock_in_test_package.test]
./mock_test.go:36:44: undefined: User
./mock_test.go:38:21: undefined: User
FAIL github.com/golang/mock/mockgen/internal/tests/mock_in_test_package [build failed]

With this patch applied:

$ go generate
$ go test
ok github.com/golang/mock/mockgen/internal/tests/mock_in_test_package 0.031s
60 changes: 60 additions & 0 deletions mockgen/internal/tests/mock_in_test_package/mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions mockgen/internal/tests/mock_in_test_package/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package users

//go:generate mockgen --source=user.go --destination=mock_test.go --package=users_test

type User struct {
Name string
}

type Finder interface {
FindUser(name string) User
Add(u User)
}
1 change: 1 addition & 0 deletions mockgen/internal/tests/test_package/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package users
59 changes: 59 additions & 0 deletions mockgen/internal/tests/test_package/mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions mockgen/internal/tests/test_package/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package users_test

//go:generate mockgen --source=user_test.go --destination=mock_test.go --package=users_test

type User struct {
Name string
}

type Finder interface {
FindUser(name string) User
Add(u User)
}
5 changes: 5 additions & 0 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func main() {
log.Fatalf("Failed writing to destination: %v", err)
}
}

func parseMockNames(names string) map[string]string {
mocksMap := make(map[string]string)
for _, kv := range strings.Split(names, ",") {
Expand Down Expand Up @@ -223,6 +224,10 @@ func sanitize(s string) string {
}

func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePath string) error {
if pkgName != pkg.Name {
outputPackagePath = ""
}

g.p("// Code generated by MockGen. DO NOT EDIT.")
if g.filename != "" {
g.p("// Source: %v", g.filename)
Expand Down