]> Cypherpunks repositories - gostls13.git/commitdiff
testing/slogtest: check for no group with empty record
authorJonathan Amsterdam <jba@google.com>
Fri, 7 Jul 2023 13:55:56 +0000 (09:55 -0400)
committerJonathan Amsterdam <jba@google.com>
Tue, 11 Jul 2023 20:08:28 +0000 (20:08 +0000)
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>

src/testing/slogtest/slogtest.go

index 71076e52f4f684d86766d3f2d21ecd93ac1db2ad..b16d1227dc5854c243e3fbad453db0c398432546 100644 (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) {