From 3eaee3d5dd55dabf9da571be7a6a4fa05f9dbf29 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 7 Jul 2023 09:55:56 -0400 Subject: [PATCH] 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 Run-TryBot: Jonathan Amsterdam TryBot-Result: Gopher Robot --- src/testing/slogtest/slogtest.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/testing/slogtest/slogtest.go b/src/testing/slogtest/slogtest.go index 71076e52f4..b16d1227dc 100644 --- a/src/testing/slogtest/slogtest.go +++ b/src/testing/slogtest/slogtest.go @@ -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) { -- 2.48.1