diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-10-26 09:37:21 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-10-26 17:01:45 +0300 |
commit | 66c6c934ccc6c372ee36b6854d3d8b6f9526fb1b (patch) | |
tree | 10bda0c7ee6ec7bb074777768549fe7d6b12848a | |
parent | 995cd3db9810219b1e45305b3f449f568006e7ba (diff) |
testhelper: Change `ModifyEnvironment()` to unset envvars on empty value
In a subsequent commit, we'll want to modernize some tests to make
better use of the testhelper package, where some of these tests need to
unset certain environment variables if they're set. Ideally we would use
our `ModifyEnvironment()` test helper for this, but it can only set an
envvar to a new value as compared to unsetting it altogether.
Change the function to unset envvars if it is given an empty string as
value. While this is a backwards-incompatible change, no tests seem to
currently make use of empty values, so it shouldn't be much of an issue
to change it like this.
-rw-r--r-- | internal/testhelper/testhelper.go | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index da0b5a308..759bf9b06 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -208,12 +208,18 @@ func WriteExecutable(t testing.TB, path string, content []byte) { } // ModifyEnvironment will change an environment variable and return a func suitable -// for `defer` to change the value back. +// for `defer` to change the value back. If the given value is empty, then the envvar will be +// unset. func ModifyEnvironment(t testing.TB, key string, value string) func() { t.Helper() oldValue, hasOldValue := os.LookupEnv(key) - require.NoError(t, os.Setenv(key, value)) + if value == "" { + require.NoError(t, os.Unsetenv(key)) + } else { + require.NoError(t, os.Setenv(key, value)) + } + return func() { if hasOldValue { require.NoError(t, os.Setenv(key, oldValue)) |