diff options
author | Marc Pervaz Boocha <mboocha@sudomsg.com> | 2025-08-07 22:54:01 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <mboocha@sudomsg.com> | 2025-08-07 22:54:01 +0530 |
commit | 8ca9c43cfb814cc30e73fe324795171fe7193371 (patch) | |
tree | 3e3aa1f7f63fc4980763a5dc4474f8aeb71dbe36 | |
parent | Added File Mode to sockets and socket activation (diff) | |
download | kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.gz kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.bz2 kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.lz kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.xz kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.zst kit-8ca9c43cfb814cc30e73fe324795171fe7193371.zip |
Fixed Complience of slog test handler
-rw-r--r-- | logging/test/handler.go | 55 | ||||
-rw-r--r-- | logging/test/handler_test.go | 66 |
2 files changed, 117 insertions, 4 deletions
diff --git a/logging/test/handler.go b/logging/test/handler.go index d1f5d40..3eef031 100644 --- a/logging/test/handler.go +++ b/logging/test/handler.go @@ -58,21 +58,65 @@ func (h *MockHandler) Enabled(ctx context.Context, level slog.Level) bool { return true // Capture all logs } +func normalizeAttrs(attrs []slog.Attr) []slog.Attr { + var out []slog.Attr + for _, attr := range attrs { + if attr.Equal(slog.Attr{}) { + continue + } + + v := attr.Value.Resolve() + + switch v.Kind() { + case slog.KindGroup: + // Skip empty group + if len(v.Group()) == 0 { + continue + } + + inner := normalizeAttrs(v.Group()) + + // Flatten if group key is empty + if attr.Key == "" { + out = append(out, inner...) + } else { + out = append(out, slog.Attr{ + Key: attr.Key, + Value: slog.GroupValue(inner...), + }) + } + default: + out = append(out, slog.Attr{ + Key: attr.Key, + Value: v, + }) + } + } + return out +} + func (h *MockHandler) Handle(ctx context.Context, r slog.Record) error { + newRecord := slog.NewRecord(r.Time, r.Level, r.Message, r.PC) if h.parent == nil { - r.Clone() - h.recorder.Append(r) + attrs := []slog.Attr{} + r.Attrs(func(attr slog.Attr) bool { + attrs = append(attrs, attr) + return true + }) + attrs = normalizeAttrs(attrs) + newRecord.AddAttrs(attrs...) + h.recorder.Append(newRecord) return nil } - newRecord := slog.NewRecord(r.Time, r.Level, r.Message, r.PC) - attrs := slices.Clone(h.attrs) r.Attrs(func(attr slog.Attr) bool { attrs = append(attrs, attr) return true }) + attrs = normalizeAttrs(attrs) + if h.group != "" { newRecord.AddAttrs(slog.Attr{ Key: h.group, @@ -86,6 +130,9 @@ func (h *MockHandler) Handle(ctx context.Context, r slog.Record) error { } func (h *MockHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + if len(attrs) == 0 { + return h + } return &MockHandler{ recorder: h.recorder, parent: h, diff --git a/logging/test/handler_test.go b/logging/test/handler_test.go new file mode 100644 index 0000000..5e9430a --- /dev/null +++ b/logging/test/handler_test.go @@ -0,0 +1,66 @@ +package test_test + +import ( + "fmt" + "log/slog" + "runtime" + "testing" + "testing/slogtest" + + "go.sudomsg.com/kit/logging/test" +) + +func TestMockHandlerCompliance(t *testing.T) { + var lastMock *test.MockHandler + + slogtest.Run(t, + func(t *testing.T) slog.Handler { + lastMock = test.NewMockLogHandler(t) + return lastMock + }, + func(t *testing.T) map[string]any { + t.Helper() + + recs := lastMock.Records() + if len(recs) == 0 { + t.Fatalf("no records captured") + } + + rec := recs[len(recs)-1] + + // Convert slog.Record to map[string]any + m := make(map[string]any) + if !rec.Time.IsZero() { + m[slog.TimeKey] = rec.Time + } + if rec.PC != 0 { + fs := runtime.CallersFrames([]uintptr{rec.PC}) + f, _ := fs.Next() + m[slog.SourceKey] = fmt.Sprintf("%s:%d", f.File, f.Line) + } + m[slog.LevelKey] = rec.Level + m[slog.MessageKey] = rec.Message + + rec.Attrs(func(attr slog.Attr) bool { + insertAttr(t, m, attr) + return true + }) + + return m + }, + ) +} + +func insertAttr(t testing.TB, m map[string]any, attr slog.Attr) { + t.Helper() + + if attr.Value.Kind() == slog.KindGroup { + groupMap := make(map[string]any) + for _, a := range attr.Value.Group() { + insertAttr(t, groupMap, a) + } + m[attr.Key] = groupMap + } else { + m[attr.Key] = attr.Value.Any() + } +} |