Skip to content

Commit

Permalink
[chore]: enable len and empty rules from testifylint (#5832)
Browse files Browse the repository at this point in the history
#### Description

Testifylint is a linter that provides best practices with the use of
testify.

This PR enables
[empty](https://github.com/Antonboom/testifylint?tab=readme-ov-file#empty)
and
[len](https://github.com/Antonboom/testifylint?tab=readme-ov-file#len)
rules from [testifylint](https://github.com/Antonboom/testifylint)

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Sep 21, 2024
1 parent aef9e4f commit 063239f
Show file tree
Hide file tree
Showing 33 changed files with 132 additions and 134 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,11 @@ linters-settings:
testifylint:
enable-all: true
disable:
- empty
- error-is-as
- error-nil
- expected-actual
- float-compare
- go-require
- len
- negative-positive
- require-error
- suite-extra-assert-call
40 changes: 20 additions & 20 deletions baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,25 +815,25 @@ func TestBaggageSetMember(t *testing.T) {
assert.NoError(t, err)
assert.NotContains(t, b0.list, key)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, 0, len(b0.list))
assert.Equal(t, 1, len(b1.list))
assert.Empty(t, b0.list)
assert.Len(t, b1.list, 1)

m.value = "v"
b2, err := b1.SetMember(m)
assert.NoError(t, err)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, baggage.Item{Value: "v"}, b2.list[key])
assert.Equal(t, 1, len(b1.list))
assert.Equal(t, 1, len(b2.list))
assert.Len(t, b1.list, 1)
assert.Len(t, b2.list, 1)

p := properties{{key: "p"}}
m.properties = p
b3, err := b2.SetMember(m)
assert.NoError(t, err)
assert.Equal(t, baggage.Item{Value: "v"}, b2.list[key])
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b3.list[key])
assert.Equal(t, 1, len(b2.list))
assert.Equal(t, 1, len(b3.list))
assert.Len(t, b2.list, 1)
assert.Len(t, b3.list, 1)

// The returned baggage needs to be immutable and should use a copy of the
// properties slice.
Expand All @@ -849,8 +849,8 @@ func TestBaggageSetMember(t *testing.T) {
assert.NotContains(t, b3.list, m.key)
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b4.list[key])
assert.Equal(t, baggage.Item{}, b4.list[m.key])
assert.Equal(t, 1, len(b3.list))
assert.Equal(t, 2, len(b4.list))
assert.Len(t, b3.list, 1)
assert.Len(t, b4.list, 2)
}

func TestBaggageSetFalseMember(t *testing.T) {
Expand All @@ -862,16 +862,16 @@ func TestBaggageSetFalseMember(t *testing.T) {
assert.Error(t, err)
assert.NotContains(t, b0.list, key)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, 0, len(b0.list))
assert.Equal(t, 0, len(b1.list))
assert.Empty(t, b0.list)
assert.Empty(t, b1.list)

m.value = "v"
b2, err := b1.SetMember(m)
assert.Error(t, err)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, baggage.Item{Value: ""}, b2.list[key])
assert.Equal(t, 0, len(b1.list))
assert.Equal(t, 0, len(b2.list))
assert.Empty(t, b1.list)
assert.Empty(t, b2.list)
}

func TestBaggageSetFalseMembers(t *testing.T) {
Expand All @@ -883,25 +883,25 @@ func TestBaggageSetFalseMembers(t *testing.T) {
assert.NoError(t, err)
assert.NotContains(t, b0.list, key)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, 0, len(b0.list))
assert.Equal(t, 1, len(b1.list))
assert.Empty(t, b0.list)
assert.Len(t, b1.list, 1)

m.value = "v"
b2, err := b1.SetMember(m)
assert.NoError(t, err)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, baggage.Item{Value: "v"}, b2.list[key])
assert.Equal(t, 1, len(b1.list))
assert.Equal(t, 1, len(b2.list))
assert.Len(t, b1.list, 1)
assert.Len(t, b2.list, 1)

p := properties{{key: "p"}}
m.properties = p
b3, err := b2.SetMember(m)
assert.NoError(t, err)
assert.Equal(t, baggage.Item{Value: "v"}, b2.list[key])
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b3.list[key])
assert.Equal(t, 1, len(b2.list))
assert.Equal(t, 1, len(b3.list))
assert.Len(t, b2.list, 1)
assert.Len(t, b3.list, 1)

// The returned baggage needs to be immutable and should use a copy of the
// properties slice.
Expand All @@ -917,8 +917,8 @@ func TestBaggageSetFalseMembers(t *testing.T) {
assert.NotContains(t, b3.list, m.key)
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b4.list[key])
assert.Equal(t, baggage.Item{}, b4.list[m.key])
assert.Equal(t, 1, len(b3.list))
assert.Equal(t, 1, len(b4.list))
assert.Len(t, b3.list, 1)
assert.Len(t, b4.list, 1)
}

func TestNilBaggageMembers(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlplog/otlploggrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func TestClient(t *testing.T) {
require.NoError(t, client.UploadLogs(ctx, resourceLogs))
require.NoError(t, client.UploadLogs(ctx, resourceLogs))

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
want := fmt.Sprintf("%s (%d log records rejected)", msg, n)
assert.ErrorContains(t, errs[0], want)
})
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlplog/otlploggrpc/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestExporter(t *testing.T) {
require.NoError(t, e.Export(ctx, records))
require.NoError(t, e.Export(ctx, records))

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
want := fmt.Sprintf("%s (%d log records rejected)", msg, n)
assert.ErrorContains(t, errs[0], want)
})
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/otlplog/otlploghttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func TestClient(t *testing.T) {
require.NoError(t, client.UploadLogs(ctx, resourceLogs))
require.NoError(t, client.UploadLogs(ctx, resourceLogs))

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
want := fmt.Sprintf("%s (%d log records rejected)", msg, n)
assert.ErrorContains(t, errs[0], want)
})
Expand Down Expand Up @@ -697,7 +697,7 @@ func TestConfig(t *testing.T) {
t.Cleanup(func() { close(rCh) })
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
assert.NoError(t, exp.Export(ctx, make([]log.Record, 1)), "failed retry")
assert.Len(t, rCh, 0, "failed HTTP responses did not occur")
assert.Empty(t, rCh, "failed HTTP responses did not occur")
})

t.Run("WithRetryAndExporterErr", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestConfigs(t *testing.T) {
assert.NotNil(t, c.Metrics.GRPCCredentials)
} else {
// nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
assert.Equal(t, 1, len(c.Metrics.TLSCfg.RootCAs.Subjects()))
assert.Len(t, c.Metrics.TLSCfg.RootCAs.Subjects(), 1)
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ func RunClientTests(f ClientFactory) func(*testing.T) {
require.NoError(t, client.ForceFlush(ctx))
rm := collector.Collect().Dump()
// Data correctness is not important, just it was received.
require.Greater(t, len(rm), 0, "no data uploaded")
require.NotEmpty(t, rm, "no data uploaded")

require.NoError(t, client.Shutdown(ctx))
rm = collector.Collect().Dump()
assert.Len(t, rm, 0, "client did not flush all data")
assert.Empty(t, rm, "client did not flush all data")
})

t.Run("UploadMetrics", func(t *testing.T) {
Expand Down Expand Up @@ -269,7 +269,7 @@ func RunClientTests(f ClientFactory) func(*testing.T) {
require.NoError(t, client.UploadMetrics(ctx, resourceMetrics))
require.NoError(t, client.Shutdown(ctx))

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
want := fmt.Sprintf("%s (%d metric data points rejected)", msg, n)
assert.ErrorContains(t, errs[0], want)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestMultiErr(t *testing.T) {
// language so this doesn't become a change-indicator.
msg := me.Error()
lines := strings.Split(msg, "\n")
assert.Equalf(t, 4, len(lines), "expected a 4 line error message, got:\n\n%s", msg)
assert.Lenf(t, lines, 4, "expected a 4 line error message, got:\n\n%s", msg)
assert.Contains(t, msg, name)
assert.Contains(t, msg, e0.Error())
assert.Contains(t, msg, testErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestConfig(t *testing.T) {
t.Cleanup(func() { close(rCh) })
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
assert.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}), "failed retry")
assert.Len(t, rCh, 0, "failed HTTP responses did not occur")
assert.Empty(t, rCh, "failed HTTP responses did not occur")
})

t.Run("WithRetryAndExporterErr", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestConfigs(t *testing.T) {
assert.NotNil(t, c.Metrics.GRPCCredentials)
} else {
// nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
assert.Equal(t, 1, len(c.Metrics.TLSCfg.RootCAs.Subjects()))
assert.Len(t, c.Metrics.TLSCfg.RootCAs.Subjects(), 1)
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ func RunClientTests(f ClientFactory) func(*testing.T) {
require.NoError(t, client.ForceFlush(ctx))
rm := collector.Collect().Dump()
// Data correctness is not important, just it was received.
require.Greater(t, len(rm), 0, "no data uploaded")
require.NotEmpty(t, rm, "no data uploaded")

require.NoError(t, client.Shutdown(ctx))
rm = collector.Collect().Dump()
assert.Len(t, rm, 0, "client did not flush all data")
assert.Empty(t, rm, "client did not flush all data")
})

t.Run("UploadMetrics", func(t *testing.T) {
Expand Down Expand Up @@ -269,7 +269,7 @@ func RunClientTests(f ClientFactory) func(*testing.T) {
require.NoError(t, client.UploadMetrics(ctx, resourceMetrics))
require.NoError(t, client.Shutdown(ctx))

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
want := fmt.Sprintf("%s (%d metric data points rejected)", msg, n)
assert.ErrorContains(t, errs[0], want)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestMultiErr(t *testing.T) {
// language so this doesn't become a change-indicator.
msg := me.Error()
lines := strings.Split(msg, "\n")
assert.Equalf(t, 4, len(lines), "expected a 4 line error message, got:\n\n%s", msg)
assert.Lenf(t, lines, 4, "expected a 4 line error message, got:\n\n%s", msg)
assert.Contains(t, msg, name)
assert.Contains(t, msg, e0.Error())
assert.Contains(t, msg, testErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlptrace/otlptracegrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func TestPartialSuccess(t *testing.T) {
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
require.NoError(t, exp.ExportSpans(ctx, roSpans))

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
require.Contains(t, errs[0].Error(), "partially successful")
require.Contains(t, errs[0].Error(), "2 spans rejected")
}
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/otlptrace/otlptracehttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func TestPartialSuccess(t *testing.T) {
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan())
assert.NoError(t, err)

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
require.Contains(t, errs[0].Error(), "partially successful")
require.Contains(t, errs[0].Error(), "2 spans rejected")
}
Expand Down Expand Up @@ -443,7 +443,7 @@ func TestOtherHTTPSuccess(t *testing.T) {
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan())
assert.NoError(t, err)

assert.Equal(t, 0, len(errs))
assert.Empty(t, errs)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,14 +867,14 @@ func TestIncompatibleMeterName(t *testing.T) {
err = testutil.GatherAndCompare(registry, file)
require.NoError(t, err)

assert.Equal(t, 1, len(errs))
assert.Len(t, errs, 1)

// A second collect shouldn't trigger new errors
_, err = file.Seek(0, io.SeekStart)
assert.NoError(t, err)
err = testutil.GatherAndCompare(registry, file)
require.NoError(t, err)
assert.Equal(t, 1, len(errs))
assert.Len(t, errs, 1)
}

func TestShutdownExporter(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exporters/zipkin/zipkin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TestExportSpans(t *testing.T) {
exporter, err := New(collector.url, WithLogger(logger))
require.NoError(t, err)
ctx := context.Background()
require.Len(t, ls.Messages, 0)
require.Empty(t, ls.Messages)
require.NoError(t, exporter.ExportSpans(ctx, spans[0:1]))
require.Len(t, ls.Messages, 1)
require.Contains(t, ls.Messages[0], "send a POST request")
Expand Down
2 changes: 1 addition & 1 deletion internal/global/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestMeterProviderDelegatesCalls(t *testing.T) {
assert.Equal(t, 1, tMeter.siCount)
assert.Equal(t, 1, tMeter.siUDCount)
assert.Equal(t, 1, tMeter.siHist)
assert.Equal(t, 1, len(tMeter.callbacks))
assert.Len(t, tMeter.callbacks, 1)

// Because the Meter was provided by testMeterProvider it should also return our test instrument
require.IsType(t, &testCountingFloatInstrument{}, ctr, "the meter did not delegate calls to the meter")
Expand Down
2 changes: 1 addition & 1 deletion internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestConfigs(t *testing.T) {
assert.NotNil(t, c.Metrics.GRPCCredentials)
} else {
// nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
assert.Equal(t, 1, len(c.Metrics.TLSCfg.RootCAs.Subjects()))
assert.Len(t, c.Metrics.TLSCfg.RootCAs.Subjects(), 1)
}
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/shared/otlp/otlpmetric/otest/client.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ func RunClientTests(f ClientFactory) func(*testing.T) {
require.NoError(t, client.ForceFlush(ctx))
rm := collector.Collect().Dump()
// Data correctness is not important, just it was received.
require.Greater(t, len(rm), 0, "no data uploaded")
require.NotEmpty(t, rm, "no data uploaded")

require.NoError(t, client.Shutdown(ctx))
rm = collector.Collect().Dump()
assert.Len(t, rm, 0, "client did not flush all data")
assert.Empty(t, rm, "client did not flush all data")
})

t.Run("UploadMetrics", func(t *testing.T) {
Expand Down Expand Up @@ -269,7 +269,7 @@ func RunClientTests(f ClientFactory) func(*testing.T) {
require.NoError(t, client.UploadMetrics(ctx, resourceMetrics))
require.NoError(t, client.Shutdown(ctx))

require.Equal(t, 1, len(errs))
require.Len(t, errs, 1)
want := fmt.Sprintf("%s (%d metric data points rejected)", msg, n)
assert.ErrorContains(t, errs[0], want)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestMultiErr(t *testing.T) {
// language so this doesn't become a change-indicator.
msg := me.Error()
lines := strings.Split(msg, "\n")
assert.Equalf(t, 4, len(lines), "expected a 4 line error message, got:\n\n%s", msg)
assert.Lenf(t, lines, 4, "expected a 4 line error message, got:\n\n%s", msg)
assert.Contains(t, msg, name)
assert.Contains(t, msg, e0.Error())
assert.Contains(t, msg, testErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion log/logtest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestLoggerEnabledFnUnset(t *testing.T) {
func TestRecorderEmitAndReset(t *testing.T) {
r := NewRecorder()
l := r.Logger("test")
assert.Len(t, r.Result()[0].Records, 0)
assert.Empty(t, r.Result()[0].Records)

r1 := log.Record{}
r1.SetSeverity(log.SeverityInfo)
Expand Down
2 changes: 1 addition & 1 deletion sdk/log/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func TestBufferExporter(t *testing.T) {
// Nothing to flush.
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush empty")
assert.Equal(t, 1, exp.ExportN(), "Export number changed")
assert.Len(t, exp.Records(), 0, "exported non-zero Records")
assert.Empty(t, exp.Records(), "exported non-zero Records")
})

t.Run("ContextCancelled", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/internal/aggregate/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func TestDeltaHistogramReset(t *testing.T) {

var data metricdata.Aggregation = metricdata.Histogram[int64]{}
require.Equal(t, 0, h.delta(&data))
require.Len(t, data.(metricdata.Histogram[int64]).DataPoints, 0)
require.Empty(t, data.(metricdata.Histogram[int64]).DataPoints)

h.measure(context.Background(), 1, alice, nil)

Expand All @@ -363,7 +363,7 @@ func TestDeltaHistogramReset(t *testing.T) {
// The attr set should be forgotten once Aggregations is called.
expect.DataPoints = nil
assert.Equal(t, 0, h.delta(&data))
assert.Len(t, data.(metricdata.Histogram[int64]).DataPoints, 0)
assert.Empty(t, data.(metricdata.Histogram[int64]).DataPoints)

// Aggregating another set should not affect the original (alice).
h.measure(context.Background(), 1, bob, nil)
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/internal/exemplar/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func testDropFiltered[N int64 | float64](t *testing.T) {
var dest []Exemplar
r.Collect(&dest)

assert.Len(t, dest, 0, "non-sampled context should not be offered")
assert.Empty(t, dest, "non-sampled context should not be offered")
}
Loading

0 comments on commit 063239f

Please sign in to comment.