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
{{ message }}
Repository navigation
proposal: testing: add Unsetenv to call os.Unsetenv and then restore the value using Cleanup #52817
I wish to have t.Unsetenv(key) that unsets environment variables and automatically restores their values using Cleanup, in a way similar to t.Setenv(key, value). Here is the hypothetical usage:
funcTestNoValue(t*testing.T) {
// unset the environment variable ENV_KEY during the testt.Unsetenv("ENV_KEY")
// ... more code here ...
}
Does t.Setenv(key, "") not work in most cases? I understand that programs can still use os.LookupEnv to differentiate an unset variable from a set-and-empty variable, but it's not very common at all. At the end of the day, these API helpers are for common needs, and I can't say that I've personally ever needed to call os.Unsetenv as opposed to os.Setenv or t.Setenv.
This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group
Honestly, most code (aside from reading configuration at application startup) probably shouldn't be reading from the environment anyway. It's the ultimate global variable.
I'm not sure that adding test helpers for things you shouldn't usually do is the best idea.
That being said, if we did want something like this, I think there's a simpler solution. The proposed function is doing two things, but only really needs to do one thing. Most of the time, test authors just want to ensure they leave the environment the way it was when the test started. This would do that, much more simply, IMO:
// CleanupEnv reverts all changes to environment variables at the end of this test.func (t*testing.T) CleanupEnv() {
vals:=os.Environ()
t.Cleanup(func(){
os.Clearenv()
for_, v:=rangevals {
parts:=strings.SplitN(v, "=", 2)
os.Setenv(parts[0], parts[1])
}
})
}
@natefinch you're not wrong, but given that we already have t.Setenv, adding t.CleanupEnv separately seems like having two confusingly similar ways to do something.
Activity
mvdan commentedon May 10, 2022
Does
t.Setenv(key, "")not work in most cases? I understand that programs can still useos.LookupEnvto differentiate an unset variable from a set-and-empty variable, but it's not very common at all. At the end of the day, these API helpers are for common needs, and I can't say that I've personally ever needed to callos.Unsetenvas opposed toos.Setenvort.Setenv.favonia commentedon May 10, 2022
@mvdan Thanks for your comment. I can find these lines in this repository that erase environment variables during testing:
go/src/cmd/go/go_test.go
Lines 277 to 280 in 6fd0520
go/src/cmd/go/internal/generate/generate_test.go
Lines 105 to 108 in 6fd0520
go/src/cmd/go/internal/work/security_test.go
Line 258 in 6fd0520
I can't really say how common it is, but Go itself is using it for testing.
rsc commentedon May 11, 2022
This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group
natefinch commentedon May 11, 2022
Honestly, most code (aside from reading configuration at application startup) probably shouldn't be reading from the environment anyway. It's the ultimate global variable.
I'm not sure that adding test helpers for things you shouldn't usually do is the best idea.
That being said, if we did want something like this, I think there's a simpler solution. The proposed function is doing two things, but only really needs to do one thing. Most of the time, test authors just want to ensure they leave the environment the way it was when the test started. This would do that, much more simply, IMO:
(with probably better error handling)
rsc commentedon May 11, 2022
@natefinch you're not wrong, but given that we already have t.Setenv, adding t.CleanupEnv separately seems like having two confusingly similar ways to do something.
I still don't think we need t.Unsetenv though.
rsc commentedon May 18, 2022
Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group
rogpeppe commentedon May 19, 2022
I agree that this isn't common enough to be that useful, especially as it's entirely trivial to do without the functionality built in:
favonia commentedon May 19, 2022
@rogpeppe Thanks for the tip. Somehow I missed this. 😃
rsc commentedon May 25, 2022
No change in consensus, so declined.
— rsc for the proposal review group