Skip to content

Commit 5113dab

Browse files
committed
mockgen: Sanitize the "any" package name
When a package name is "any" mockgen will use that as a package name in the generated code which causes issues due to colliding with Go's "any" type. Update the sanitization logic to prevent this collision.
1 parent 6dd8fe5 commit 5113dab

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package any
2+
3+
// Any is a type of a package that tests the sanitization of imported packages
4+
// named any.
5+
type Any struct {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package sanitization
2+
3+
import (
4+
"go.uber.org/mock/mockgen/internal/tests/sanitization/any"
5+
)
6+
7+
//go:generate mockgen -destination mockout/mock.go -package mockout . AnyMock
8+
9+
type AnyMock interface {
10+
Do(a *any.Any, b int)
11+
}

mockgen/internal/tests/sanitization/mockout/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.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package sanitization
2+
3+
import (
4+
"testing"
5+
6+
"go.uber.org/mock/gomock"
7+
any0 "go.uber.org/mock/mockgen/internal/tests/sanitization/any"
8+
"go.uber.org/mock/mockgen/internal/tests/sanitization/mockout"
9+
)
10+
11+
func TestSanitization(t *testing.T) {
12+
ctrl := gomock.NewController(t)
13+
m := mockout.NewMockAnyMock(ctrl)
14+
m.EXPECT().Do(gomock.Any(), 1)
15+
m.Do(&any0.Any{}, 1)
16+
}

mockgen/mockgen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
378378
}
379379

380380
i := 0
381-
for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() {
381+
for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() || pkgName == "any" {
382382
pkgName = base + strconv.Itoa(i)
383383
i++
384384
}

0 commit comments

Comments
 (0)