From 7cb923b2fd3fed99b312fbafa74d9c2ba6069583 Mon Sep 17 00:00:00 2001 From: abhisek Date: Fri, 15 Aug 2025 21:28:23 +0530 Subject: [PATCH] fix: safely handle global config in test runner --- internal/auth/auth_test.go | 47 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index 5988ad5..74301c9 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -6,6 +6,15 @@ import ( "github.com/stretchr/testify/assert" ) +func withGlobalConfig(t *testing.T, fn func() *Config) { + oldConfig := globalConfig + t.Cleanup(func() { + globalConfig = oldConfig + }) + + globalConfig = fn() +} + func TestDefaultConfig(t *testing.T) { config := DefaultConfig() @@ -73,12 +82,10 @@ func TestTenantDomain(t *testing.T) { }) t.Run("should fallback to default from global config", func(t *testing.T) { - oldVal := globalConfig.TenantDomain - t.Cleanup(func() { - globalConfig.TenantDomain = oldVal + withGlobalConfig(t, func() *Config { + return &Config{TenantDomain: "test-tenant-from-config"} }) - globalConfig.TenantDomain = "test-tenant-from-config" assert.Equal(t, "test-tenant-from-config", TenantDomain()) }) } @@ -100,12 +107,10 @@ func TestApiKey(t *testing.T) { }) t.Run("should fallback to default from global config", func(t *testing.T) { - oldVal := globalConfig.ApiKey - t.Cleanup(func() { - globalConfig.ApiKey = oldVal + withGlobalConfig(t, func() *Config { + return &Config{ApiKey: "test-api-key-from-config"} }) - globalConfig.ApiKey = "test-api-key-from-config" assert.Equal(t, "test-api-key-from-config", ApiKey()) }) } @@ -122,21 +127,16 @@ func TestApiUrl(t *testing.T) { }) t.Run("should fallback to default from global config", func(t *testing.T) { - oldVal := globalConfig.ApiUrl - t.Cleanup(func() { - globalConfig.ApiUrl = oldVal + withGlobalConfig(t, func() *Config { + return &Config{ApiUrl: "https://test-api-from-config.safedep.io"} }) - globalConfig.ApiUrl = "https://test-api-from-config.safedep.io" assert.Equal(t, "https://test-api-from-config.safedep.io", ApiUrl()) }) t.Run("should return default API URL when no config is set", func(t *testing.T) { - // Clear global config temporarily - oldConfig := globalConfig - globalConfig = nil - t.Cleanup(func() { - globalConfig = oldConfig + withGlobalConfig(t, func() *Config { + return nil }) assert.Equal(t, defaultApiUrl, ApiUrl()) @@ -165,21 +165,16 @@ func TestCommunityMode(t *testing.T) { }) t.Run("should fallback to default from global config", func(t *testing.T) { - oldVal := globalConfig.Community - t.Cleanup(func() { - globalConfig.Community = oldVal + withGlobalConfig(t, func() *Config { + return &Config{Community: true} }) - globalConfig.Community = true assert.True(t, CommunityMode()) }) t.Run("should return false when no config is set", func(t *testing.T) { - // Clear global config temporarily - oldConfig := globalConfig - globalConfig = nil - t.Cleanup(func() { - globalConfig = oldConfig + withGlobalConfig(t, func() *Config { + return nil }) assert.False(t, CommunityMode())