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

Conversation

abhinav
Copy link
Collaborator

@abhinav abhinav commented Feb 12, 2024

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.

Copy link

codecov bot commented Feb 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (2d14fb7) 98.78% compared to head (7ef57f8) 98.79%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1159   +/-   ##
=======================================
  Coverage   98.78%   98.79%           
=======================================
  Files          30       30           
  Lines        3057     3061    +4     
=======================================
+ Hits         3020     3024    +4     
  Misses         30       30           
  Partials        7        7           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.
Copy link
Contributor

@JacobOaks JacobOaks left a comment

Choose a reason for hiding this comment

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

LGTM!

@JacobOaks JacobOaks merged commit d00172a into uber-go:master Feb 20, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants