Skip to content

Commit 3f85c35

Browse files
authored
logtest: add Desc (#6796)
Fixes #6490 Fixes #6341
1 parent d2fff76 commit 3f85c35

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2626
The package contains semantic conventions from the `v1.32.0` version of the OpenTelemetry Semantic Conventions.
2727
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)
2828
- Add `Transform` option in `go.opentelemetry.io/otel/log/logtest`. (#6794)
29+
- Add `Desc` option in `go.opentelemetry.io/otel/log/logtest`. (#6796)
2930

3031
### Removed
3132

log/logtest/assert.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,25 @@ func assertEqual[T Recording | Record](t testingT, want, got T, opts ...AssertOp
4545
cmpOpts = append(cmpOpts, cfg.cmpOpts...)
4646

4747
if diff := cmp.Diff(want, got, cmpOpts...); diff != "" {
48-
t.Errorf("mismatch (-want +got):\n%s", diff)
48+
msg := "mismatch (-want +got):\n%s"
49+
if cfg.msg != "" {
50+
msg = cfg.msg + "\n" + msg
51+
}
52+
53+
args := make([]any, 0, len(cfg.args)+1)
54+
args = append(args, cfg.args...)
55+
args = append(args, diff)
56+
57+
t.Errorf(msg, args...)
4958
return false
5059
}
5160
return true
5261
}
5362

5463
type assertConfig struct {
5564
cmpOpts []cmp.Option
65+
msg string
66+
args []any
5667
}
5768

5869
// AssertOption allows for fine grain control over how AssertEqual operates.
@@ -75,3 +86,13 @@ func Transform[A, B any](f func(A) B) AssertOption {
7586
return cfg
7687
})
7788
}
89+
90+
// Desc prepends the given text to an assertion failure message.
91+
// The text is formatted with the args using fmt.Sprintf.
92+
func Desc(text string, args ...any) AssertOption {
93+
return fnOption(func(cfg assertConfig) assertConfig {
94+
cfg.msg = text
95+
cfg.args = args
96+
return cfg
97+
})
98+
}

log/logtest/assert_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package logtest
55

66
import (
77
"context"
8+
"fmt"
89
"testing"
910
"time"
1011

1112
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214

1315
"go.opentelemetry.io/otel/log"
1416
)
@@ -20,7 +22,7 @@ type mockTestingT struct {
2022
}
2123

2224
func (m *mockTestingT) Errorf(format string, args ...any) {
23-
m.errors = append(m.errors, format)
25+
m.errors = append(m.errors, fmt.Sprintf(format, args...))
2426
}
2527

2628
func TestAssertEqual(t *testing.T) {
@@ -186,3 +188,18 @@ func TestAssertEqualRecord(t *testing.T) {
186188
})
187189
}
188190
}
191+
192+
func TestDesc(t *testing.T) {
193+
mockT := &mockTestingT{}
194+
a := Record{
195+
Attributes: []log.KeyValue{log.String("foo", "bar")},
196+
}
197+
b := Record{
198+
Attributes: []log.KeyValue{log.Int("n", 1)},
199+
}
200+
201+
assertEqual(mockT, a, b, Desc("custom message, %s", "test"))
202+
203+
require.Len(t, mockT.errors, 1, "expected one error")
204+
assert.Contains(t, mockT.errors[0], "custom message, test\n", "expected custom message")
205+
}

0 commit comments

Comments
 (0)