From 186bd18db4f6eb585c0234fcb3473af17eb6a11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Mon, 7 Jul 2025 18:13:53 +0800 Subject: [PATCH] log/logtest: change AssertEqual to accept TestingT for benchmark support (#6908) --- CHANGELOG.md | 4 ++++ log/logtest/assert.go | 14 ++++---------- log/logtest/assert_test.go | 14 +++++++++++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02a815ef7..9be9fa06d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - `RPCGRPCRequestMetadata` - `RPCGRPCResponseMetadata` +### Changed + +- Change `AssertEqual` in `go.opentelemetry.io/otel/log/logtest` to accept `TestingT` in order to support benchmarks and fuzz tests. (#6908) + diff --git a/log/logtest/assert.go b/log/logtest/assert.go index 9e87f86a0..dfd754425 100644 --- a/log/logtest/assert.go +++ b/log/logtest/assert.go @@ -5,7 +5,6 @@ package logtest // import "go.opentelemetry.io/otel/log/logtest" import ( "context" - "testing" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -13,19 +12,14 @@ import ( "go.opentelemetry.io/otel/log" ) -// AssertEqual asserts that the two concrete data-types from the logtest package are equal. -func AssertEqual[T Recording | Record](t *testing.T, want, got T, opts ...AssertOption) bool { - t.Helper() - return assertEqual(t, want, got, opts...) -} - -// testingT reports failure messages. +// TestingT reports failure messages. // *testing.T implements this interface. -type testingT interface { +type TestingT interface { Errorf(format string, args ...any) } -func assertEqual[T Recording | Record](t testingT, want, got T, opts ...AssertOption) bool { +// AssertEqual asserts that the two concrete data-types from the logtest package are equal. +func AssertEqual[T Recording | Record](t TestingT, want, got T, opts ...AssertOption) bool { if h, ok := t.(interface{ Helper() }); ok { h.Helper() } diff --git a/log/logtest/assert_test.go b/log/logtest/assert_test.go index 865f96fab..1bdc531fb 100644 --- a/log/logtest/assert_test.go +++ b/log/logtest/assert_test.go @@ -17,6 +17,14 @@ import ( var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) +// Compile-time check to ensure testing structs implement TestingT. +var ( + _ TestingT = (*testing.T)(nil) + _ TestingT = (*testing.B)(nil) + _ TestingT = (*testing.F)(nil) + _ TestingT = (*mockTestingT)(nil) +) + type mockTestingT struct { errors []string } @@ -114,7 +122,7 @@ func TestAssertEqualRecording(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { mockT := &mockTestingT{} - result := assertEqual(mockT, tc.a, tc.b, tc.opts...) + result := AssertEqual(mockT, tc.a, tc.b, tc.opts...) if result != tc.want { t.Errorf("AssertEqual() = %v, want %v", result, tc.want) } @@ -178,7 +186,7 @@ func TestAssertEqualRecord(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { mockT := &mockTestingT{} - result := assertEqual(mockT, tc.a, tc.b, tc.opts...) + result := AssertEqual(mockT, tc.a, tc.b, tc.opts...) if result != tc.want { t.Errorf("AssertEqual() = %v, want %v", result, tc.want) } @@ -198,7 +206,7 @@ func TestDesc(t *testing.T) { Attributes: []log.KeyValue{log.Int("n", 1)}, } - assertEqual(mockT, a, b, Desc("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")