1
0
mirror of https://github.com/golang/go synced 2024-11-17 02:04:48 -07:00

testing/slogtest: check for no group with empty record

As #61067 pointed out, slog did not properly handle empty groups.
https://go.dev/cl/508436 dealt with most cases inside slog itself,
but handlers must still do a check on their own. Namely, a handler
must not output a group created by WithGroup unless the Record
has attributes.

This change adds a test to slogtest to check that case.

Fixes #61227.

Change-Id: Ibc065b6e5f6e199a41bce8332ea8c7f9d8373392
Reviewed-on: https://go-review.googlesource.com/c/go/+/508438
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Jonathan Amsterdam 2023-07-07 09:55:56 -04:00
parent 167c8b73bf
commit 3eaee3d5dd

View File

@ -162,6 +162,20 @@ func TestHandler(h slog.Handler, results func() []map[string]any) error {
inGroup("G", inGroup("H", hasAttr("e", "f"))),
},
},
{
explanation: withSource("a Handler should not output groups for an empty Record"),
f: func(l *slog.Logger) {
l.With("a", "b").WithGroup("G").With("c", "d").WithGroup("H").Info("msg")
},
checks: []check{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "msg"),
hasAttr("a", "b"),
inGroup("G", hasAttr("c", "d")),
inGroup("G", missingKey("H")),
},
},
{
explanation: withSource("a Handler should call Resolve on attribute values"),
f: func(l *slog.Logger) {