Replace os.Setenv with t.Setenv in all unit tests #6122
Labels
area/test
Issues or PRs related to unit and integration tests.
good first issue
Good for newcomers
kind/task
Categorizes issue or PR as related to a routine task that needs to be performed
priority/backlog
Higher priority than priority/awaiting-more-evidence.
We have a few unit tests which require environment variables to be set. At the moment, we typically call
os.Setenv
, with a deferred call toos.Unsetenv
to undo the change. For example:antrea/pkg/util/env/env_test.go
Lines 42 to 43 in bf06790
https://pkg.go.dev/testing#T.Setenv has been available since Go 1.17. It is a cleaner way to set environment variables for unit tests, and the variable will be unset automatically during test cleanup. We already use it in one test file: https://github.com/antrea-io/antrea/blob/main/pkg/apiserver/certificate/selfsignedcert_provider_test.go
All existing unit tests should be updated to use
t.Setenv
instead ofos.Setenv
, for readability and consistency.The text was updated successfully, but these errors were encountered: