diff options
author | Jonathan Amsterdam <jba@google.com> | 2023-07-07 09:55:56 -0400 |
---|---|---|
committer | Jonathan Amsterdam <jba@google.com> | 2023-07-11 20:08:28 +0000 |
commit | 3eaee3d5dd55dabf9da571be7a6a4fa05f9dbf29 (patch) | |
tree | 336f7e64004e77011f07ab7f80ab2cd5f219b149 | |
parent | 167c8b73bf92bdfed147e53b030331ac9260e0f6 (diff) | |
download | go-3eaee3d5dd55dabf9da571be7a6a4fa05f9dbf29.tar.gz go-3eaee3d5dd55dabf9da571be7a6a4fa05f9dbf29.zip |
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>
-rw-r--r-- | src/testing/slogtest/slogtest.go | 14 |
1 files changed, 14 insertions, 0 deletions
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 @@ -163,6 +163,20 @@ func TestHandler(h slog.Handler, results func() []map[string]any) error { }, }, { + 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) { l.Info("msg", "k", &replace{"replaced"}) |