Skip to content

Commit

Permalink
[chore]: enable len rule from testifylint
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 committed Sep 6, 2024
1 parent d19ebe4 commit 40f908a
Show file tree
Hide file tree
Showing 27 changed files with 103 additions and 90 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ linters-settings:
- float-compare
- formatter
- go-require
- len
- negative-positive
- nil-compare
- require-error
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ gotest-with-cover:
@$(MAKE) for-all-target TARGET="test-with-cover"
$(GOCMD) tool covdata textfmt -i=./coverage/unit -o ./coverage.txt

.PHONY: gotestifylint-fix
gotestifylint-fix:
$(MAKE) for-all-target TARGET="testifylint-fix"

.PHONY: goporto
goporto: $(PORTO)
$(PORTO) -w --include-internal --skip-dirs "^cmd/mdatagen/third_party$$" ./
Expand Down
9 changes: 9 additions & 0 deletions Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ MULTIMOD := $(TOOLS_BIN_DIR)/multimod
PORTO := $(TOOLS_BIN_DIR)/porto
SEMCONVGEN := $(TOOLS_BIN_DIR)/semconvgen
SEMCONVKIT := $(TOOLS_BIN_DIR)/semconvkit
TESTIFYLINT := $(TOOLS_BIN_DIR)/testifylint

TESTIFYLINT_OPT?= --enable-all --disable=compares,empty,error-is-as,error-nil,expected-actual,float-compare,formatter,go-require,negative-positive,nil-compare,require-error

.PHONY: install-tools
install-tools: $(TOOLS_BIN_NAMES)
Expand Down Expand Up @@ -88,3 +91,9 @@ impi: $(IMPI)
.PHONY: moddownload
moddownload:
$(GOCMD) mod download

.PHONY: testifylint-fix
testifylint-fix:$(TESTIFYLINT)
@echo "running $(TESTIFYLINT)"
@$(TESTIFYLINT) $(TESTIFYLINT_OPT) -fix ./...

6 changes: 3 additions & 3 deletions connector/forwardconnector/forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestForward(t *testing.T) {
assert.NoError(t, metricsToMetrics.Shutdown(ctx))
assert.NoError(t, logsToLogs.Shutdown(ctx))

assert.Equal(t, 1, len(tracesSink.AllTraces()))
assert.Equal(t, 2, len(metricsSink.AllMetrics()))
assert.Equal(t, 3, len(logsSink.AllLogs()))
assert.Len(t, tracesSink.AllTraces(), 1)
assert.Len(t, metricsSink.AllMetrics(), 2)
assert.Len(t, logsSink.AllLogs(), 3)
}
8 changes: 4 additions & 4 deletions consumer/consumertest/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestTracesSink(t *testing.T) {
assert.Equal(t, want, sink.AllTraces())
assert.Equal(t, len(want), sink.SpanCount())
sink.Reset()
assert.Equal(t, 0, len(sink.AllTraces()))
assert.Len(t, sink.AllTraces(), 0)
assert.Equal(t, 0, sink.SpanCount())
}

Expand All @@ -43,7 +43,7 @@ func TestMetricsSink(t *testing.T) {
assert.Equal(t, want, sink.AllMetrics())
assert.Equal(t, 2*len(want), sink.DataPointCount())
sink.Reset()
assert.Equal(t, 0, len(sink.AllMetrics()))
assert.Len(t, sink.AllMetrics(), 0)
assert.Equal(t, 0, sink.DataPointCount())
}

Expand All @@ -58,7 +58,7 @@ func TestLogsSink(t *testing.T) {
assert.Equal(t, want, sink.AllLogs())
assert.Equal(t, len(want), sink.LogRecordCount())
sink.Reset()
assert.Equal(t, 0, len(sink.AllLogs()))
assert.Len(t, sink.AllLogs(), 0)
assert.Equal(t, 0, sink.LogRecordCount())
}

Expand All @@ -72,5 +72,5 @@ func TestProfilesSink(t *testing.T) {
}
assert.Equal(t, want, sink.AllProfiles())
sink.Reset()
assert.Equal(t, 0, len(sink.AllProfiles()))
assert.Len(t, sink.AllProfiles(), 0)
}
2 changes: 1 addition & 1 deletion exporter/exporterhelper/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func checkWrapSpanForLogsExporter(t *testing.T, sr *tracetest.SpanRecorder, trac

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

parentSpan := gotSpanData[numRequests]
require.Equalf(t, fakeLogsParentSpanName, parentSpan.Name(), "SpanData %v", parentSpan)
Expand Down
2 changes: 1 addition & 1 deletion exporter/exporterhelper/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func checkWrapSpanForMetricsExporter(t *testing.T, sr *tracetest.SpanRecorder, t

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

parentSpan := gotSpanData[numRequests]
require.Equalf(t, fakeMetricsParentSpanName, parentSpan.Name(), "SpanData %v", parentSpan)
Expand Down
2 changes: 1 addition & 1 deletion exporter/exporterhelper/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func checkWrapSpanForTracesExporter(t *testing.T, sr *tracetest.SpanRecorder, tr

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

parentSpan := gotSpanData[numRequests]
require.Equalf(t, fakeTraceParentSpanName, parentSpan.Name(), "SpanData %v", parentSpan)
Expand Down
6 changes: 3 additions & 3 deletions exporter/otlpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestSendTraces(t *testing.T) {

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

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

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

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

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

st := status.New(codes.InvalidArgument, "Invalid argument")
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func Test_ComponentStatusReporting_SharedInstance(t *testing.T) {
err = s.Shutdown(context.Background())
require.NoError(t, err)

require.Equal(t, 2, len(eventsReceived))
require.Len(t, eventsReceived, 2)

for instanceID, events := range eventsReceived {
pipelineIDs := ""
Expand Down
2 changes: 1 addition & 1 deletion internal/sharedcomponent/sharedcomponent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestReportStatusOnStartShutdown(t *testing.T) {
require.Equal(t, tc.shutdownErr, err)
}

require.Equal(t, tc.expectedNumReporterInstances, len(reportedStatuses))
require.Len(t, reportedStatuses, tc.expectedNumReporterInstances)

for _, actualStatuses := range reportedStatuses {
require.Equal(t, tc.expectedStatuses, actualStatuses)
Expand Down
4 changes: 2 additions & 2 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ func createExclusionsList(exclusionsText string, t testing.TB) []portpair {
var exclusions []portpair

parts := strings.Split(exclusionsText, "--------")
require.Equal(t, len(parts), 3)
require.Len(t, parts, 3)
portsText := strings.Split(parts[2], "*")
require.Greater(t, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions."
lines := strings.Split(portsText[0], "\n")
for _, line := range lines {
if strings.TrimSpace(line) != "" {
entries := strings.Fields(strings.TrimSpace(line))
require.Equal(t, len(entries), 2)
require.Len(t, entries, 2)
pair := portpair{entries[0], entries[1]}
exclusions = append(exclusions, pair)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ Start Port End Port
* - Administered port exclusions.
`
exclusions := createExclusionsList(exclusionsText, t)
require.Equal(t, len(exclusions), 2)
require.Len(t, exclusions, 2)

emptyExclusions := createExclusionsList(emptyExclusionsText, t)
require.Equal(t, len(emptyExclusions), 0)
require.Len(t, emptyExclusions, 0)
}
2 changes: 1 addition & 1 deletion internal/tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.1
toolchain go1.22.6

require (
github.com/Antonboom/testifylint v1.4.3
github.com/a8m/envsubst v1.4.2
github.com/client9/misspell v0.3.4
github.com/golangci/golangci-lint v1.60.1
Expand All @@ -29,7 +30,6 @@ require (
github.com/Abirdcfly/dupword v0.0.14 // indirect
github.com/Antonboom/errname v0.1.13 // indirect
github.com/Antonboom/nilnil v0.1.9 // indirect
github.com/Antonboom/testifylint v1.4.3 // indirect
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
github.com/Crocmagnon/fatcontext v0.4.0 // indirect
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 // indirect
Expand Down
1 change: 1 addition & 0 deletions internal/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package tools // import "go.opentelemetry.io/collector/internal/tools"
// This ensures that all systems use the same version of tools in addition to regular dependencies.

import (
_ "github.com/Antonboom/testifylint"
_ "github.com/a8m/envsubst/cmd/envsubst"
_ "github.com/client9/misspell/cmd/misspell"
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
Expand Down
2 changes: 1 addition & 1 deletion otelcol/buffered_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_bufferedCore_Write(t *testing.T) {
e,
fields,
}
require.Equal(t, 1, len(bc.logs))
require.Len(t, bc.logs, 1)
require.Equal(t, expected, bc.logs[0])
}

Expand Down
2 changes: 1 addition & 1 deletion otelcol/collector_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_collectorCore_Write(t *testing.T) {
e,
fields,
}
require.Equal(t, 1, len(cc.core.(*bufferedCore).logs))
require.Len(t, cc.core.(*bufferedCore).logs, 1)
require.Equal(t, expected, cc.core.(*bufferedCore).logs[0])
}

Expand Down
10 changes: 5 additions & 5 deletions otelcol/otelcoltest/nop_factories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ func TestNopFactories(t *testing.T) {
nopFactories, err := NopFactories()
require.NoError(t, err)

require.Equal(t, 1, len(nopFactories.Receivers))
require.Len(t, nopFactories.Receivers, 1)
nopReceiverFactory, ok := nopFactories.Receivers[nopType]
require.True(t, ok)
require.Equal(t, nopType, nopReceiverFactory.Type())

require.Equal(t, 1, len(nopFactories.Processors))
require.Len(t, nopFactories.Processors, 1)
nopProcessorFactory, ok := nopFactories.Processors[nopType]
require.True(t, ok)
require.Equal(t, nopType, nopProcessorFactory.Type())

require.Equal(t, 1, len(nopFactories.Exporters))
require.Len(t, nopFactories.Exporters, 1)
nopExporterFactory, ok := nopFactories.Exporters[nopType]
require.True(t, ok)
require.Equal(t, nopType, nopExporterFactory.Type())

require.Equal(t, 1, len(nopFactories.Extensions))
require.Len(t, nopFactories.Extensions, 1)
nopExtensionFactory, ok := nopFactories.Extensions[nopType]
require.True(t, ok)
require.Equal(t, nopType, nopExtensionFactory.Type())

require.Equal(t, 1, len(nopFactories.Connectors))
require.Len(t, nopFactories.Connectors, 1)
nopConnectorFactory, ok := nopFactories.Connectors[nopType]
require.True(t, ok)
require.Equal(t, nopType, nopConnectorFactory.Type())
Expand Down
2 changes: 1 addition & 1 deletion pdata/pcommon/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TestMap_Range(t *testing.T) {
delete(rawMap, k)
return true
})
assert.EqualValues(t, 0, len(rawMap))
assert.Len(t, rawMap, 0)
}

func TestMap_FromRaw(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pdata/pcommon/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestSlice_EnsureCapacity(t *testing.T) {
for i := 0; i < es.Len(); i++ {
expectedEs[es.At(i).getOrig()] = true
}
assert.Equal(t, es.Len(), len(expectedEs))
assert.Len(t, expectedEs, es.Len())
es.EnsureCapacity(ensureSmallLen)
assert.Less(t, ensureSmallLen, es.Len())
foundEs := make(map[*otlpcommon.AnyValue]bool, es.Len())
Expand All @@ -89,7 +89,7 @@ func TestSlice_EnsureCapacity(t *testing.T) {
// Test ensure larger capacity
const ensureLargeLen = 9
oldLen := es.Len()
assert.Equal(t, oldLen, len(expectedEs))
assert.Len(t, expectedEs, oldLen)
es.EnsureCapacity(ensureLargeLen)
assert.Equal(t, ensureLargeLen, cap(*es.getOrig()))
}
Expand Down
24 changes: 12 additions & 12 deletions processor/batchprocessor/batch_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestBatchProcessorSentBySize(t *testing.T) {

require.Equal(t, requestCount*spansPerRequest, sink.SpanCount())
receivedTraces := sink.AllTraces()
require.EqualValues(t, expectedBatchesNum, len(receivedTraces))
require.Len(t, receivedTraces, expectedBatchesNum)
for _, td := range receivedTraces {
sizeSum += sizer.TracesSize(td)
rss := td.ResourceSpans()
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestBatchProcessorSentBySizeWithMaxSize(t *testing.T) {

require.Equal(t, totalSpans, sink.SpanCount())
receivedTraces := sink.AllTraces()
require.EqualValues(t, expectedBatchesNum, len(receivedTraces))
require.Len(t, receivedTraces, expectedBatchesNum)
// we have to count the size after it was processed since splitTraces will cause some
// repeated ResourceSpan data to be sent through the processor
var min, max int
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestBatchProcessorSentByTimeout(t *testing.T) {

require.Equal(t, requestCount*spansPerRequest, sink.SpanCount())
receivedTraces := sink.AllTraces()
require.EqualValues(t, expectedBatchesNum, len(receivedTraces))
require.Len(t, receivedTraces, expectedBatchesNum)
for _, td := range receivedTraces {
rss := td.ResourceSpans()
require.Equal(t, expectedBatchingFactor, rss.Len())
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestBatchProcessorTraceSendWhenClosing(t *testing.T) {
require.NoError(t, batcher.Shutdown(context.Background()))

require.Equal(t, requestCount*spansPerRequest, sink.SpanCount())
require.Equal(t, 1, len(sink.AllTraces()))
require.Len(t, sink.AllTraces(), 1)
}

func TestBatchMetricProcessor_ReceivingData(t *testing.T) {
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestBatchMetricProcessorBatchSize(t *testing.T) {

require.Equal(t, requestCount*2*metricsPerRequest, sink.DataPointCount())
receivedMds := sink.AllMetrics()
require.Equal(t, expectedBatchesNum, len(receivedMds))
require.Len(t, receivedMds, expectedBatchesNum)
for _, md := range receivedMds {
require.Equal(t, expectedBatchingFactor, md.ResourceMetrics().Len())
for i := 0; i < expectedBatchingFactor; i++ {
Expand Down Expand Up @@ -733,7 +733,7 @@ func TestBatchMetricsProcessor_Timeout(t *testing.T) {

require.Equal(t, requestCount*2*metricsPerRequest, sink.DataPointCount())
receivedMds := sink.AllMetrics()
require.Equal(t, expectedBatchesNum, len(receivedMds))
require.Len(t, receivedMds, expectedBatchesNum)
for _, md := range receivedMds {
require.Equal(t, expectedBatchingFactor, md.ResourceMetrics().Len())
for i := 0; i < expectedBatchingFactor; i++ {
Expand Down Expand Up @@ -765,7 +765,7 @@ func TestBatchMetricProcessor_Shutdown(t *testing.T) {
require.NoError(t, batcher.Shutdown(context.Background()))

require.Equal(t, requestCount*2*metricsPerRequest, sink.DataPointCount())
require.Equal(t, 1, len(sink.AllMetrics()))
require.Len(t, sink.AllMetrics(), 1)
}

func getTestSpanName(requestNum, index int) string {
Expand Down Expand Up @@ -972,7 +972,7 @@ func TestBatchLogProcessor_BatchSize(t *testing.T) {

require.Equal(t, requestCount*logsPerRequest, sink.LogRecordCount())
receivedMds := sink.AllLogs()
require.Equal(t, expectedBatchesNum, len(receivedMds))
require.Len(t, receivedMds, expectedBatchesNum)
for _, ld := range receivedMds {
require.Equal(t, expectedBatchingFactor, ld.ResourceLogs().Len())
for i := 0; i < expectedBatchingFactor; i++ {
Expand Down Expand Up @@ -1094,7 +1094,7 @@ func TestBatchLogsProcessor_Timeout(t *testing.T) {

require.Equal(t, requestCount*logsPerRequest, sink.LogRecordCount())
receivedMds := sink.AllLogs()
require.Equal(t, expectedBatchesNum, len(receivedMds))
require.Len(t, receivedMds, expectedBatchesNum)
for _, ld := range receivedMds {
require.Equal(t, expectedBatchingFactor, ld.ResourceLogs().Len())
for i := 0; i < expectedBatchingFactor; i++ {
Expand Down Expand Up @@ -1126,7 +1126,7 @@ func TestBatchLogProcessor_Shutdown(t *testing.T) {
require.NoError(t, batcher.Shutdown(context.Background()))

require.Equal(t, requestCount*logsPerRequest, sink.LogRecordCount())
require.Equal(t, 1, len(sink.AllLogs()))
require.Len(t, sink.AllLogs(), 1)
}

func getTestLogSeverityText(requestNum, index int) string {
Expand Down Expand Up @@ -1349,7 +1349,7 @@ func TestBatchZeroConfig(t *testing.T) {

// Expect them to be the original sizes.
receivedMds := sink.AllLogs()
require.Equal(t, requestCount, len(receivedMds))
require.Len(t, receivedMds, requestCount)
for i, ld := range receivedMds {
require.Equal(t, 1, ld.ResourceLogs().Len())
require.Equal(t, logsPerRequest+i, ld.LogRecordCount())
Expand Down Expand Up @@ -1387,7 +1387,7 @@ func TestBatchSplitOnly(t *testing.T) {

// Expect them to be the limited by maxBatch.
receivedMds := sink.AllLogs()
require.Equal(t, requestCount*logsPerRequest/maxBatch, len(receivedMds))
require.Len(t, receivedMds, requestCount*logsPerRequest/maxBatch)
for _, ld := range receivedMds {
require.Equal(t, maxBatch, ld.LogRecordCount())
}
Expand Down
Loading

0 comments on commit 40f908a

Please sign in to comment.