Replace os.Setenv usage by testing.Setenv in tests
Overview
There are some usages of os.Setenv
in tests:
internal/testhelper/testhelper.go
22: os.Setenv(key, value)
27: os.Setenv(key, originalValue)
71: err := os.Setenv(key, value)
72: return func() { os.Setenv(key, oldValue) }, err
internal/command/command_test.go
67: os.Setenv(k, v)
72: os.Setenv(k, v)
internal/config/config.go
126: os.Setenv("SSL_CERT_DIR", c.SslCertDir)
os.Setenv
should not be used as-is because the environment variable will store the set value after the test is complete. In order to do it correctly, we've implemented Setenv
function in internal/testhelper/testhelper.go
: this function sets the old value of the environment variable after the test is complete. But even that function is not necessary because Golang's testing
package provides a function with the similar functionality: https://pkg.go.dev/testing#T.Setenv
Proposal
- Replace all
os.Setenv
andtesthelper.Setenv
usages byt.Setenv
: https://pkg.go.dev/testing#T.Setenv - Remove
Setenv
function frominternal/testhelper/testhelper.go
Example of usage: !812 (f3c80161)
Bonus
Workhorse codebase contains a os.Setenv
usage as well: https://gitlab.com/gitlab-org/gitlab/-/blob/32d7efd86bea87b1e25ea145988b76053f4c13b1/workhorse/internal/config/config_test.go#L153-154. It would be cool to fix it but it's probably too small for a separate issue in gitlab-org/gitlab
project