Skip to content

Commit b47f3b6

Browse files
committed
Update lint rules, force testify/assert for tests
Use testify's assert package instead of the standard library's testing package.
1 parent 68f413c commit b47f3b6

20 files changed

Lines changed: 380 additions & 780 deletions

.golangci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ linters-settings:
1919
recommendations:
2020
- errors
2121
forbidigo:
22+
analyze-types: true
2223
forbid:
2324
- ^fmt.Print(f|ln)?$
2425
- ^log.(Panic|Fatal|Print)(f|ln)?$
2526
- ^os.Exit$
2627
- ^panic$
2728
- ^print(ln)?$
29+
- p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$
30+
pkg: ^testing$
31+
msg: "use testify/assert instead"
2832
varnamelen:
2933
max-distance: 12
3034
min-name-length: 2
@@ -127,9 +131,12 @@ issues:
127131
exclude-dirs-use-default: false
128132
exclude-rules:
129133
# Allow complex tests and examples, better to be self contained
130-
- path: (examples|main\.go|_test\.go)
134+
- path: (examples|main\.go)
131135
linters:
136+
- gocognit
132137
- forbidigo
138+
- path: _test\.go
139+
linters:
133140
- gocognit
134141

135142
# Allow forbidden identifiers in CLI commands

connctx/connctx_test.go

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
package connctx
55

66
import (
7-
"bytes"
87
"context"
98
"errors"
109
"io"
1110
"net"
1211
"testing"
1312
"time"
13+
14+
"github.com/stretchr/testify/assert"
1415
)
1516

1617
func TestRead(t *testing.T) {
@@ -30,20 +31,12 @@ func TestRead(t *testing.T) {
3031
c := New(ca)
3132
b := make([]byte, 100)
3233
n, err := c.ReadContext(context.Background(), b)
33-
if err != nil {
34-
t.Fatal(err)
35-
}
36-
if n != len(data) {
37-
t.Errorf("Wrong data length, expected %d, got %d", len(data), n)
38-
}
39-
if !bytes.Equal(data, b[:n]) {
40-
t.Errorf("Wrong data, expected %v, got %v", data, b)
41-
}
34+
assert.NoError(t, err)
35+
assert.Len(t, data, n)
36+
assert.Equal(t, data, b[:n])
4237

4338
err = <-chErr
44-
if err != nil {
45-
t.Fatal(err)
46-
}
39+
assert.NoError(t, err)
4740
}
4841

4942
func TestReadTimeout(t *testing.T) {
@@ -58,12 +51,8 @@ func TestReadTimeout(t *testing.T) {
5851
c := New(ca)
5952
b := make([]byte, 100)
6053
n, err := c.ReadContext(ctx, b)
61-
if err == nil {
62-
t.Error("Read unexpectedly succeeded")
63-
}
64-
if n != 0 {
65-
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
66-
}
54+
assert.Error(t, err)
55+
assert.Empty(t, n)
6756
}
6857

6958
func TestReadCancel(t *testing.T) {
@@ -81,12 +70,8 @@ func TestReadCancel(t *testing.T) {
8170
c := New(ca)
8271
b := make([]byte, 100)
8372
n, err := c.ReadContext(ctx, b)
84-
if err == nil {
85-
t.Error("Read unexpectedly succeeded")
86-
}
87-
if n != 0 {
88-
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
89-
}
73+
assert.Error(t, err)
74+
assert.Empty(t, n)
9075
}
9176

9277
func TestReadClosed(t *testing.T) {
@@ -97,12 +82,8 @@ func TestReadClosed(t *testing.T) {
9782

9883
b := make([]byte, 100)
9984
n, err := c.ReadContext(context.Background(), b)
100-
if !errors.Is(err, io.EOF) {
101-
t.Errorf("Expected error '%v', got '%v'", io.EOF, err)
102-
}
103-
if n != 0 {
104-
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
105-
}
85+
assert.ErrorIs(t, err, io.EOF)
86+
assert.Empty(t, n)
10687
}
10788

10889
func TestWrite(t *testing.T) {
@@ -124,21 +105,13 @@ func TestWrite(t *testing.T) {
124105
c := New(ca)
125106
data := []byte{0x01, 0x02, 0xFF}
126107
n, err := c.WriteContext(context.Background(), data)
127-
if err != nil {
128-
t.Fatal(err)
129-
}
130-
if n != len(data) {
131-
t.Errorf("Wrong data length, expected %d, got %d", len(data), n)
132-
}
108+
assert.NoError(t, err)
109+
assert.Len(t, data, n)
133110

134111
err = <-chErr
135112
b := <-chRead
136-
if !bytes.Equal(data, b) {
137-
t.Errorf("Wrong data, expected %v, got %v", data, b)
138-
}
139-
if err != nil {
140-
t.Fatal(err)
141-
}
113+
assert.Equal(t, b, data)
114+
assert.NoError(t, err)
142115
}
143116

144117
func TestWriteTimeout(t *testing.T) {
@@ -153,12 +126,8 @@ func TestWriteTimeout(t *testing.T) {
153126
c := New(ca)
154127
b := make([]byte, 100)
155128
n, err := c.WriteContext(ctx, b)
156-
if err == nil {
157-
t.Error("Write unexpectedly succeeded")
158-
}
159-
if n != 0 {
160-
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
161-
}
129+
assert.Error(t, err)
130+
assert.Empty(t, n)
162131
}
163132

164133
func TestWriteCancel(t *testing.T) {
@@ -176,12 +145,8 @@ func TestWriteCancel(t *testing.T) {
176145
c := New(ca)
177146
b := make([]byte, 100)
178147
n, err := c.WriteContext(ctx, b)
179-
if err == nil {
180-
t.Error("Write unexpectedly succeeded")
181-
}
182-
if n != 0 {
183-
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
184-
}
148+
assert.Error(t, err)
149+
assert.Empty(t, n)
185150
}
186151

187152
func TestWriteClosed(t *testing.T) {
@@ -192,12 +157,8 @@ func TestWriteClosed(t *testing.T) {
192157

193158
b := make([]byte, 100)
194159
n, err := c.WriteContext(context.Background(), b)
195-
if !errors.Is(err, ErrClosing) {
196-
t.Errorf("Expected error '%v', got '%v'", ErrClosing, err)
197-
}
198-
if n != 0 {
199-
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
200-
}
160+
assert.ErrorIs(t, err, ErrClosing)
161+
assert.Empty(t, n)
201162
}
202163

203164
// Test for TestLocalAddrAndRemoteAddr.
@@ -211,26 +172,39 @@ func (a stringAddr) String() string { return a.addr }
211172

212173
type connAddrMock struct{}
213174

214-
func (*connAddrMock) RemoteAddr() net.Addr { return stringAddr{"remote_net", "remote_addr"} }
215-
func (*connAddrMock) LocalAddr() net.Addr { return stringAddr{"local_net", "local_addr"} }
216-
func (*connAddrMock) Read(_ []byte) (n int, err error) { panic("unimplemented") }
217-
func (*connAddrMock) Write(_ []byte) (n int, err error) { panic("unimplemented") }
218-
func (*connAddrMock) Close() error { panic("unimplemented") }
219-
func (*connAddrMock) SetDeadline(_ time.Time) error { panic("unimplemented") }
220-
func (*connAddrMock) SetReadDeadline(_ time.Time) error { panic("unimplemented") }
221-
func (*connAddrMock) SetWriteDeadline(_ time.Time) error { panic("unimplemented") }
175+
func (*connAddrMock) RemoteAddr() net.Addr { return stringAddr{"remote_net", "remote_addr"} }
176+
func (*connAddrMock) LocalAddr() net.Addr { return stringAddr{"local_net", "local_addr"} }
177+
func (*connAddrMock) Read(_ []byte) (n int, err error) {
178+
panic("unimplemented") //nolint
179+
}
180+
181+
func (*connAddrMock) Write(_ []byte) (n int, err error) {
182+
panic("unimplemented") //nolint
183+
}
184+
185+
func (*connAddrMock) Close() error {
186+
panic("unimplemented") //nolint
187+
}
188+
189+
func (*connAddrMock) SetDeadline(_ time.Time) error {
190+
panic("unimplemented") //nolint
191+
}
192+
193+
func (*connAddrMock) SetReadDeadline(_ time.Time) error {
194+
panic("unimplemented") //nolint
195+
}
196+
197+
func (*connAddrMock) SetWriteDeadline(_ time.Time) error {
198+
panic("unimplemented") //nolint
199+
}
222200

223201
func TestLocalAddrAndRemoteAddr(t *testing.T) {
224202
c := New(&connAddrMock{})
225203
al := c.LocalAddr()
226204
ar := c.RemoteAddr()
227205

228-
if al.String() != "local_addr" {
229-
t.Error("Wrong LocalAddr implementation")
230-
}
231-
if ar.String() != "remote_addr" {
232-
t.Error("Wrong RemoteAddr implementation")
233-
}
206+
assert.Equal(t, "local_addr", al.String())
207+
assert.Equal(t, "remote_addr", ar.String())
234208
}
235209

236210
func BenchmarkBase(b *testing.B) {

deadline/deadline_test.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
package deadline
55

66
import (
7-
"bytes"
87
"context"
9-
"errors"
108
"testing"
119
"time"
10+
11+
"github.com/stretchr/testify/assert"
1212
)
1313

1414
func TestDeadline(t *testing.T) {
@@ -32,9 +32,7 @@ func TestDeadline(t *testing.T) {
3232

3333
calls := collectCh(ch, 3, 100*time.Millisecond)
3434
expectedCalls := []byte{0, 2, 1}
35-
if !bytes.Equal(calls, expectedCalls) {
36-
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
37-
}
35+
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
3836
})
3937

4038
t.Run("DeadlineExtend", func(t *testing.T) { //nolint:dupl
@@ -55,9 +53,7 @@ func TestDeadline(t *testing.T) {
5553

5654
calls := collectCh(ch, 3, 100*time.Millisecond)
5755
expectedCalls := []byte{0, 1, 2}
58-
if !bytes.Equal(calls, expectedCalls) {
59-
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
60-
}
56+
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
6157
})
6258

6359
t.Run("DeadlinePretend", func(t *testing.T) { //nolint:dupl
@@ -78,9 +74,7 @@ func TestDeadline(t *testing.T) {
7874

7975
calls := collectCh(ch, 3, 100*time.Millisecond)
8076
expectedCalls := []byte{2, 0, 1}
81-
if !bytes.Equal(calls, expectedCalls) {
82-
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
83-
}
77+
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
8478
})
8579

8680
t.Run("DeadlineCancel", func(t *testing.T) {
@@ -98,9 +92,7 @@ func TestDeadline(t *testing.T) {
9892

9993
calls := collectCh(ch, 2, 60*time.Millisecond)
10094
expectedCalls := []byte{0}
101-
if !bytes.Equal(calls, expectedCalls) {
102-
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls)
103-
}
95+
assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal")
10496
})
10597
}
10698

@@ -134,42 +126,30 @@ func TestContext(t *testing.T) { //nolint:cyclop
134126

135127
select {
136128
case <-deadline.Done():
137-
t.Fatal("Deadline unexpectedly done")
129+
assert.Fail(t, "Deadline unexpectedly done")
138130
case <-time.After(50 * time.Millisecond):
139131
}
140-
if err := deadline.Err(); err != nil {
141-
t.Errorf("Wrong Err(), expected: nil, got: %v", err)
142-
}
132+
assert.NoError(t, deadline.Err())
143133
deadline.Set(time.Unix(0, 1)) // exceeded
144134
select {
145135
case <-deadline.Done():
146136
case <-time.After(50 * time.Millisecond):
147-
t.Fatal("Timeout")
148-
}
149-
if err := deadline.Err(); !errors.Is(err, context.DeadlineExceeded) {
150-
t.Errorf("Wrong Err(), expected: %v, got: %v", context.DeadlineExceeded, err)
137+
assert.Fail(t, "Timeout")
151138
}
139+
assert.ErrorIs(t, deadline.Err(), context.DeadlineExceeded)
152140
})
153141
t.Run("Deadline", func(t *testing.T) {
154142
d := New()
155143
t0, expired0 := d.Deadline()
156-
if !t0.IsZero() {
157-
t.Errorf("Initial Deadline is expected to be 0, got %v", t0)
158-
}
159-
if expired0 {
160-
t.Error("Deadline is not expected to be expired at initial state")
161-
}
144+
assert.True(t, t0.IsZero(), "Initial Deadline is expected to be 0")
145+
assert.False(t, expired0, "Deadline is not expected to be expired at initial state")
162146

163147
dl := time.Unix(12345, 0)
164148
d.Set(dl) // exceeded
165149

166150
t1, expired1 := d.Deadline()
167-
if !t1.Equal(dl) {
168-
t.Errorf("Initial Deadline is expected to be %v, got %v", dl, t1)
169-
}
170-
if !expired1 {
171-
t.Error("Deadline is expected to be expired")
172-
}
151+
assert.True(t, t1.Equal(dl), "Initial Deadline is expected to be %v, got %v", dl, t1)
152+
assert.True(t, expired1, "Deadline is expected to be expired")
173153
})
174154
}
175155

0 commit comments

Comments
 (0)