Robert Pająk 2025-05-21 09:15:30 +02:00 committed by GitHub
parent d2fff768dc
commit 3f85c35038
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 2 deletions

View File

@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
The package contains semantic conventions from the `v1.32.0` version of the OpenTelemetry Semantic Conventions.
See the [migration documentation](./semconv/v1.32.0/MIGRATION.md) for information on how to upgrade from `go.opentelemetry.io/otel/semconv/v1.31.0`(#6782)
- Add `Transform` option in `go.opentelemetry.io/otel/log/logtest`. (#6794)
- Add `Desc` option in `go.opentelemetry.io/otel/log/logtest`. (#6796)
### Removed

View File

@ -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
})
}
// 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 = text
cfg.args = args
return cfg
})
}

View File

@ -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 TestDesc(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, 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")
}