-
Notifications
You must be signed in to change notification settings - Fork 18k
testing/slogtest: unable to get TestHandler suite passing when Handler doesn't emit slog.TimeKey #61706
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
Comments
It's great that you're using We can't change the package until the next release, and I don't see how we would change it for this. Maybe the function needs to take options like "I don't plan on displaying the time, ever." I don't want to do that, because every such option weakens the Handler contract. I'm open to other suggestions. Anyway, you need a workaround for the next six months. Here are two ideas:
|
Hey @jba, thanks for the ideas on hacky solutions! I think both would work for this case. I was hoping to flag this before the release, but oh well. For future consideration towards removing this test: This test feels like an oddball of the current test suite. It's the only test that checks for a missing key on a non-attr property. As a result, the Some examples where this assumption breaks down:
|
Thanks @matthewmueller, I'll think on it. /cc @ianthehat |
Change https://go.dev/cl/516076 mentions this issue: |
I do not have suggestions, I just want to note a use case I ran into that proved to be hard to test with slogtest. // errHandler wraps [slog.JSONHandler] to only log errors.
type errHandler struct {
*slog.JSONHandler
}
func (h errHandler) Enabled(_ context.Context, l slog.Level) bool {
if l >= slog.LevelError {
return true
}
return false
} Testing this with I would love an option to tell |
@komuw Your handler seems to be a specialization of this example. I think wrapping handlers like those that don't actually affect the format don't need |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm creating a new
slog.Handler
that intentionally doesn't print the time out. I'm trying to useslogtest.TestHandler
to test the new handler, but I'm running up against a test that I don't think I can make pass without altering theHandle
method to include the time.What did you expect to see?
I expect there to be some way to get the test suite passing without requiring the handler to print out the time.
What did you see instead?
The problematic test is:
In: https://tip.golang.org/src/testing/slogtest/slogtest.go
The documentation (helpfully) says the following:
So in my tests I do the following:
Unfortunately, the test case above fails and I can't figure out how I'd unset this key just for this test
The text was updated successfully, but these errors were encountered: