Skip to content

Commit

Permalink
feat(fxtest): Add WithTestLogger option (#1159)
Browse files Browse the repository at this point in the history
fxtest.New has this really nice behavior where by default,
it uses the `testing.TB` as the destination for log output.
Unfortunately, fxtest is not good for testing failure cases
because it fails the test if the container failed.

    app := fxtest.New(t,
        fx.Invoke(func() error { return errors.New("fail") }),
    )

    err := app.Start(ctx)
    // We never get here because fxtest.New has already failed the test.

So the expectation there is to use `fx.New(..)` which, by default,
logs to stderr.
This can be addressed by using the `fx.WithLogger` option
in combination with `fxtest.NewTestLogger`,
but it ends up being a mouthful:

    app := fx.New(
        fx.Invoke(func() error { return errors.New("fail") }),
fx.WithLogger(func() fxevent.Logger { return fxtest.NewTestLogger(t) }),
    )

This PR is a proposal and implementation of a new
`fxtest.WithTestLogger` option that shortens the above to:

    app := fx.New(
        fx.Invoke(func() error { return errors.New("fail") }),
        fxtest.WithTestLogger(t),
    )

As an example, a couple tests in Fx itself become more readable
with this new API.

Co-authored-by: Jacob Oaks <joaks@uber.com>
  • Loading branch information
abhinav and JacobOaks committed Feb 20, 2024
1 parent 2d14fb7 commit d00172a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
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

0 comments on commit d00172a

Please sign in to comment.