Skip to content
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

testing/slogtest: add additional tests #62280

Closed
pohly opened this issue Aug 25, 2023 · 11 comments
Closed

testing/slogtest: add additional tests #62280

pohly opened this issue Aug 25, 2023 · 11 comments
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@pohly
Copy link

pohly commented Aug 25, 2023

There are some cases that are not covered by testing/slogtest in Go 1.21. It would be nice to increase coverage because then all slog.Handler implementations will benefit.

When looking at coverage of an implement that I was testing with testing/slogtest, I found that the following cases were not covered:

  • some value types: KindInt64, KindUint64, KindFloat64, KindBool, KindDuration, KindTime
  • some custom value type, for example a struct { SomeValue int }
  • empty group in a key/value pair (WithGroup is covered)
  • a Record with empty PC
  • a Record with an unknown PC (i.e. one where runtime.CallersFrames returns nothing)
  • different levels (Error, Warning, Debug)

/cc @jba

@pohly pohly added the Proposal label Aug 25, 2023
@gopherbot gopherbot added this to the Proposal milestone Aug 25, 2023
@pohly
Copy link
Author

pohly commented Aug 25, 2023

Not sure whether this should be a "proposal" or "bug"... 😅

@seankhliao seankhliao changed the title proposal: affected/package: testing/slogtest proposal: testing/slogtest: additional tests Aug 25, 2023
@pohly
Copy link
Author

pohly commented Aug 25, 2023

I might not be hitting my "if len(attr.Value.Group() == 0" code path because Record.Add already ignores the empty group in the key/value pairs. As with PC special cases, a direct call to the handler's Handle will be needed to cover this case.

@ianlancetaylor
Copy link
Contributor

There is no new API here so this doesn't have to be a proposal. Thanks.

@ianlancetaylor ianlancetaylor changed the title proposal: testing/slogtest: additional tests testing/slogtest: add additional tests Aug 25, 2023
@ianlancetaylor ianlancetaylor added help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed Proposal labels Aug 25, 2023
@ianlancetaylor ianlancetaylor modified the milestones: Proposal, Backlog Aug 25, 2023
@jba
Copy link
Contributor

jba commented Aug 29, 2023

slogtest deliberately uses only one or two value kinds so users don't have to write full parsers. I don't want to change that. Same applies to custom types.

I don't know what the test would be for error levels. We already check for the presence of the level key. Anything else would be format-dependent.

empty group in a key/value pair (WithGroup is covered)
a Record with empty PC
a Record with an unknown PC (i.e. one where runtime.CallersFrames returns nothing)

These might be worth it. I'll give it some thought. (Getting back from vacation.)

@AthulMuralidhar
Copy link

is this a good first issue to work on? im pretty new here and would like to contribute :)

@jba
Copy link
Contributor

jba commented Sep 7, 2023

empty group in a key/value pair (WithGroup is covered)

This test case covers that:

l.Info("msg", "a", "b", slog.Group("G"), "e", "f")

@pohly did you have something else in mind?

a Record with an unknown PC (i.e. one where runtime.CallersFrames returns nothing)

I don't know a guaranteed way to generate a bad PC.

a Record with empty PC

This seems do-able: verify that a handler doesn't output an attribute whose key is slog.SourceKey if Record.PC is zero.

@AthulMuralidhar This would be a good beginner project. I believe it is relatively straightforward to add this case to slogtest.go.

@pohly
Copy link
Author

pohly commented Sep 8, 2023

l.Info("msg", "a", "b", slog.Group("G"), "e", "f")
@pohly did you have something else in mind?

That's it, but I suspect it must be passed to slog.Handler.Handle, not slog.Logger. Otherwise the handler will not see it.

@AthulMuralidhar
Copy link

AthulMuralidhar commented Sep 11, 2023

@jba thx! i'll setup and start getting busy :) - started on a fork here: AthulMuralidhar#1

@gopherbot
Copy link

Change https://go.dev/cl/536117 mentions this issue: testing/slogtest: test no source key with empty PC in record

@arukiidou
Copy link

@ianlancetaylor @jba

Could you please re-open this?
Some test cases are still not covered.

arukiidou added a commit to arukiidou/go that referenced this issue Feb 8, 2024
Refs golang#62280

Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
arukiidou added a commit to arukiidou/go that referenced this issue Feb 8, 2024
@gopherbot
Copy link

Change https://go.dev/cl/562635 mentions this issue: testing/slogtest: test nested groups in empty record

arukiidou added a commit to arukiidou/go that referenced this issue Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants