You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched the issue tracker for an issue that matches the one I want to file, without success.
Problem Description
When trying to do configuration tests with viper and reading from the environment the tests cannot be run in parallel and any environment variables need to be set with t.Setenv, by making the function configurable we can provide many more options on how to do quick testing and parsing of configurations.
Proposed Solution
By adding the ability to change the os.LookupEnv function using an Option to the viper instance (defaulting to os.Lookupenv) we can ensure these tests can be run in parallel and can have their lookups mocked very simply.
Add a new attribute to the Viper struct
envLookupFunc func(key string) (string, bool)
set the default value in New to v.envLookupFunc = os.LookupEnv
add a new option
Change the usage of os.Getenv to use os.LookupEnv instead, or apply the same technique as above.
Alternatives Considered
No response
Additional Information
This feature is based upon the testing system described in the below article where tests pass a custom os.Getenv function to the call to the run code to simulate different configurations.
A maintainer will take a look at your issue shortly. 👀
In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.
📣 If you've already given us your feedback, you can still help by spreading the news,
either by sharing the above link or telling people about this on Twitter:
Preflight Checklist
Problem Description
When trying to do configuration tests with viper and reading from the environment the tests cannot be run in parallel and any environment variables need to be set with t.Setenv, by making the function configurable we can provide many more options on how to do quick testing and parsing of configurations.
Proposed Solution
By adding the ability to change the os.LookupEnv function using an Option to the viper instance (defaulting to os.Lookupenv) we can ensure these tests can be run in parallel and can have their lookups mocked very simply.
Add a new attribute to the
Viper
structset the default value in
New
tov.envLookupFunc = os.LookupEnv
add a new option
Change the usage of os.Getenv to use os.LookupEnv instead, or apply the same technique as above.
Alternatives Considered
No response
Additional Information
This feature is based upon the testing system described in the below article where tests pass a custom os.Getenv function to the call to the
run
code to simulate different configurations.https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years/
The text was updated successfully, but these errors were encountered: