From f726f2830be9b1a764c989f933b7f302a9af3550 Mon Sep 17 00:00:00 2001 From: Robert Pajak Date: Tue, 20 May 2025 11:33:47 +0200 Subject: [PATCH 1/3] logtest: add Message --- CHANGELOG.md | 1 + log/logtest/assert.go | 23 ++++++++++++++++++++++- log/logtest/assert_test.go | 19 ++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8922fe55a3f..db1805beba1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Update `Baggage` in `go.opentelemetry.io/otel/propagation` to retrieve multiple values for a key when the carrier implements `ValuesGetter`. (#5973) - Add `AssertEqual` function in `go.opentelemetry.io/otel/log/logtest`. (#6662) - Add `Transform` option in `go.opentelemetry.io/otel/log/logtest`. (#6794) +- Add `Message` option in `go.opentelemetry.io/otel/log/logtest`. (#?) ### Removed diff --git a/log/logtest/assert.go b/log/logtest/assert.go index 4bb95212f7e..d2283cde264 100644 --- a/log/logtest/assert.go +++ b/log/logtest/assert.go @@ -45,7 +45,16 @@ func assertEqual[T Recording | Record](t testingT, want, got T, opts ...AssertOp cmpOpts = append(cmpOpts, cfg.cmpOpts...) if diff := cmp.Diff(want, got, cmpOpts...); diff != "" { - t.Errorf("mismatch (-want +got):\n%s", diff) + msg := "mismatch (-want +got):\n%s" + if cfg.msg != "" { + msg = cfg.msg + "\n" + msg + } + + args := make([]any, 0, len(cfg.args)+1) + args = append(args, cfg.args...) + args = append(args, diff) + + t.Errorf(msg, args...) return false } return true @@ -53,6 +62,8 @@ func assertEqual[T Recording | Record](t testingT, want, got T, opts ...AssertOp type assertConfig struct { cmpOpts []cmp.Option + msg string + args []any } // AssertOption allows for fine grain control over how AssertEqual operates. @@ -75,3 +86,13 @@ func Transform[A, B any](f func(A) B) AssertOption { return cfg }) } + +// Message sets a prepended assertion failure message. +// The message is formatted with the arguments using fmt.Sprintf. +func Message(msg string, args ...any) AssertOption { + return fnOption(func(cfg assertConfig) assertConfig { + cfg.msg = msg + cfg.args = args + return cfg + }) +} diff --git a/log/logtest/assert_test.go b/log/logtest/assert_test.go index 9eb27a41361..ff85225bf48 100644 --- a/log/logtest/assert_test.go +++ b/log/logtest/assert_test.go @@ -5,10 +5,12 @@ package logtest import ( "context" + "fmt" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/log" ) @@ -20,7 +22,7 @@ type mockTestingT struct { } func (m *mockTestingT) Errorf(format string, args ...any) { - m.errors = append(m.errors, format) + m.errors = append(m.errors, fmt.Sprintf(format, args...)) } func TestAssertEqual(t *testing.T) { @@ -186,3 +188,18 @@ func TestAssertEqualRecord(t *testing.T) { }) } } + +func TestMessage(t *testing.T) { + mockT := &mockTestingT{} + a := Record{ + Attributes: []log.KeyValue{log.String("foo", "bar")}, + } + b := Record{ + Attributes: []log.KeyValue{log.Int("n", 1)}, + } + + assertEqual(mockT, a, b, Message("custom message, %s", "test")) + + require.Len(t, mockT.errors, 1, "expected one error") + assert.Contains(t, mockT.errors[0], "custom message, test\n", "expected custom message") +} From c63ff611cd2980a6c2ac4f2a3ab7e605b1ba918e Mon Sep 17 00:00:00 2001 From: Robert Pajak Date: Tue, 20 May 2025 11:34:40 +0200 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db1805beba1..edfa9d2d782 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Update `Baggage` in `go.opentelemetry.io/otel/propagation` to retrieve multiple values for a key when the carrier implements `ValuesGetter`. (#5973) - Add `AssertEqual` function in `go.opentelemetry.io/otel/log/logtest`. (#6662) - Add `Transform` option in `go.opentelemetry.io/otel/log/logtest`. (#6794) -- Add `Message` option in `go.opentelemetry.io/otel/log/logtest`. (#?) +- Add `Message` option in `go.opentelemetry.io/otel/log/logtest`. (#6796) ### Removed From d17443a6f53c276028b304261147dc4533ce642e Mon Sep 17 00:00:00 2001 From: Robert Pajak Date: Tue, 20 May 2025 18:27:22 +0200 Subject: [PATCH 3/3] Rename Message to Desc --- CHANGELOG.md | 2 +- log/logtest/assert.go | 8 ++++---- log/logtest/assert_test.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edfa9d2d782..84d6821fee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Update `Baggage` in `go.opentelemetry.io/otel/propagation` to retrieve multiple values for a key when the carrier implements `ValuesGetter`. (#5973) - Add `AssertEqual` function in `go.opentelemetry.io/otel/log/logtest`. (#6662) - Add `Transform` option in `go.opentelemetry.io/otel/log/logtest`. (#6794) -- Add `Message` option in `go.opentelemetry.io/otel/log/logtest`. (#6796) +- Add `Desc` option in `go.opentelemetry.io/otel/log/logtest`. (#6796) ### Removed diff --git a/log/logtest/assert.go b/log/logtest/assert.go index d2283cde264..9e87f86a03f 100644 --- a/log/logtest/assert.go +++ b/log/logtest/assert.go @@ -87,11 +87,11 @@ func Transform[A, B any](f func(A) B) AssertOption { }) } -// Message sets a prepended assertion failure message. -// The message is formatted with the arguments using fmt.Sprintf. -func Message(msg string, args ...any) AssertOption { +// Desc prepends the given text to an assertion failure message. +// The text is formatted with the args using fmt.Sprintf. +func Desc(text string, args ...any) AssertOption { return fnOption(func(cfg assertConfig) assertConfig { - cfg.msg = msg + cfg.msg = text cfg.args = args return cfg }) diff --git a/log/logtest/assert_test.go b/log/logtest/assert_test.go index ff85225bf48..865f96fab42 100644 --- a/log/logtest/assert_test.go +++ b/log/logtest/assert_test.go @@ -189,7 +189,7 @@ func TestAssertEqualRecord(t *testing.T) { } } -func TestMessage(t *testing.T) { +func TestDesc(t *testing.T) { mockT := &mockTestingT{} a := Record{ Attributes: []log.KeyValue{log.String("foo", "bar")}, @@ -198,7 +198,7 @@ func TestMessage(t *testing.T) { Attributes: []log.KeyValue{log.Int("n", 1)}, } - assertEqual(mockT, a, b, Message("custom message, %s", "test")) + assertEqual(mockT, a, b, Desc("custom message, %s", "test")) require.Len(t, mockT.errors, 1, "expected one error") assert.Contains(t, mockT.errors[0], "custom message, test\n", "expected custom message")