uber-go / fx

A dependency injection based application framework for Go.
https://uber-go.github.io/fx/
MIT License
5.89k stars 291 forks source link

Add ability to override os.Exit for fxtest #1117

Open codyaray opened 1 year ago

codyaray commented 1 year ago

Is your feature request related to a problem? Please describe.

We have a component test library for testing services (which use an uberfx-based library) as a blackbox. For this test framework we would like to catch all errors rather than exit the process.

As a bonus, it would be extra helpful if we had a good mechanism to test the actual error too (prior to os.Exit(1) being called), but understand that's a bigger scope - as long as that error is logged using the FX logger at least we can intercept that.

Describe the solution you'd like

I'd love to have an ability to override the osExit function used, similar to what's supported internally: https://github.com/uber-go/fx/pull/792

Describe alternatives you've considered

We've added our own timeouts in our test cases that developers set separately from FX timeouts. This is a recent comment that prompted me opening this ticket when it came up in a PR on our internal component test framework:

Screen Shot 2023-09-08 at 9 20 13 PM

cc @ctrando

Is this a breaking change? No

Additional context n/a

sywhang commented 1 year ago

I believe this is a fair ask if we're discussing how we can override it for testing purposes. Retitled the issue to reflect that - we can add this to fxtest package that can be used for configuring a test App.

For more general-purpose configuration of App though, i don't think it makes much sense to override that since there's no way to stop user-provided code from calling os.Exit or override its behavior.