Skip to content

Commit 9264d6d

Browse files
committed
Update lint rules, force testify/assert
1 parent 7752cd4 commit 9264d6d

20 files changed

+175
-460
lines changed

.golangci.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ linters-settings:
2020
recommendations:
2121
- errors
2222
forbidigo:
23+
analyze-types: true
2324
forbid:
2425
- ^fmt.Print(f|ln)?$
2526
- ^log.(Panic|Fatal|Print)(f|ln)?$
2627
- ^os.Exit$
2728
- ^panic$
2829
- ^print(ln)?$
30+
- p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$
31+
pkg: ^testing$
32+
msg: "use testify/assert instead"
2933
varnamelen:
3034
max-distance: 12
3135
min-name-length: 2
@@ -124,13 +128,18 @@ linters:
124128
- wsl # Whitespace Linter - Forces you to use empty lines!
125129

126130
issues:
131+
max-issues-per-linter: 0
132+
max-same-issues: 0
127133
exclude-use-default: false
128134
exclude-dirs-use-default: false
129135
exclude-rules:
130136
# Allow complex tests and examples, better to be self contained
131-
- path: (examples|main\.go|_test\.go)
137+
- path: (examples|main\.go)
132138
linters:
139+
- gocognit
133140
- forbidigo
141+
- path: _test\.go
142+
linters:
134143
- gocognit
135144

136145
# Allow forbidden identifiers in CLI commands

application_defined_test.go

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
package rtcp
55

66
import (
7-
"errors"
8-
"reflect"
97
"testing"
8+
9+
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func TestTApplicationPacketUnmarshal(t *testing.T) {
@@ -128,27 +128,16 @@ func TestTApplicationPacketUnmarshal(t *testing.T) {
128128
} {
129129
var apk ApplicationDefined
130130
err := apk.Unmarshal(test.Data)
131-
if got, want := err, test.WantError; !errors.Is(got, want) {
132-
t.Fatalf("Unmarshal %q result: got = %v, want %v", test.Name, got, want)
133-
}
131+
assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q", test.Name)
134132
if err != nil {
135133
continue
136134
}
137135

138-
if got, want := apk, test.Want; !reflect.DeepEqual(got, want) {
139-
t.Fatalf("Unmarshal %q result: got %v, want %v", test.Name, got, want)
140-
}
136+
assert.Equalf(t, test.Want, apk, "Unmarshal %q", test.Name)
141137

142138
// Check SSRC is matching
143-
if apk.SSRC != 0x4baae1ab {
144-
t.Fatalf("SSRC %q result: got packet SSRC %x instead of %x", test.Name, apk.SSRC, 0x4baae1ab)
145-
}
146-
if apk.SSRC != apk.DestinationSSRC()[0] {
147-
t.Fatalf(
148-
"SSRC %q result: DestinationSSRC() %x doesn't match SSRC field %x",
149-
test.Name, apk.DestinationSSRC()[0], apk.SSRC,
150-
)
151-
}
139+
assert.Equalf(t, uint32(0x4baae1ab), apk.SSRC, "%q SSRC mismatch", test.Name)
140+
assert.Equalf(t, uint32(0x4baae1ab), apk.DestinationSSRC()[0], "%q DestinationSSRC mismatch", test.Name)
152141
}
153142
}
154143

@@ -246,24 +235,14 @@ func TestTApplicationPacketMarshal(t *testing.T) {
246235
},
247236
} {
248237
rawPacket, err := test.Packet.Marshal()
249-
250-
// Check for expected errors
251-
if got, want := err, test.WantError; !errors.Is(got, want) {
252-
t.Fatalf("Marshal %q result: got = %v, want %v", test.Name, got, want)
253-
}
238+
assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name)
254239
if err != nil {
255240
continue
256241
}
257242

258-
// Check for expected successful result
259-
if got, want := rawPacket, test.Want; !reflect.DeepEqual(got, want) {
260-
t.Fatalf("Marshal %q result: got %v, want %v", test.Name, got, want)
261-
}
243+
assert.Equalf(t, test.Want, rawPacket, "Marshal %q", test.Name)
262244

263-
// Check if MarshalSize() is matching the marshaled bytes
264245
marshalSize := test.Packet.MarshalSize()
265-
if marshalSize != len(rawPacket) {
266-
t.Fatalf("MarshalSize %q result: got %d bytes instead of %d", test.Name, len(rawPacket), marshalSize)
267-
}
246+
assert.Equalf(t, marshalSize, len(rawPacket), "MarshalSize %q", test.Name)
268247
}
269248
}

compound_packet_test.go

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package rtcp
55

66
import (
7-
"errors"
8-
"reflect"
97
"testing"
108

119
"github.com/stretchr/testify/assert"
@@ -39,21 +37,14 @@ func TestBadCompound(t *testing.T) {
3937

4038
// this should return an error,
4139
// it violates the "must start with RR or SR" rule
42-
err = compound.Validate()
40+
assert.ErrorIs(t, compound.Validate(), errBadFirstPacket)
41+
assert.Equal(t, 2, len(compound))
4342

44-
if got, want := err, errBadFirstPacket; !errors.Is(got, want) {
45-
t.Fatalf("Unmarshal(badcompound) err=%v, want %v", got, want)
46-
}
43+
_, ok := compound[0].(*Goodbye)
44+
assert.True(t, ok)
4745

48-
if got, want := len(compound), 2; got != want {
49-
t.Fatalf("Unmarshal(badcompound) len=%d, want %d", got, want)
50-
}
51-
if _, ok := compound[0].(*Goodbye); !ok {
52-
t.Fatalf("Unmarshal(badcompound); first packet = %#v, want Goodbye", compound[0])
53-
}
54-
if _, ok := compound[1].(*PictureLossIndication); !ok {
55-
t.Fatalf("Unmarshal(badcompound); second packet = %#v, want PictureLossIndication", compound[1])
56-
}
46+
_, ok = compound[1].(*PictureLossIndication)
47+
assert.True(t, ok)
5748
}
5849

5950
func TestValidPacket(t *testing.T) {
@@ -135,9 +126,7 @@ func TestValidPacket(t *testing.T) {
135126
Err: nil,
136127
},
137128
} {
138-
if got, want := test.Packet.Validate(), test.Err; !errors.Is(got, want) {
139-
t.Fatalf("Valid(%s) = %v, want %v", test.Name, got, want)
140-
}
129+
assert.ErrorIsf(t, test.Packet.Validate(), test.Err, "Validate(%s)", test.Name)
141130
}
142131
}
143132

@@ -214,16 +203,11 @@ func TestCNAME(t *testing.T) {
214203
Text: "cname",
215204
},
216205
} {
217-
if got, want := test.Packet.Validate(), test.Err; !errors.Is(got, want) {
218-
t.Fatalf("Valid(%s) = %v, want %v", test.Name, got, want)
219-
}
206+
assert.ErrorIsf(t, test.Packet.Validate(), test.Err, "Validate(%s)", test.Name)
207+
220208
name, err := test.Packet.CNAME()
221-
if got, want := err, test.Err; !errors.Is(got, want) {
222-
t.Fatalf("CNAME(%s) = %v, want %v", test.Name, got, want)
223-
}
224-
if got, want := name, test.Text; got != want {
225-
t.Fatalf("CNAME(%s) = %v, want %v", test.Name, got, want)
226-
}
209+
assert.ErrorIsf(t, err, test.Err, "CNAME(%s)", test.Name)
210+
assert.Equalf(t, test.Text, name, "CNAME(%s)", test.Name)
227211
}
228212
}
229213

@@ -254,25 +238,16 @@ func TestCompoundPacketRoundTrip(t *testing.T) {
254238
},
255239
} {
256240
data, err := test.Packet.Marshal()
257-
if got, want := err, test.Err; !errors.Is(got, want) {
258-
t.Fatalf("Marshal(%v) err = %v, want nil", test.Name, err)
259-
}
241+
assert.ErrorIsf(t, err, test.Err, "Marshal(%v)", test.Name)
260242
if err != nil {
261243
continue
262244
}
263245

264246
var c CompoundPacket
265-
if err = c.Unmarshal(data); err != nil {
266-
t.Fatalf("Unmarshal(%v) err = %v, want nil", test.Name, err)
267-
}
247+
assert.NoErrorf(t, c.Unmarshal(data), "Unmarshal(%v)", test.Name)
268248

269249
data2, err := c.Marshal()
270-
if err != nil {
271-
t.Fatalf("Marshal(%v) err = %v, want nil", test.Name, err)
272-
}
273-
274-
if got, want := data, data2; !reflect.DeepEqual(got, want) {
275-
t.Fatalf("Unmarshal(Marshal(%v)) = %v, want %v", test.Name, got, want)
276-
}
250+
assert.NoErrorf(t, err, "Marshal(%v)", test.Name)
251+
assert.Equalf(t, data, data2, "Marshal(%v) mismatch", test.Name)
277252
}
278253
}

extended_report_test.go

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ package rtcp
55

66
import (
77
"fmt"
8-
"reflect"
98
"testing"
9+
10+
"github.com/stretchr/testify/assert"
1011
)
1112

1213
// Assert that ExtendedReport is a Packet.
@@ -225,17 +226,11 @@ func TestEncode(t *testing.T) {
225226
expected := encodedPacket()
226227
packet := testPacket()
227228
rawPacket, err := packet.Marshal()
228-
if err != nil {
229-
t.Fatalf("Error marshaling packet: %v", err)
230-
}
231-
if len(rawPacket) != len(expected) {
232-
t.Fatalf("Encoded message is %d bytes; expected is %d", len(rawPacket), len(expected))
233-
}
229+
assert.NoError(t, err)
230+
assert.Equal(t, len(expected), len(rawPacket), "Encoded message length does not match expected length")
234231

235232
for i := 0; i < len(rawPacket); i++ {
236-
if rawPacket[i] != expected[i] {
237-
t.Errorf("Byte %d of encoded packet does not match: expected 0x%02X, got 0x%02X", i, expected[i], rawPacket[i])
238-
}
233+
assert.Equalf(t, expected[i], rawPacket[i], "Byte %d of encoded packet does not match", i)
239234
}
240235
}
241236

@@ -246,40 +241,26 @@ func TestDecode(t *testing.T) {
246241
// We need to make sure the header has been set up correctly
247242
// before we test for equality
248243
extendedReports, ok := expected.(*ExtendedReport)
249-
if !ok {
250-
t.Fatal("Failed to cast")
251-
}
244+
assert.True(t, ok)
252245

253246
for _, p := range extendedReports.Reports {
254247
p.setupBlockHeader()
255248
}
256249

257250
report := new(ExtendedReport)
258251
err := report.Unmarshal(encoded)
259-
if err != nil {
260-
t.Fatalf("Error unmarshaling packet: %v", err)
261-
}
262-
263-
if !reflect.DeepEqual(report, expected) {
264-
t.Errorf("(deep equal) Decoded packet does not match expected packet")
265-
}
252+
assert.NoError(t, err)
253+
assert.Equal(t, expected, report)
266254

267255
pktStringer, ok := expected.(fmt.Stringer)
268-
if !ok {
269-
t.Fatal("Failed to cast")
270-
}
271-
272-
if report.String() != pktStringer.String() {
273-
t.Errorf("(string compare) Decoded packet does not match expected packet")
274-
}
256+
assert.True(t, ok)
257+
assert.Equal(t, report.String(), pktStringer.String(), "Decoded packet does not match expected packet")
275258

276259
var includeSenderSSRC bool
277260
for _, ssrc := range report.DestinationSSRC() {
278261
if ssrc == report.SenderSSRC {
279262
includeSenderSSRC = true
280263
}
281264
}
282-
if !includeSenderSSRC {
283-
t.Errorf("DestinationSSRC does not include the SenderSSRC")
284-
}
265+
assert.True(t, includeSenderSSRC, "DestinationSSRC does not include the SenderSSRC")
285266
}

full_intra_request_test.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
package rtcp
55

66
import (
7-
"errors"
8-
"reflect"
97
"testing"
8+
9+
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func TestFullIntraRequestUnmarshal(t *testing.T) {
@@ -141,16 +141,12 @@ func TestFullIntraRequestUnmarshal(t *testing.T) {
141141
} {
142142
var fir FullIntraRequest
143143
err := fir.Unmarshal(test.Data)
144-
if got, want := err, test.WantError; !errors.Is(got, want) {
145-
t.Fatalf("Unmarshal %q rr: err = %v, want %v", test.Name, got, want)
146-
}
144+
assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q rr mismatch", test.Name)
147145
if err != nil {
148146
continue
149147
}
150148

151-
if got, want := fir, test.Want; !reflect.DeepEqual(got, want) {
152-
t.Fatalf("Unmarshal %q rr: got %v, want %v", test.Name, got, want)
153-
}
149+
assert.Equalf(t, test.Want, fir, "Unmarshal %q rr mismatch", test.Name)
154150
}
155151
}
156152

@@ -184,21 +180,14 @@ func TestFullIntraRequestRoundTrip(t *testing.T) {
184180
},
185181
} {
186182
data, err := test.Packet.Marshal()
187-
if got, want := err, test.WantError; !errors.Is(got, want) {
188-
t.Fatalf("Marshal %q: err = %v, want %v", test.Name, got, want)
189-
}
183+
assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name)
190184
if err != nil {
191185
continue
192186
}
193187

194188
var decoded FullIntraRequest
195-
if err := decoded.Unmarshal(data); err != nil {
196-
t.Fatalf("Unmarshal %q: %v", test.Name, err)
197-
}
198-
199-
if got, want := decoded, test.Packet; !reflect.DeepEqual(got, want) {
200-
t.Fatalf("%q rr round trip: got %#v, want %#v", test.Name, got, want)
201-
}
189+
assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name)
190+
assert.Equalf(t, test.Packet, decoded, "%q rr header mismatch", test.Name)
202191
}
203192
}
204193

@@ -232,15 +221,11 @@ func TestFullIntraRequestUnmarshalHeader(t *testing.T) {
232221
} {
233222
var fir FullIntraRequest
234223
err := fir.Unmarshal(test.Data)
235-
if got, want := err, test.WantError; !errors.Is(got, want) {
236-
t.Fatalf("Unmarshal header %q rr: err = %v, want %v", test.Name, got, want)
237-
}
224+
assert.ErrorIsf(t, err, test.WantError, "Unmarshal header %q rr mismatch", test.Name)
238225
if err != nil {
239226
continue
240227
}
241228

242-
if got, want := fir.Header(), test.Want; !reflect.DeepEqual(got, want) {
243-
t.Fatalf("Unmarshal header %q rr: got %v, want %v", test.Name, got, want)
244-
}
229+
assert.Equalf(t, test.Want, fir.Header(), "Unmarshal header %q rr mismatch", test.Name)
245230
}
246231
}

0 commit comments

Comments
 (0)