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

improve slogtest #13

Merged
merged 6 commits into from
Aug 9, 2024
Merged

improve slogtest #13

merged 6 commits into from
Aug 9, 2024

Conversation

imjasonh
Copy link
Member

@imjasonh imjasonh commented Feb 12, 2024

Without the slogtest change, with the newly added test:

go test ./slogtest/ -v                
=== RUN   TestSlogTest
    slogtest.go:20: time=2024-02-12T16:50:56.437-05:00 level=INFO msg="hello world" foo=bar
        
--- PASS: TestSlogTest (0.00s)
PASS
ok      github.com/chainguard-dev/clog/slogtest 0.116s

With this change:

go test ./slogtest -v
=== RUN   TestSlogTest
    slogtest.go:31: level=INFO source=/Users/jason/git/clog/slogtest/slogtest_test.go:13 msg="hello world" foo=bar
    slogtest.go:31: level=INFO source=/Users/jason/git/clog/slogtest/slogtest_test.go:14 msg="me again" bar=baz
    slogtest.go:31: level=INFO source=/Users/jason/git/clog/slogtest/slogtest_test.go:15 msg="okay last one" baz=true

@imjasonh
Copy link
Member Author

Also removing trailing newlines so logs go from this:

go test ./slogtest/ -v 
=== RUN   TestSlogTest
    slogtest.go:21: time=2024-02-12T17:04:57.896-05:00 level=INFO msg="hello world" foo=bar
        
    slogtest.go:21: time=2024-02-12T17:04:57.896-05:00 level=INFO msg="me again" bar=baz
        
    slogtest.go:21: time=2024-02-12T17:04:57.896-05:00 level=INFO msg="okay last one" baz=true
        
--- PASS: TestSlogTest (0.00s)
PASS

to this

go test ./slogtest/ -v 
=== RUN   TestSlogTest
    slogtest.go:21: time=2024-02-12T17:03:46.996-05:00 level=INFO msg="hello world" foo=bar
    slogtest.go:21: time=2024-02-12T17:03:46.996-05:00 level=INFO msg="me again" bar=baz
    slogtest.go:21: time=2024-02-12T17:03:46.996-05:00 level=INFO msg="okay last one" baz=true
--- PASS: TestSlogTest (0.00s)
PASS

@imjasonh imjasonh requested a review from wlynch February 12, 2024 22:06
@wlynch
Copy link
Member

wlynch commented Feb 12, 2024

Drive-by observation - This is coming from the testing package, not slog (https://cs.opensource.google/go/go/+/refs/tags/go1.22.0:src/testing/testing.go;l=1010).

Setting AddSource: true will include the correct path.

=== RUN   TestSlogTest
    slogtest.go:20: time=2024-02-12T17:12:42.519-05:00 level=INFO source=/Users/wlynch/go/src/github.com/chainguard-dev/clog/slogtest/slogtest_test.go:12 msg="hello world" foo=bar
        
--- PASS: TestSlogTest (0.00s)
PASS
ok      github.com/chainguard-dev/clog/slogtest 0.112s

@imjasonh imjasonh changed the title WIP: fix slogtest reporter with t.Helper improve slogtest Aug 9, 2024
@imjasonh imjasonh marked this pull request as ready for review August 9, 2024 14:39
@imjasonh
Copy link
Member Author

imjasonh commented Aug 9, 2024

I've updated this PR to not use t.Helper, and instead just improve the experience of slogtest in general.

This deprecates slogtest.TestContextWithLogger(t) in favor of slogtest.Context(t), and adds go docs.

@imjasonh imjasonh enabled auto-merge (squash) August 9, 2024 14:41

// TestLogger gets a logger to use in unit and end to end tests.
// This logger is configured to log at debug level.
func TestLogger(t Logger) *clog.Logger {
return TestLoggerWithOptions(t, &slog.HandlerOptions{
Level: slog.LevelDebug,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep this? IIRC this lets debug logs surface in test output (which follows the regular go test pattern where they only show if the test fails).

@imjasonh imjasonh merged commit 56a6d5a into chainguard-dev:main Aug 9, 2024
2 checks passed
imjasonh added a commit that referenced this pull request Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants