Skip to content

Commit a16b4d8

Browse files
committed
Use stretchr/testify to mock log.
Simplifies test log usage and removes additional targets and dependencies to go:generate mocks.
1 parent a2b2c7a commit a16b4d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4771
-3648
lines changed

Makefile

+3-6
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ test_linters:
4040

4141
# Maintenance
4242

43-
generate: README.md docs/demo.svg install.sh pkg/logutils/mock_logutils/mock_log.go vendor
43+
generate: README.md docs/demo.svg install.sh vendor
4444
fast_generate: README.md vendor
4545

4646
maintainer-clean: clean
47-
rm -f docs/demo.svg README.md install.sh pkg/logutils/mock_logutils/mock_log.go
47+
rm -f docs/demo.svg README.md install.sh
4848
rm -rf vendor
4949
.PHONY: generate maintainer-clean
5050

@@ -66,7 +66,7 @@ release:
6666

6767
# Non-PHONY targets (real files)
6868

69-
golangci-lint: FORCE pkg/logutils/mock_logutils/mock_log.go
69+
golangci-lint: FORCE
7070
go build -o $@ ./cmd/golangci-lint
7171

7272
tools:
@@ -88,9 +88,6 @@ install.sh: .goreleaser.yml
8888
README.md: FORCE golangci-lint
8989
go run ./scripts/gen_readme/main.go
9090

91-
pkg/logutils/mock_logutils/mock_log.go: pkg/logutils/log.go
92-
go generate ./...
93-
9491
go.mod: FORCE
9592
go mod tidy
9693
go mod verify

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/fatih/color v1.7.0
88
github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
99
github.com/go-lintpack/lintpack v0.5.2
10-
github.com/golang/mock v1.3.1
1110
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
1211
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
1312
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6

go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4er
339339
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
340340
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
341341
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
342-
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
343342
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
344343
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
345344
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -808,6 +807,7 @@ github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50/go.mod h1:1pdIZ
808807
github.com/streadway/amqp v0.0.0-20181205114330-a314942b2fd9/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY=
809808
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
810809
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
810+
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
811811
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
812812
github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
813813
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

pkg/golinters/gofmt_test.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ package golinters
33
import (
44
"testing"
55

6-
"github.com/golang/mock/gomock"
7-
8-
"github.com/golangci/golangci-lint/pkg/result"
9-
106
diffpkg "github.com/sourcegraph/go-diff/diff"
117
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/mock"
129

1310
"github.com/golangci/golangci-lint/pkg/logutils"
14-
"github.com/golangci/golangci-lint/pkg/logutils/mock_logutils"
11+
"github.com/golangci/golangci-lint/pkg/result"
1512
)
1613

1714
func testDiffProducesChanges(t *testing.T, log logutils.Log, diff string, expectedChanges ...Change) {
@@ -130,10 +127,9 @@ index 0000000..6399915
130127
+// line
131128
`
132129

133-
ctrl := gomock.NewController(t)
134-
defer ctrl.Finish()
135-
log := mock_logutils.NewMockLog(ctrl)
136-
log.EXPECT().Infof("The diff contains only additions: no original or deleted lines: %#v", gomock.Any())
130+
log := logutils.NewMockLog()
131+
log.On("Infof", "The diff contains only additions: no original or deleted lines: %#v", mock.Anything)
132+
137133
var noChanges []Change
138134
testDiffProducesChanges(t, log, diff, noChanges...)
139135
}

pkg/logutils/log.go

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package logutils
22

3-
//go:generate go run ../../vendor/github.com/golang/mock/mockgen -source log.go -destination mock_logutils/mock_log.go
4-
//go:generate go run ../../vendor/golang.org/x/tools/cmd/goimports -w mock_logutils/mock_log.go
5-
63
type Log interface {
74
Fatalf(format string, args ...interface{})
85
Panicf(format string, args ...interface{})

pkg/logutils/mock.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package logutils
2+
3+
import (
4+
"github.com/stretchr/testify/mock"
5+
)
6+
7+
type MockLog struct {
8+
mock.Mock
9+
}
10+
11+
func NewMockLog() *MockLog {
12+
return &MockLog{}
13+
}
14+
15+
func (m *MockLog) Fatalf(format string, args ...interface{}) {
16+
mArgs := []interface{}{format}
17+
m.Called(append(mArgs, args...)...)
18+
}
19+
20+
func (m *MockLog) Panicf(format string, args ...interface{}) {
21+
mArgs := []interface{}{format}
22+
m.Called(append(mArgs, args...)...)
23+
}
24+
25+
func (m *MockLog) Errorf(format string, args ...interface{}) {
26+
mArgs := []interface{}{format}
27+
m.Called(append(mArgs, args...)...)
28+
}
29+
30+
func (m *MockLog) Warnf(format string, args ...interface{}) {
31+
mArgs := []interface{}{format}
32+
m.Called(append(mArgs, args...)...)
33+
}
34+
35+
func (m *MockLog) Infof(format string, args ...interface{}) {
36+
mArgs := []interface{}{format}
37+
m.Called(append(mArgs, args...)...)
38+
}
39+
40+
func (m *MockLog) Child(name string) Log {
41+
m.Called(name)
42+
return m
43+
}
44+
45+
func (m *MockLog) SetLevel(level LogLevel) {
46+
m.Called(level)
47+
}

pkg/logutils/mock_logutils/mock_log.go

-146
This file was deleted.

pkg/result/processors/nolint_test.go

+9-19
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import (
66
"path/filepath"
77
"testing"
88

9-
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
10-
11-
"github.com/golang/mock/gomock"
129
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/mock"
1311

1412
"github.com/golangci/golangci-lint/pkg/lint/astcache"
13+
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
1514
"github.com/golangci/golangci-lint/pkg/logutils"
16-
"github.com/golangci/golangci-lint/pkg/logutils/mock_logutils"
1715
"github.com/golangci/golangci-lint/pkg/result"
1816
)
1917

@@ -43,18 +41,15 @@ func newTestNolintProcessor(log logutils.Log) *Nolint {
4341
return NewNolint(cache, log, lintersdb.NewManager(nil))
4442
}
4543

46-
func getOkLogger(ctrl *gomock.Controller) *mock_logutils.MockLog {
47-
log := mock_logutils.NewMockLog(ctrl)
48-
log.EXPECT().Infof(gomock.Any(), gomock.Any()).AnyTimes()
44+
func getMockLog() *logutils.MockLog {
45+
log := logutils.NewMockLog()
46+
log.On("Infof", mock.Anything, mock.Anything).Maybe()
4947
return log
5048
}
5149

5250
//nolint:funlen
5351
func TestNolint(t *testing.T) {
54-
ctrl := gomock.NewController(t)
55-
defer ctrl.Finish()
56-
57-
p := newTestNolintProcessor(getOkLogger(ctrl))
52+
p := newTestNolintProcessor(getMockLog())
5853
defer p.Finish()
5954

6055
// test inline comments
@@ -152,21 +147,16 @@ func TestNolintInvalidLinterName(t *testing.T) {
152147
},
153148
}
154149

155-
ctrl := gomock.NewController(t)
156-
defer ctrl.Finish()
157-
log := getOkLogger(ctrl)
158-
log.EXPECT().Warnf("Found unknown linters in //nolint directives: %s", "bad1, bad2")
150+
log := getMockLog()
151+
log.On("Warnf", "Found unknown linters in //nolint directives: %s", "bad1, bad2")
159152

160153
p := newTestNolintProcessor(log)
161154
processAssertEmpty(t, p, issues...)
162155
p.Finish()
163156
}
164157

165158
func TestNolintAliases(t *testing.T) {
166-
ctrl := gomock.NewController(t)
167-
defer ctrl.Finish()
168-
169-
p := newTestNolintProcessor(getOkLogger(ctrl))
159+
p := newTestNolintProcessor(getMockLog())
170160
for _, line := range []int{47, 49, 51} {
171161
line := line
172162
t.Run(fmt.Sprintf("line-%d", line), func(t *testing.T) {

tools.go

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ package tools
99
//
1010
// nolint
1111
import (
12-
_ "github.com/golang/mock/mockgen"
1312
_ "github.com/goreleaser/godownloader"
1413
_ "github.com/goreleaser/goreleaser"
15-
_ "golang.org/x/tools/cmd/goimports"
1614
)

vendor/github.com/golang/mock/AUTHORS

-12
This file was deleted.

0 commit comments

Comments
 (0)