From 51d57f0c628bbe9a1d61ad93b52703d390da0735 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 14 Nov 2024 21:25:06 +0100 Subject: [PATCH] [chore]: use ErrorContains with testify (#11680) #### Description use ErrorContains with testify when possible Signed-off-by: Matthieu MOREL --- component/componenttest/configtest_test.go | 3 +-- pdata/internal/json/attribute_test.go | 4 +--- pdata/plog/json_test.go | 6 ++---- pdata/pmetric/json_test.go | 8 ++------ pdata/pprofile/json_test.go | 12 +++--------- pdata/ptrace/json_test.go | 20 +++++--------------- 6 files changed, 14 insertions(+), 39 deletions(-) diff --git a/component/componenttest/configtest_test.go b/component/componenttest/configtest_test.go index 70f776b030e..a51df5fce5c 100644 --- a/component/componenttest/configtest_test.go +++ b/component/componenttest/configtest_test.go @@ -139,8 +139,7 @@ func TestCheckConfigStruct(t *testing.T) { if tt.wantErrMsgSubStr == "" { assert.NoError(t, err) } else { - require.Error(t, err) - assert.Contains(t, err.Error(), tt.wantErrMsgSubStr) + require.ErrorContains(t, err, tt.wantErrMsgSubStr) } }) } diff --git a/pdata/internal/json/attribute_test.go b/pdata/internal/json/attribute_test.go index fa355dbce38..92f7fd1e307 100644 --- a/pdata/internal/json/attribute_test.go +++ b/pdata/internal/json/attribute_test.go @@ -183,9 +183,7 @@ func TestReadValueInvliadBytesValue(t *testing.T) { defer jsoniter.ConfigFastest.ReturnIterator(iter) ReadValue(iter, &otlpcommon.AnyValue{}) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "base64") - } + assert.ErrorContains(t, iter.Error, "base64") } func TestReadArrayUnknownField(t *testing.T) { diff --git a/pdata/plog/json_test.go b/pdata/plog/json_test.go index 6e1162311d1..160e5b7909e 100644 --- a/pdata/plog/json_test.go +++ b/pdata/plog/json_test.go @@ -123,8 +123,7 @@ func TestUnmarshalJsoniterLogWrongTraceID(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewLogRecord().unmarshalJsoniter(iter) - require.Error(t, iter.Error) - assert.Contains(t, iter.Error.Error(), "parse trace_id") + require.ErrorContains(t, iter.Error, "parse trace_id") } func TestUnmarshalJsoniterLogWrongSpanID(t *testing.T) { @@ -132,8 +131,7 @@ func TestUnmarshalJsoniterLogWrongSpanID(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewLogRecord().unmarshalJsoniter(iter) - require.Error(t, iter.Error) - assert.Contains(t, iter.Error.Error(), "parse span_id") + require.ErrorContains(t, iter.Error, "parse span_id") } func BenchmarkJSONUnmarshal(b *testing.B) { diff --git a/pdata/pmetric/json_test.go b/pdata/pmetric/json_test.go index 8d1d5ac7434..3030fb2fb47 100644 --- a/pdata/pmetric/json_test.go +++ b/pdata/pmetric/json_test.go @@ -531,9 +531,7 @@ func TestExemplarInvalidTraceID(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewExemplar().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse trace_id") - } + assert.ErrorContains(t, iter.Error, "parse trace_id") } func TestExemplarInvalidSpanID(t *testing.T) { @@ -541,9 +539,7 @@ func TestExemplarInvalidSpanID(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewExemplar().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse span_id") - } + assert.ErrorContains(t, iter.Error, "parse span_id") } func TestExemplar(t *testing.T) { diff --git a/pdata/pprofile/json_test.go b/pdata/pprofile/json_test.go index 5da98421d81..98bdd858e64 100644 --- a/pdata/pprofile/json_test.go +++ b/pdata/pprofile/json_test.go @@ -167,9 +167,7 @@ func TestUnmarshalJsoniterProfileInvalidProfileIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewProfileContainer().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse profile_id") - } + assert.ErrorContains(t, iter.Error, "parse profile_id") } func TestUnmarshalJsoniterResourceProfiles(t *testing.T) { @@ -287,9 +285,7 @@ func TestUnmarshalJsoniterLinkInvalidTraceIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewLink().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse trace_id") - } + assert.ErrorContains(t, iter.Error, "parse trace_id") } func TestUnmarshalJsoniterSpanLinkInvalidSpanIDField(t *testing.T) { @@ -297,9 +293,7 @@ func TestUnmarshalJsoniterSpanLinkInvalidSpanIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewLink().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse span_id") - } + assert.ErrorContains(t, iter.Error, "parse span_id") } func TestUnmarshalJsoniterLabel(t *testing.T) { diff --git a/pdata/ptrace/json_test.go b/pdata/ptrace/json_test.go index 11af3457835..8e1b683fc1d 100644 --- a/pdata/ptrace/json_test.go +++ b/pdata/ptrace/json_test.go @@ -159,9 +159,7 @@ func TestUnmarshalJsoniterSpanInvalidTraceIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewSpan().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse trace_id") - } + assert.ErrorContains(t, iter.Error, "parse trace_id") } func TestUnmarshalJsoniterSpanInvalidSpanIDField(t *testing.T) { @@ -169,9 +167,7 @@ func TestUnmarshalJsoniterSpanInvalidSpanIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewSpan().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse span_id") - } + assert.ErrorContains(t, iter.Error, "parse span_id") } func TestUnmarshalJsoniterSpanInvalidParentSpanIDField(t *testing.T) { @@ -179,9 +175,7 @@ func TestUnmarshalJsoniterSpanInvalidParentSpanIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewSpan().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse parent_span_id") - } + assert.ErrorContains(t, iter.Error, "parse parent_span_id") } func TestUnmarshalJsoniterSpanStatus(t *testing.T) { @@ -209,9 +203,7 @@ func TestUnmarshalJsoniterSpanLinkInvalidTraceIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewSpanLink().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse trace_id") - } + assert.ErrorContains(t, iter.Error, "parse trace_id") } func TestUnmarshalJsoniterSpanLinkInvalidSpanIDField(t *testing.T) { @@ -219,9 +211,7 @@ func TestUnmarshalJsoniterSpanLinkInvalidSpanIDField(t *testing.T) { iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) defer jsoniter.ConfigFastest.ReturnIterator(iter) NewSpanLink().unmarshalJsoniter(iter) - if assert.Error(t, iter.Error) { - assert.Contains(t, iter.Error.Error(), "parse span_id") - } + assert.ErrorContains(t, iter.Error, "parse span_id") } func TestUnmarshalJsoniterSpanEvent(t *testing.T) {