-
Notifications
You must be signed in to change notification settings - Fork 21
[test] Add tests for logger.SlogHandler.Handle and related functions #3381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |||||||||||||||
| "log/slog" | ||||||||||||||||
| "os" | ||||||||||||||||
| "testing" | ||||||||||||||||
| "time" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/stretchr/testify/assert" | ||||||||||||||||
| "github.com/stretchr/testify/require" | ||||||||||||||||
|
|
@@ -482,3 +483,351 @@ func TestSlogHandler_Handle_EdgeCases(t *testing.T) { | |||||||||||||||
| assert.Equal(logger.Enabled(), enabled) | ||||||||||||||||
| }) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // TestSlogHandler_Handle_WithDebugEnabled tests Handle() when the logger is enabled. | ||||||||||||||||
| // These tests use t.Setenv to force-enable the logger regardless of the CI environment. | ||||||||||||||||
| func TestSlogHandler_Handle_WithDebugEnabled(t *testing.T) { | ||||||||||||||||
| tests := []struct { | ||||||||||||||||
| name string | ||||||||||||||||
| level slog.Level | ||||||||||||||||
| message string | ||||||||||||||||
| expectedPrefix string | ||||||||||||||||
| }{ | ||||||||||||||||
| { | ||||||||||||||||
| name: "debug level produces DEBUG prefix", | ||||||||||||||||
| level: slog.LevelDebug, | ||||||||||||||||
| message: "debug test message", | ||||||||||||||||
| expectedPrefix: "[DEBUG] ", | ||||||||||||||||
| }, | ||||||||||||||||
| { | ||||||||||||||||
| name: "info level produces INFO prefix", | ||||||||||||||||
| level: slog.LevelInfo, | ||||||||||||||||
| message: "info test message", | ||||||||||||||||
| expectedPrefix: "[INFO] ", | ||||||||||||||||
| }, | ||||||||||||||||
| { | ||||||||||||||||
| name: "warn level produces WARN prefix", | ||||||||||||||||
| level: slog.LevelWarn, | ||||||||||||||||
| message: "warn test message", | ||||||||||||||||
| expectedPrefix: "[WARN] ", | ||||||||||||||||
| }, | ||||||||||||||||
| { | ||||||||||||||||
| name: "error level produces ERROR prefix", | ||||||||||||||||
| level: slog.LevelError, | ||||||||||||||||
| message: "error test message", | ||||||||||||||||
| expectedPrefix: "[ERROR] ", | ||||||||||||||||
| }, | ||||||||||||||||
| { | ||||||||||||||||
| name: "unknown level produces no prefix", | ||||||||||||||||
| level: slog.Level(99), | ||||||||||||||||
| message: "unknown level message", | ||||||||||||||||
| expectedPrefix: "", | ||||||||||||||||
| }, | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| for _, tt := range tests { | ||||||||||||||||
| t.Run(tt.name, func(t *testing.T) { | ||||||||||||||||
| // Force-enable the logger by setting DEBUG=* before creating logger | ||||||||||||||||
| t.Setenv("DEBUG", "*") | ||||||||||||||||
|
|
||||||||||||||||
| output := captureStderr(func() { | ||||||||||||||||
| l := New("test:handle_levels") | ||||||||||||||||
| handler := NewSlogHandler(l) | ||||||||||||||||
|
|
||||||||||||||||
| r := slog.NewRecord(time.Now(), tt.level, tt.message, 0) | ||||||||||||||||
| err := handler.Handle(context.Background(), r) | ||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| assert.Contains(t, output, tt.message) | ||||||||||||||||
| if tt.expectedPrefix != "" { | ||||||||||||||||
| assert.Contains(t, output, tt.expectedPrefix) | ||||||||||||||||
|
||||||||||||||||
| assert.Contains(t, output, tt.expectedPrefix) | |
| assert.Contains(t, output, tt.expectedPrefix) | |
| } else { | |
| assert.NotContains(t, output, "[DEBUG] ") | |
| assert.NotContains(t, output, "[INFO] ") | |
| assert.NotContains(t, output, "[WARN] ") | |
| assert.NotContains(t, output, "[ERROR] ") |
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests use the hard-coded string "DEBUG" for environment configuration, while other tests in this file use the EnvDebug constant. Using EnvDebug here too avoids magic strings and keeps env-var naming consistent across the package.
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is named/commented as if it exercises the non-string key fallback (fmt.Sprint), but slog.Attr.Key is always a string and this test only adds a normal slog.String attr. As written it doesn't validate the fallback branch in SlogHandler.Handle and is misleading. Either rename/rewrite the test to match what it actually covers, or remove the unreachable fallback logic in Handle (and adjust the PR description accordingly).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require.NoErroris called insidecaptureStderr's callback. If this assertion ever fails,requirewillFailNowand skip the cleanup logic insidecaptureStderr, leavingos.Stderrredirected for the rest of the test run. Prefer returning the error from the callback and asserting outside, or useassert.NoErrorinside the callback, or updatecaptureStderrto restoreos.Stderrviadeferso it runs even onFailNow.This issue also appears in the following locations of the same file: