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

feat(fxtest): Add WithTestLogger option #1159

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unrelease
- No changes yet.
## Unreleased

### Added
- fxtest: Add WithTestLogger option that uses a `testing.TB` as the
Fx event logger.

## [1.20.1](https://github.com/uber-go/fx/compare/v1.20.0...v1.20.1) - 2023-10-17

Expand Down
4 changes: 2 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewForTest(tb testing.TB, opts ...Option) *App {
// Provide both: Logger and WithLogger so that if the test
// WithLogger fails, we don't pollute stderr.
Logger(fxtest.NewTestPrinter(tb)),
WithLogger(func() fxevent.Logger { return fxtest.NewTestLogger(tb) }),
fxtest.WithTestLogger(tb),
}
opts = append(testOpts, opts...)

Expand All @@ -73,7 +73,7 @@ func validateTestApp(tb testing.TB, opts ...Option) error {
// Provide both: Logger and WithLogger so that if the test
// WithLogger fails, we don't pollute stderr.
Logger(fxtest.NewTestPrinter(tb)),
WithLogger(func() fxevent.Logger { return fxtest.NewTestLogger(tb) }),
fxtest.WithTestLogger(tb),
}
opts = append(testOpts, opts...)

Expand Down
3 changes: 1 addition & 2 deletions fxtest/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"context"

"go.uber.org/fx"
"go.uber.org/fx/fxevent"
)

// App is a wrapper around fx.App that provides some testing helpers. By
Expand All @@ -38,7 +37,7 @@ type App struct {
// New creates a new test application.
func New(tb TB, opts ...fx.Option) *App {
allOpts := make([]fx.Option, 0, len(opts)+1)
allOpts = append(allOpts, fx.WithLogger(func() fxevent.Logger { return NewTestLogger(tb) }))
allOpts = append(allOpts, WithTestLogger(tb))
allOpts = append(allOpts, opts...)

app := fx.New(allOpts...)
Expand Down
8 changes: 8 additions & 0 deletions fxtest/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func NewTestLogger(t TB) fxevent.Logger {
return fxlog.DefaultLogger(testutil.WriteSyncer{T: t})
}

// WithTestLogger returns an fx.Option that uses the provided TB
// as the destination for Fx's log output.
func WithTestLogger(t TB) fx.Option {
return fx.WithLogger(func() fxevent.Logger {
return NewTestLogger(t)
})
}

type testPrinter struct {
TB
}
Expand Down
Loading