A linter that checks the usage of go mocking libraries
Note: The original golang/mock package is archived and the maintained fork is go.uber.org/mock.
This linter supports both.
$ go install github.com/hendrywiranto/gomocklinter@latest
$ gomocklinter ./...
or build the binary and use go vet
$ go build -o gomocklinter main.go
$ go vet -vettool=./gomocklinter ./...
Autofix is supported that will delete the entire line with .Finish()
call to gomock.Controller
$ ./gomocklinter -fix ./...
Please note that, there may be some adjustments need to be done by hand after this action
// Before
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish() // will be deleted entirely
}
// After
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
}
As highlighted in https://pkg.go.dev/github.com/golang/mock/gomock#NewController
New in go1.14+, if you are passing a *testing.T into this function you no longer need to call ctrl.Finish() in your test methods.
// Bad
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish() // no need to call this since go1.14
}
// Good
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
}