Skip to content

Commit 6925a30

Browse files
authored
[chore]: enable len and empty rules from testifylint (#11021)
#### Description Testifylint is a linter that provides best practices with the use of testify. This PR enables [len](https://github.com/Antonboom/testifylint?tab=readme-ov-file#len) and [empty](https://github.com/Antonboom/testifylint?tab=readme-ov-file#empty) rules from [testifylint](https://github.com/Antonboom/testifylint) It also adds testifylint as tool to use with a make command Signed-off-by: Matthieu MOREL <[email protected]>
1 parent e599957 commit 6925a30

File tree

37 files changed

+130
-118
lines changed

37 files changed

+130
-118
lines changed

.golangci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,12 @@ linters-settings:
125125
# TODO: enable all rules
126126
disable:
127127
- compares
128-
- empty
129128
- error-is-as
130129
- error-nil
131130
- expected-actual
132131
- float-compare
133132
- formatter
134133
- go-require
135-
- len
136134
- negative-positive
137135
- nil-compare
138136
- require-error

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ gotest-with-cover:
5656
@$(MAKE) for-all-target TARGET="test-with-cover"
5757
$(GOCMD) tool covdata textfmt -i=./coverage/unit -o ./coverage.txt
5858

59+
.PHONY: gotestifylint-fix
60+
gotestifylint-fix:
61+
$(MAKE) for-all-target TARGET="testifylint-fix"
62+
5963
.PHONY: goporto
6064
goporto: $(PORTO)
6165
$(PORTO) -w --include-internal --skip-dirs "^cmd/mdatagen/third_party$$" ./

Makefile.Common

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ MULTIMOD := $(TOOLS_BIN_DIR)/multimod
3636
PORTO := $(TOOLS_BIN_DIR)/porto
3737
SEMCONVGEN := $(TOOLS_BIN_DIR)/semconvgen
3838
SEMCONVKIT := $(TOOLS_BIN_DIR)/semconvkit
39+
TESTIFYLINT := $(TOOLS_BIN_DIR)/testifylint
40+
41+
TESTIFYLINT_OPT?= --enable-all --disable=compares,error-is-as,error-nil,expected-actual,float-compare,formatter,go-require,negative-positive,nil-compare,require-error
3942

4043
.PHONY: install-tools
4144
install-tools: $(TOOLS_BIN_NAMES)
@@ -88,3 +91,9 @@ impi: $(IMPI)
8891
.PHONY: moddownload
8992
moddownload:
9093
$(GOCMD) mod download
94+
95+
.PHONY: testifylint-fix
96+
testifylint-fix:$(TESTIFYLINT)
97+
@echo "running $(TESTIFYLINT)"
98+
@$(TESTIFYLINT) $(TESTIFYLINT_OPT) -fix ./...
99+

cmd/builder/internal/builder/config_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ func TestNewBuiltinConfig(t *testing.T) {
171171
// Unlike the config initialized in NewDefaultConfig(), we expect
172172
// the builtin default to be practically useful, so there must be
173173
// a set of modules present.
174-
assert.NotZero(t, len(cfg.Receivers))
175-
assert.NotZero(t, len(cfg.Exporters))
176-
assert.NotZero(t, len(cfg.Extensions))
177-
assert.NotZero(t, len(cfg.Processors))
174+
assert.NotEmpty(t, cfg.Receivers)
175+
assert.NotEmpty(t, cfg.Exporters)
176+
assert.NotEmpty(t, cfg.Extensions)
177+
assert.NotEmpty(t, cfg.Processors)
178178
}
179179

180180
func TestSkipGoValidation(t *testing.T) {

connector/connectorprofiles/profiles_router_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ func TestProfilessRouterConsumer(t *testing.T) {
120120
assert.Len(t, rcs, 2)
121121
assert.ElementsMatch(t, []component.ID{fooID, barID}, rcs)
122122

123-
assert.Len(t, foo.AllProfiles(), 0)
124-
assert.Len(t, bar.AllProfiles(), 0)
123+
assert.Empty(t, foo.AllProfiles())
124+
assert.Empty(t, bar.AllProfiles())
125125

126126
both, err := r.Consumer(fooID, barID)
127127
assert.NotNil(t, both)

connector/forwardconnector/forward_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestForward(t *testing.T) {
5757
assert.NoError(t, metricsToMetrics.Shutdown(ctx))
5858
assert.NoError(t, logsToLogs.Shutdown(ctx))
5959

60-
assert.Equal(t, 1, len(tracesSink.AllTraces()))
61-
assert.Equal(t, 2, len(metricsSink.AllMetrics()))
62-
assert.Equal(t, 3, len(logsSink.AllLogs()))
60+
assert.Len(t, tracesSink.AllTraces(), 1)
61+
assert.Len(t, metricsSink.AllMetrics(), 2)
62+
assert.Len(t, logsSink.AllLogs(), 3)
6363
}

connector/logs_router_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func TestLogsRouterConsumers(t *testing.T) {
119119
assert.Len(t, rcs, 2)
120120
assert.ElementsMatch(t, []component.ID{fooID, barID}, rcs)
121121

122-
assert.Len(t, foo.AllLogs(), 0)
123-
assert.Len(t, bar.AllLogs(), 0)
122+
assert.Empty(t, foo.AllLogs())
123+
assert.Empty(t, bar.AllLogs())
124124

125125
both, err := r.Consumer(fooID, barID)
126126
assert.NotNil(t, both)

connector/metrics_router_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func TestMetricsRouterConsumers(t *testing.T) {
119119
assert.Len(t, rcs, 2)
120120
assert.ElementsMatch(t, []component.ID{fooID, barID}, rcs)
121121

122-
assert.Len(t, foo.AllMetrics(), 0)
123-
assert.Len(t, bar.AllMetrics(), 0)
122+
assert.Empty(t, foo.AllMetrics())
123+
assert.Empty(t, bar.AllMetrics())
124124

125125
both, err := r.Consumer(fooID, barID)
126126
assert.NotNil(t, both)

connector/traces_router_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func TestTracesRouterConsumer(t *testing.T) {
119119
assert.Len(t, rcs, 2)
120120
assert.ElementsMatch(t, []component.ID{fooID, barID}, rcs)
121121

122-
assert.Len(t, foo.AllTraces(), 0)
123-
assert.Len(t, bar.AllTraces(), 0)
122+
assert.Empty(t, foo.AllTraces())
123+
assert.Empty(t, bar.AllTraces())
124124

125125
both, err := r.Consumer(fooID, barID)
126126
assert.NotNil(t, both)

consumer/consumertest/sink_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestTracesSink(t *testing.T) {
2828
assert.Equal(t, want, sink.AllTraces())
2929
assert.Equal(t, len(want), sink.SpanCount())
3030
sink.Reset()
31-
assert.Equal(t, 0, len(sink.AllTraces()))
31+
assert.Empty(t, sink.AllTraces())
3232
assert.Equal(t, 0, sink.SpanCount())
3333
}
3434

@@ -43,7 +43,7 @@ func TestMetricsSink(t *testing.T) {
4343
assert.Equal(t, want, sink.AllMetrics())
4444
assert.Equal(t, 2*len(want), sink.DataPointCount())
4545
sink.Reset()
46-
assert.Equal(t, 0, len(sink.AllMetrics()))
46+
assert.Empty(t, sink.AllMetrics())
4747
assert.Equal(t, 0, sink.DataPointCount())
4848
}
4949

@@ -58,7 +58,7 @@ func TestLogsSink(t *testing.T) {
5858
assert.Equal(t, want, sink.AllLogs())
5959
assert.Equal(t, len(want), sink.LogRecordCount())
6060
sink.Reset()
61-
assert.Equal(t, 0, len(sink.AllLogs()))
61+
assert.Empty(t, sink.AllLogs())
6262
assert.Equal(t, 0, sink.LogRecordCount())
6363
}
6464

@@ -72,5 +72,5 @@ func TestProfilesSink(t *testing.T) {
7272
}
7373
assert.Equal(t, want, sink.AllProfiles())
7474
sink.Reset()
75-
assert.Equal(t, 0, len(sink.AllProfiles()))
75+
assert.Empty(t, sink.AllProfiles())
7676
}

exporter/exporterhelper/logs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func checkWrapSpanForLogsExporter(t *testing.T, sr *tracetest.SpanRecorder, trac
418418

419419
// Inspection time!
420420
gotSpanData := sr.Ended()
421-
require.Equal(t, numRequests+1, len(gotSpanData))
421+
require.Len(t, gotSpanData, numRequests+1)
422422

423423
parentSpan := gotSpanData[numRequests]
424424
require.Equalf(t, fakeLogsParentSpanName, parentSpan.Name(), "SpanData %v", parentSpan)

exporter/exporterhelper/metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func checkWrapSpanForMetricsExporter(t *testing.T, sr *tracetest.SpanRecorder, t
424424

425425
// Inspection time!
426426
gotSpanData := sr.Ended()
427-
require.Equal(t, numRequests+1, len(gotSpanData))
427+
require.Len(t, gotSpanData, numRequests+1)
428428

429429
parentSpan := gotSpanData[numRequests]
430430
require.Equalf(t, fakeMetricsParentSpanName, parentSpan.Name(), "SpanData %v", parentSpan)

exporter/exporterhelper/traces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func checkWrapSpanForTracesExporter(t *testing.T, sr *tracetest.SpanRecorder, tr
426426

427427
// Inspection time!
428428
gotSpanData := sr.Ended()
429-
require.Equal(t, numRequests+1, len(gotSpanData))
429+
require.Len(t, gotSpanData, numRequests+1)
430430

431431
parentSpan := gotSpanData[numRequests]
432432
require.Equalf(t, fakeTraceParentSpanName, parentSpan.Name(), "SpanData %v", parentSpan)

exporter/otlpexporter/otlp_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func TestSendTraces(t *testing.T) {
300300

301301
md := rcv.getMetadata()
302302
require.EqualValues(t, md.Get("header"), expectedHeader)
303-
require.Equal(t, len(md.Get("User-Agent")), 1)
303+
require.Len(t, md.Get("User-Agent"), 1)
304304
require.Contains(t, md.Get("User-Agent")[0], "Collector/1.2.3test")
305305

306306
// Return partial success
@@ -472,7 +472,7 @@ func TestSendMetrics(t *testing.T) {
472472

473473
mdata := rcv.getMetadata()
474474
require.EqualValues(t, mdata.Get("header"), expectedHeader)
475-
require.Equal(t, len(mdata.Get("User-Agent")), 1)
475+
require.Len(t, mdata.Get("User-Agent"), 1)
476476
require.Contains(t, mdata.Get("User-Agent")[0], "Collector/1.2.3test")
477477

478478
st := status.New(codes.InvalidArgument, "Invalid argument")
@@ -763,7 +763,7 @@ func TestSendLogData(t *testing.T) {
763763
assert.EqualValues(t, ld, rcv.getLastRequest())
764764

765765
md := rcv.getMetadata()
766-
require.Equal(t, len(md.Get("User-Agent")), 1)
766+
require.Len(t, md.Get("User-Agent"), 1)
767767
require.Contains(t, md.Get("User-Agent")[0], "Collector/1.2.3test")
768768

769769
st := status.New(codes.InvalidArgument, "Invalid argument")

internal/e2e/status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func Test_ComponentStatusReporting_SharedInstance(t *testing.T) {
121121
err = s.Shutdown(context.Background())
122122
require.NoError(t, err)
123123

124-
require.Equal(t, 2, len(eventsReceived))
124+
require.Len(t, eventsReceived, 2)
125125

126126
for instanceID, events := range eventsReceived {
127127
pipelineIDs := ""

internal/sharedcomponent/sharedcomponent_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ type baseComponent struct {
2626

2727
func TestNewMap(t *testing.T) {
2828
comps := NewMap[component.ID, *baseComponent]()
29-
assert.Len(t, comps.components, 0)
29+
assert.Empty(t, comps.components)
3030
}
3131

3232
func TestNewSharedComponentsCreateError(t *testing.T) {
3333
comps := NewMap[component.ID, *baseComponent]()
34-
assert.Len(t, comps.components, 0)
34+
assert.Empty(t, comps.components)
3535
myErr := errors.New("my error")
3636
_, err := comps.LoadOrStore(
3737
id,
3838
func() (*baseComponent, error) { return nil, myErr },
3939
newNopTelemetrySettings(),
4040
)
4141
assert.ErrorIs(t, err, myErr)
42-
assert.Len(t, comps.components, 0)
42+
assert.Empty(t, comps.components)
4343
}
4444

4545
func TestSharedComponentsLoadOrStore(t *testing.T) {
@@ -65,7 +65,7 @@ func TestSharedComponentsLoadOrStore(t *testing.T) {
6565

6666
// Shutdown nop will remove
6767
assert.NoError(t, got.Shutdown(context.Background()))
68-
assert.Len(t, comps.components, 0)
68+
assert.Empty(t, comps.components)
6969
gotThird, err := comps.LoadOrStore(
7070
id,
7171
func() (*baseComponent, error) { return nop, nil },
@@ -200,7 +200,7 @@ func TestReportStatusOnStartShutdown(t *testing.T) {
200200
require.Equal(t, tc.shutdownErr, err)
201201
}
202202

203-
require.Equal(t, tc.expectedNumReporterInstances, len(reportedStatuses))
203+
require.Len(t, reportedStatuses, tc.expectedNumReporterInstances)
204204

205205
for _, actualStatuses := range reportedStatuses {
206206
require.Equal(t, tc.expectedStatuses, actualStatuses)

internal/testutil/testutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ func createExclusionsList(exclusionsText string, t testing.TB) []portpair {
149149
var exclusions []portpair
150150

151151
parts := strings.Split(exclusionsText, "--------")
152-
require.Equal(t, len(parts), 3)
152+
require.Len(t, parts, 3)
153153
portsText := strings.Split(parts[2], "*")
154154
require.Greater(t, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions."
155155
lines := strings.Split(portsText[0], "\n")
156156
for _, line := range lines {
157157
if strings.TrimSpace(line) != "" {
158158
entries := strings.Fields(strings.TrimSpace(line))
159-
require.Equal(t, len(entries), 2)
159+
require.Len(t, entries, 2)
160160
pair := portpair{entries[0], entries[1]}
161161
exclusions = append(exclusions, pair)
162162
}

internal/testutil/testutil_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Start Port End Port
6868
* - Administered port exclusions.
6969
`
7070
exclusions := createExclusionsList(exclusionsText, t)
71-
require.Equal(t, len(exclusions), 2)
71+
require.Len(t, exclusions, 2)
7272

7373
emptyExclusions := createExclusionsList(emptyExclusionsText, t)
74-
require.Equal(t, len(emptyExclusions), 0)
74+
require.Empty(t, emptyExclusions)
7575
}

internal/tools/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.1
55
toolchain go1.22.6
66

77
require (
8+
github.com/Antonboom/testifylint v1.4.3
89
github.com/a8m/envsubst v1.4.2
910
github.com/client9/misspell v0.3.4
1011
github.com/golangci/golangci-lint v1.60.1
@@ -29,7 +30,6 @@ require (
2930
github.com/Abirdcfly/dupword v0.0.14 // indirect
3031
github.com/Antonboom/errname v0.1.13 // indirect
3132
github.com/Antonboom/nilnil v0.1.9 // indirect
32-
github.com/Antonboom/testifylint v1.4.3 // indirect
3333
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
3434
github.com/Crocmagnon/fatcontext v0.4.0 // indirect
3535
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 // indirect

internal/tools/tools.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package tools // import "go.opentelemetry.io/collector/internal/tools"
1111
// This ensures that all systems use the same version of tools in addition to regular dependencies.
1212

1313
import (
14+
_ "github.com/Antonboom/testifylint"
1415
_ "github.com/a8m/envsubst/cmd/envsubst"
1516
_ "github.com/client9/misspell/cmd/misspell"
1617
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"

otelcol/buffered_core_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func Test_bufferedCore_Write(t *testing.T) {
7272
e,
7373
fields,
7474
}
75-
require.Equal(t, 1, len(bc.logs))
75+
require.Len(t, bc.logs, 1)
7676
require.Equal(t, expected, bc.logs[0])
7777
}
7878

otelcol/collector_core_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func Test_collectorCore_Write(t *testing.T) {
7272
e,
7373
fields,
7474
}
75-
require.Equal(t, 1, len(cc.core.(*bufferedCore).logs))
75+
require.Len(t, cc.core.(*bufferedCore).logs, 1)
7676
require.Equal(t, expected, cc.core.(*bufferedCore).logs[0])
7777
}
7878

otelcol/otelcoltest/nop_factories_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ func TestNopFactories(t *testing.T) {
1717
nopFactories, err := NopFactories()
1818
require.NoError(t, err)
1919

20-
require.Equal(t, 1, len(nopFactories.Receivers))
20+
require.Len(t, nopFactories.Receivers, 1)
2121
nopReceiverFactory, ok := nopFactories.Receivers[nopType]
2222
require.True(t, ok)
2323
require.Equal(t, nopType, nopReceiverFactory.Type())
2424

25-
require.Equal(t, 1, len(nopFactories.Processors))
25+
require.Len(t, nopFactories.Processors, 1)
2626
nopProcessorFactory, ok := nopFactories.Processors[nopType]
2727
require.True(t, ok)
2828
require.Equal(t, nopType, nopProcessorFactory.Type())
2929

30-
require.Equal(t, 1, len(nopFactories.Exporters))
30+
require.Len(t, nopFactories.Exporters, 1)
3131
nopExporterFactory, ok := nopFactories.Exporters[nopType]
3232
require.True(t, ok)
3333
require.Equal(t, nopType, nopExporterFactory.Type())
3434

35-
require.Equal(t, 1, len(nopFactories.Extensions))
35+
require.Len(t, nopFactories.Extensions, 1)
3636
nopExtensionFactory, ok := nopFactories.Extensions[nopType]
3737
require.True(t, ok)
3838
require.Equal(t, nopType, nopExtensionFactory.Type())
3939

40-
require.Equal(t, 1, len(nopFactories.Connectors))
40+
require.Len(t, nopFactories.Connectors, 1)
4141
nopConnectorFactory, ok := nopFactories.Connectors[nopType]
4242
require.True(t, ok)
4343
require.Equal(t, nopType, nopConnectorFactory.Type())

pdata/pcommon/map_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func TestMap_Range(t *testing.T) {
320320
delete(rawMap, k)
321321
return true
322322
})
323-
assert.EqualValues(t, 0, len(rawMap))
323+
assert.Empty(t, rawMap)
324324
}
325325

326326
func TestMap_FromRaw(t *testing.T) {

pdata/pcommon/slice_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestSlice_EnsureCapacity(t *testing.T) {
7777
for i := 0; i < es.Len(); i++ {
7878
expectedEs[es.At(i).getOrig()] = true
7979
}
80-
assert.Equal(t, es.Len(), len(expectedEs))
80+
assert.Len(t, expectedEs, es.Len())
8181
es.EnsureCapacity(ensureSmallLen)
8282
assert.Less(t, ensureSmallLen, es.Len())
8383
foundEs := make(map[*otlpcommon.AnyValue]bool, es.Len())
@@ -89,7 +89,7 @@ func TestSlice_EnsureCapacity(t *testing.T) {
8989
// Test ensure larger capacity
9090
const ensureLargeLen = 9
9191
oldLen := es.Len()
92-
assert.Equal(t, oldLen, len(expectedEs))
92+
assert.Len(t, expectedEs, oldLen)
9393
es.EnsureCapacity(ensureLargeLen)
9494
assert.Equal(t, ensureLargeLen, cap(*es.getOrig()))
9595
}

0 commit comments

Comments
 (0)