From bb5fd5e98f66206b5bcfed8aee20f3be240a04bf Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Fri, 23 Apr 2021 14:45:47 -0600 Subject: [PATCH] fix linter errors (#552) --- gomock/call.go | 2 +- gomock/call_test.go | 176 +++++-------------- gomock/controller_test.go | 33 ++-- gomock/matchers_test.go | 4 +- mockgen/internal/tests/self_package/types.go | 4 +- mockgen/mockgen.go | 7 - sample/concurrent/concurrent_test.go | 6 +- sample/user_test.go | 5 +- 8 files changed, 70 insertions(+), 167 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index bd994266..3f77be4e 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -380,7 +380,7 @@ func (c *Call) matches(args []interface{}) error { // Check that all prerequisite calls have been satisfied. for _, preReqCall := range c.preReqs { if !preReqCall.satisfied() { - return fmt.Errorf("Expected call at %s doesn't have a prerequisite call satisfied:\n%v\nshould be called before:\n%v", + return fmt.Errorf("expected call at %s doesn't have a prerequisite call satisfied:\n%v\nshould be called before:\n%v", c.origin, preReqCall, c) } } diff --git a/gomock/call_test.go b/gomock/call_test.go index e90a5d67..49e6986a 100644 --- a/gomock/call_test.go +++ b/gomock/call_test.go @@ -126,9 +126,7 @@ var testCases []testCase = []testCase{ { description: "argument to Do is not a function", doFunc: "meow", - callFunc: func(x int, y int) { - return - }, + callFunc: func(x int, y int) {}, args: []interface{}{0, 1}, expectPanic: true, }, { @@ -141,12 +139,8 @@ var testCases []testCase = []testCase{ expectPanic: true, }, { description: "number of args for Do func don't match Call func", - doFunc: func(x int) { - return - }, - callFunc: func(x int, y int) { - return - }, + doFunc: func(x int) {}, + callFunc: func(x int, y int) {}, args: []interface{}{0, 1}, expectPanic: true, }, { @@ -161,12 +155,8 @@ var testCases []testCase = []testCase{ expectPanic: true, }, { description: "arg type for Do func incompatible with Call func", - doFunc: func(x int) { - return - }, - callFunc: func(x string) { - return - }, + doFunc: func(x int) {}, + callFunc: func(x string) {}, args: []interface{}{"meow"}, expectPanic: true, }, { @@ -181,22 +171,14 @@ var testCases []testCase = []testCase{ expectPanic: true, }, { description: "Do func(int) Call func(int)", - doFunc: func(x int) { - return - }, - callFunc: func(x int) { - return - }, - args: []interface{}{0}, + doFunc: func(x int) {}, + callFunc: func(x int) {}, + args: []interface{}{0}, }, { description: "Do func(int) Call func(interface{})", - doFunc: func(x int) { - return - }, - callFunc: func(x interface{}) { - return - }, - args: []interface{}{0}, + doFunc: func(x int) {}, + callFunc: func(x interface{}) {}, + args: []interface{}{0}, }, { description: "Do func(int) bool Call func(int) bool", doFunc: func(x int) bool { @@ -217,12 +199,8 @@ var testCases []testCase = []testCase{ args: []interface{}{0}, }, { description: "Do func(string) Call func([]byte)", - doFunc: func(x string) { - return - }, - callFunc: func(x []byte) { - return - }, + doFunc: func(x string) {}, + callFunc: func(x []byte) {}, args: []interface{}{[]byte("meow")}, expectPanic: true, }, { @@ -237,22 +215,14 @@ var testCases []testCase = []testCase{ expectPanic: true, }, { description: "Do func(map[int]string) Call func(map[interface{}]int)", - doFunc: func(x map[int]string) { - return - }, - callFunc: func(x map[interface{}]int) { - return - }, + doFunc: func(x map[int]string) {}, + callFunc: func(x map[interface{}]int) {}, args: []interface{}{map[interface{}]int{"meow": 0}}, expectPanic: true, }, { description: "Do func(map[int]string) Call func(map[interface{}]interface{})", - doFunc: func(x map[int]string) { - return - }, - callFunc: func(x map[interface{}]interface{}) { - return - }, + doFunc: func(x map[int]string) {}, + callFunc: func(x map[interface{}]interface{}) {}, args: []interface{}{map[interface{}]interface{}{"meow": "meow"}}, expectPanic: true, }, { @@ -277,61 +247,37 @@ var testCases []testCase = []testCase{ expectPanic: true, }, { description: "Do func([]string) Call func([]interface{})", - doFunc: func(x []string) { - return - }, - callFunc: func(x []interface{}) { - return - }, + doFunc: func(x []string) {}, + callFunc: func(x []interface{}) {}, args: []interface{}{[]interface{}{0}}, expectPanic: true, }, { description: "Do func([]string) Call func([]int)", - doFunc: func(x []string) { - return - }, - callFunc: func(x []int) { - return - }, + doFunc: func(x []string) {}, + callFunc: func(x []int) {}, args: []interface{}{[]int{0, 1}}, expectPanic: true, }, { description: "Do func([]int) Call func([]int)", - doFunc: func(x []int) { - return - }, - callFunc: func(x []int) { - return - }, - args: []interface{}{[]int{0, 1}}, + doFunc: func(x []int) {}, + callFunc: func(x []int) {}, + args: []interface{}{[]int{0, 1}}, }, { description: "Do func([]int) Call func([]interface{})", - doFunc: func(x []int) { - return - }, - callFunc: func(x []interface{}) { - return - }, + doFunc: func(x []int) {}, + callFunc: func(x []interface{}) {}, args: []interface{}{[]interface{}{0}}, expectPanic: true, }, { description: "Do func([]int) Call func(...interface{})", - doFunc: func(x []int) { - return - }, - callFunc: func(x ...interface{}) { - return - }, + doFunc: func(x []int) {}, + callFunc: func(x ...interface{}) {}, args: []interface{}{0, 1}, expectPanic: true, }, { description: "Do func([]int) Call func(...int)", - doFunc: func(x []int) { - return - }, - callFunc: func(x ...int) { - return - }, + doFunc: func(x []int) {}, + callFunc: func(x ...int) {}, args: []interface{}{0, 1}, expectPanic: true, }, { @@ -395,33 +341,21 @@ var testCases []testCase = []testCase{ expectPanic: true, }, { description: "Do func(...int) Call func([]int)", - doFunc: func(x ...int) { - return - }, - callFunc: func(x []int) { - return - }, + doFunc: func(x ...int) {}, + callFunc: func(x []int) {}, args: []interface{}{[]int{0, 1}}, expectPanic: true, }, { description: "Do func(...int) Call func([]interface{})", - doFunc: func(x ...int) { - return - }, - callFunc: func(x []interface{}) { - return - }, + doFunc: func(x ...int) {}, + callFunc: func(x []interface{}) {}, args: []interface{}{[]interface{}{0, 1}}, expectPanic: true, }, { description: "Do func(...int) Call func(...interface{})", - doFunc: func(x ...int) { - return - }, - callFunc: func(x ...interface{}) { - return - }, - args: []interface{}{0, 1}, + doFunc: func(x ...int) {}, + callFunc: func(x ...interface{}) {}, + args: []interface{}{0, 1}, }, { description: "Do func(...int) bool Call func(...int) bool", doFunc: func(x ...int) bool { @@ -462,40 +396,24 @@ var testCases []testCase = []testCase{ args: []interface{}{0, 1}, }, { description: "Do func(...int) Call func(...int)", - doFunc: func(x ...int) { - return - }, - callFunc: func(x ...int) { - return - }, - args: []interface{}{0, 1}, + doFunc: func(x ...int) {}, + callFunc: func(x ...int) {}, + args: []interface{}{0, 1}, }, { description: "Do func(foo); foo implements interface X Call func(interface X)", - doFunc: func(x foo) { - return - }, - callFunc: func(x fmt.Stringer) { - return - }, - args: []interface{}{foo{}}, + doFunc: func(x foo) {}, + callFunc: func(x fmt.Stringer) {}, + args: []interface{}{foo{}}, }, { description: "Do func(b); b does not implement interface X Call func(interface X)", - doFunc: func(x b) { - return - }, - callFunc: func(x fmt.Stringer) { - return - }, + doFunc: func(x b) {}, + callFunc: func(x fmt.Stringer) {}, args: []interface{}{foo{}}, expectPanic: true, }, { description: "Do func(b) Call func(a); a and b are not aliases", - doFunc: func(x b) { - return - }, - callFunc: func(x a) { - return - }, + doFunc: func(x b) {}, + callFunc: func(x a) {}, args: []interface{}{a{}}, expectPanic: true, }, { diff --git a/gomock/controller_test.go b/gomock/controller_test.go index d28fee11..9ba5a0e0 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -54,17 +54,6 @@ func (e *ErrorReporter) assertFail(msg string) { } } -func (e *ErrorReporter) assertLogf(expectedErrMsgs ...string) { - if len(e.log) < len(expectedErrMsgs) { - e.t.Fatalf("got %d Logf messages, want %d", len(e.log), len(expectedErrMsgs)) - } - for i, expectedErrMsg := range expectedErrMsgs { - if !strings.Contains(e.log[i], expectedErrMsg) { - e.t.Errorf("Error message:\ngot: %q\nwant to contain: %q\n", e.log[i], expectedErrMsg) - } - } -} - // Use to check that code triggers a fatal test failure. func (e *ErrorReporter) assertFatal(fn func(), expectedErrMsgs ...string) { defer func() { @@ -411,14 +400,14 @@ func TestMinTimes1(t *testing.T) { }) // It succeeds if there is one call - reporter, ctrl = createFixtures(t) + _, ctrl = createFixtures(t) subject = new(Subject) ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1) ctrl.Call(subject, "FooMethod", "argument") ctrl.Finish() // It succeeds if there are many calls - reporter, ctrl = createFixtures(t) + _, ctrl = createFixtures(t) subject = new(Subject) ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1) for i := 0; i < 100; i++ { @@ -473,7 +462,7 @@ func TestMinMaxTimes(t *testing.T) { }) // It succeeds if there is just the right number of calls - reporter, ctrl = createFixtures(t) + _, ctrl = createFixtures(t) subject = new(Subject) ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(2).MinTimes(2) ctrl.Call(subject, "FooMethod", "argument") @@ -491,7 +480,7 @@ func TestMinMaxTimes(t *testing.T) { }) // If MinTimes is called after MaxTimes is called with 1, MinTimes takes precedence. - reporter, ctrl = createFixtures(t) + _, ctrl = createFixtures(t) subject = new(Subject) ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1).MinTimes(2) for i := 0; i < 100; i++ { @@ -506,7 +495,8 @@ func TestDo(t *testing.T) { doCalled := false var argument string - ctrl.RecordCall(subject, "FooMethod", "argument").Do( + wantArg := "argument" + ctrl.RecordCall(subject, "FooMethod", wantArg).Do( func(arg string) { doCalled = true argument = arg @@ -515,12 +505,12 @@ func TestDo(t *testing.T) { t.Error("Do() callback called too early.") } - ctrl.Call(subject, "FooMethod", "argument") + ctrl.Call(subject, "FooMethod", wantArg) if !doCalled { t.Error("Do() callback not called.") } - if "argument" != argument { + if wantArg != argument { t.Error("Do callback received wrong argument.") } @@ -533,7 +523,8 @@ func TestDoAndReturn(t *testing.T) { doCalled := false var argument string - ctrl.RecordCall(subject, "FooMethod", "argument").DoAndReturn( + wantArg := "argument" + ctrl.RecordCall(subject, "FooMethod", wantArg).DoAndReturn( func(arg string) int { doCalled = true argument = arg @@ -543,12 +534,12 @@ func TestDoAndReturn(t *testing.T) { t.Error("Do() callback called too early.") } - rets := ctrl.Call(subject, "FooMethod", "argument") + rets := ctrl.Call(subject, "FooMethod", wantArg) if !doCalled { t.Error("Do() callback not called.") } - if "argument" != argument { + if wantArg != argument { t.Error("Do callback received wrong argument.") } if len(rets) != 1 { diff --git a/gomock/matchers_test.go b/gomock/matchers_test.go index 91ec5a44..42a1c5a8 100644 --- a/gomock/matchers_test.go +++ b/gomock/matchers_test.go @@ -90,6 +90,8 @@ type Dog struct { Breed, Name string } +type ctxKey struct{} + // A thorough test of assignableToTypeOfMatcher func TestAssignableToTypeOfMatcher(t *testing.T) { ctrl := gomock.NewController(t) @@ -137,7 +139,7 @@ func TestAssignableToTypeOfMatcher(t *testing.T) { t.Errorf(`AssignableToTypeOf(context.Context) should not match context.Background()`) } - ctxWithValue := context.WithValue(context.Background(), "key", "val") + ctxWithValue := context.WithValue(context.Background(), ctxKey{}, "val") if match := gomock.AssignableToTypeOf(ctxInterface).Matches(ctxWithValue); !match { t.Errorf(`AssignableToTypeOf(context.Context) should not match ctxWithValue`) } diff --git a/mockgen/internal/tests/self_package/types.go b/mockgen/internal/tests/self_package/types.go index e1083ca5..bf1bc766 100644 --- a/mockgen/internal/tests/self_package/types.go +++ b/mockgen/internal/tests/self_package/types.go @@ -2,9 +2,7 @@ package core //go:generate mockgen -package core -self_package github.com/golang/mock/mockgen/internal/tests/self_package -destination mock.go github.com/golang/mock/mockgen/internal/tests/self_package Methods -type Info struct { - name string -} +type Info struct{} type Methods interface { getInfo() Info diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 4a4ebe85..50487070 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -236,13 +236,6 @@ func (g *generator) out() { } } -func removeDot(s string) string { - if len(s) > 0 && s[len(s)-1] == '.' { - return s[0 : len(s)-1] - } - return s -} - // sanitize cleans up a string to make a suitable package name. func sanitize(s string) string { t := "" diff --git a/sample/concurrent/concurrent_test.go b/sample/concurrent/concurrent_test.go index 3beebe60..878ac427 100644 --- a/sample/concurrent/concurrent_test.go +++ b/sample/concurrent/concurrent_test.go @@ -4,6 +4,7 @@ import ( "testing" "context" + "github.com/golang/mock/gomock" mock "github.com/golang/mock/sample/concurrent/mock" @@ -23,10 +24,11 @@ func call(ctx context.Context, m Math) (int, error) { } } -// testConcurrentFails is expected to fail (and is disabled). It +// TestConcurrentFails is expected to fail (and is disabled). It // demonstrates how to use gomock.WithContext to interrupt the test // from a different goroutine. -func testConcurrentFails(t *testing.T) { +func TestConcurrentFails(t *testing.T) { + t.Skip("Test is expected to fail, remove skip to trying running yourself.") ctrl, ctx := gomock.WithContext(context.Background(), t) defer ctrl.Finish() m := mock.NewMockMath(ctrl) diff --git a/sample/user_test.go b/sample/user_test.go index f8ef0433..c007cfe6 100644 --- a/sample/user_test.go +++ b/sample/user_test.go @@ -168,9 +168,8 @@ func TestDoAndReturnSignature(t *testing.T) { mockIndex := mock_user.NewMockIndex(ctrl) mockIndex.EXPECT().Slice(gomock.Any(), gomock.Any()).DoAndReturn( - func(_ []int, _ []byte) { - return - }) + func(_ []int, _ []byte) {}, + ) defer func() { if r := recover(); r == nil {