Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 22 additions & 1 deletion log/logtest/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,25 @@ 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
}

type assertConfig struct {
cmpOpts []cmp.Option
msg string
args []any
}

// AssertOption allows for fine grain control over how AssertEqual operates.
Expand All @@ -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
})
}
19 changes: 18 additions & 1 deletion log/logtest/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
}
Loading