diff --git a/src/plugin/config_test.go b/src/plugin/config_test.go index 7ea1f27..5754738 100644 --- a/src/plugin/config_test.go +++ b/src/plugin/config_test.go @@ -13,8 +13,7 @@ func TestFailOnMissingEnvironment(t *testing.T) { var config plugin.Config fetcher := plugin.EnvironmentConfigFetcher{} - t.Setenv("BUILDKITE_PLUGIN_EXAMPLE_GO_MESSAGE", "") - os.Unsetenv("BUILDKITE_PLUGIN_EXAMPLE_GO_MESSAGE") + unsetEnv(t, "BUILDKITE_PLUGIN_EXAMPLE_GO_MESSAGE") err := fetcher.Fetch(&config) @@ -33,3 +32,20 @@ func TestFetchConfigFromEnvironment(t *testing.T) { require.NoError(t, err, "fetch should not error") assert.Equal(t, "test-message", config.Message, "fetched message should match environment") } + +func unsetEnv(t *testing.T, key string) { + t.Helper() + + // ensure state is restored correctly + currValue, exists := os.LookupEnv(key) + t.Cleanup(func() { + if exists { + os.Setenv(key, currValue) + } else { + os.Unsetenv(key) + } + }) + + // clear the value + os.Unsetenv(key) +}