-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
test: use T.Setenv
to set env vars in tests
#43517
Conversation
This commit replaces `os.Setenv` with `t.Setenv` in tests. The environment variable is automatically restored to its original value when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.Setenv Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine to me too -- it will potentially cause issues with some packagers who will have to backport Go 1.17 if they want to backport newer Docker releases, but I don't think there's much more we can do (reasonably) to help there.
Thanks @Juneezee ! As Tianon mentioned in the previous comment; we discussed this change in our maintainers call, and agreed that it's ok to make these changes (and it's nice to use more of Go's built-in functionality). As a follow-up we should have a look at existing uses of (We'd still need to keep / want Let me know if you're interested to work on that! |
@thaJeztah Thanks for accepting the PR! Sure, I'd love to work on replacing |
oh! I just realised I was touching much of those in #43340, so perhaps I should just include that change in that one 😅 |
- What I did
A testing cleanup.
This PR replaces
os.Setenv
witht.Setenv
. Starting from Go 1.17, we can uset.Setenv
to set environment variable in test. The environment variable is automatically restored to its original value when the test and all its subtests complete.Reference: https://pkg.go.dev/testing#T.Setenv
- How I did it
Search for usages of
os.Setenv
in*_test.go
using GoLand, and replace them witht.Setenv
accordingly.- How to verify it
All tests should continue to pass, except those that are already failing before this PR.
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)