Skip to content

Commit

Permalink
Add Unit test for github.com/uber-go/mock (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrywiranto committed Nov 15, 2023
1 parent 39f6d9f commit 3285549
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

A linter that checks whether an unnecessary call to .Finish() on gomock.Controller exists

Note: The original [golang/mock](https://github.com/golang/mock) package is archived and the maintained fork is [go.uber.org/mock](https://github.com/uber/mock). This linter supports both.

## Installation & usage

```
$ go install github.com/hendrywiranto/gomockcontrollerfinish@latest
$ gomockcontrollerfinish ./...
```

or use `go vet`
or build the binary and use `go vet`
```
$ go build -o gomockcontrollerfinish main.go
$ go vet -vettool=./gomockcontrollerfinish ./...
Expand Down
5 changes: 4 additions & 1 deletion pkg/analyzer/testdata/src/examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module examples

go 1.20

require github.com/golang/mock v1.6.0
require (
github.com/golang/mock v1.6.0
go.uber.org/mock v0.3.0
)
2 changes: 2 additions & 0 deletions pkg/analyzer/testdata/src/examples/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
6 changes: 4 additions & 2 deletions pkg/analyzer/testdata/src/examples/original_pkg_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package examples
package examples_test

import (
"testing"

"examples"

"github.com/golang/mock/gomock"
)

Expand Down Expand Up @@ -35,6 +37,6 @@ func TestNoFinishCall(t *testing.T) {
}

func TestFinishCallOther(t *testing.T) {
mock := New()
mock := examples.New()
mock.Finish()
}
5 changes: 2 additions & 3 deletions pkg/analyzer/testdata/src/examples/renamed_pkg_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package examples
package examples_test

import (
"testing"

"github.com/golang/mock/gomock"
gomick "github.com/golang/mock/gomock"
)

Expand All @@ -27,7 +26,7 @@ func TestRenamedFinsihCallInAnotherFunction(t *testing.T) {
renamedCallFinish(mock)
}

func renamedCallFinish(mock *gomock.Controller) {
func renamedCallFinish(mock *gomick.Controller) {
mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/analyzer/testdata/src/examples/uber_mock_pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package examples_test

import (
"examples"
"testing"

"go.uber.org/mock/gomock"
)

func TestUberFinishCall(t *testing.T) {
mock := gomock.NewController(t)
mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberFinishCallDefer(t *testing.T) {
mock := gomock.NewController(t)
defer mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberFinishCallWithoutT(t *testing.T) {
mock := gomock.NewController(nil)
mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberFinsihCallInAnotherFunction(t *testing.T) {
mock := gomock.NewController(t)
callUberFinish(mock)
}

func callUberFinish(mock *gomock.Controller) {
mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberNoFinishCall(t *testing.T) {
gomock.NewController(t)
}

func TestUberFinishCallOther(t *testing.T) {
mock := examples.New()
mock.Finish()
}
35 changes: 35 additions & 0 deletions pkg/analyzer/testdata/src/examples/uber_mock_renamed_pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package examples_test

import (
"testing"

gomick "go.uber.org/mock/gomock"
)

func TestUberRenamedFinishCall(t *testing.T) {
mock := gomick.NewController(t)
mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberRenamedFinishCallDefer(t *testing.T) {
mock := gomick.NewController(t)
defer mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberRenamedFinishCallWithoutT(t *testing.T) {
mock := gomick.NewController(nil)
mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberRenamedFinsihCallInAnotherFunction(t *testing.T) {
mock := gomick.NewController(t)
uberRenamedCallFinish(mock)
}

func uberRenamedCallFinish(mock *gomick.Controller) {
mock.Finish() // want "calling Finish on gomock.Controller is no longer needed"
}

func TestUberRenamedNoFinishCall(t *testing.T) {
gomick.NewController(t)
}

0 comments on commit 3285549

Please sign in to comment.