package common import ( "os" "testing" ) var testEnvKey string = "TEST_MQTT_KEY" func assertError(t testing.TB, got, want string) { t.Helper() if got != want { t.Errorf("Error: got '%s', want '%s'", got, want) } } func TestGetenvDefault(t *testing.T) { t.Run("key not set", func(t *testing.T) { originalEnv := os.Getenv(testEnvKey) os.Unsetenv(testEnvKey) defer os.Setenv(testEnvKey, originalEnv) got := GetenvDefault(testEnvKey, "found") want := "found" assertError(t, got, want) }) t.Run("key set", func(t *testing.T) { originalEnv := os.Getenv(testEnvKey) os.Setenv(testEnvKey, "batman") defer os.Setenv(testEnvKey, originalEnv) got := GetenvDefault(testEnvKey, "found") want := "batman" assertError(t, got, want) }) }