mirror of
https://github.com/safedep/vet.git
synced 2025-12-10 00:22:08 -06:00
fix: safely handle global config in test runner
This commit is contained in:
parent
e32784a09e
commit
7cb923b2fd
@ -6,6 +6,15 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestDefaultConfig(t *testing.T) {
|
||||||
config := DefaultConfig()
|
config := DefaultConfig()
|
||||||
|
|
||||||
@ -73,12 +82,10 @@ func TestTenantDomain(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should fallback to default from global config", func(t *testing.T) {
|
t.Run("should fallback to default from global config", func(t *testing.T) {
|
||||||
oldVal := globalConfig.TenantDomain
|
withGlobalConfig(t, func() *Config {
|
||||||
t.Cleanup(func() {
|
return &Config{TenantDomain: "test-tenant-from-config"}
|
||||||
globalConfig.TenantDomain = oldVal
|
|
||||||
})
|
})
|
||||||
|
|
||||||
globalConfig.TenantDomain = "test-tenant-from-config"
|
|
||||||
assert.Equal(t, "test-tenant-from-config", TenantDomain())
|
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) {
|
t.Run("should fallback to default from global config", func(t *testing.T) {
|
||||||
oldVal := globalConfig.ApiKey
|
withGlobalConfig(t, func() *Config {
|
||||||
t.Cleanup(func() {
|
return &Config{ApiKey: "test-api-key-from-config"}
|
||||||
globalConfig.ApiKey = oldVal
|
|
||||||
})
|
})
|
||||||
|
|
||||||
globalConfig.ApiKey = "test-api-key-from-config"
|
|
||||||
assert.Equal(t, "test-api-key-from-config", ApiKey())
|
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) {
|
t.Run("should fallback to default from global config", func(t *testing.T) {
|
||||||
oldVal := globalConfig.ApiUrl
|
withGlobalConfig(t, func() *Config {
|
||||||
t.Cleanup(func() {
|
return &Config{ApiUrl: "https://test-api-from-config.safedep.io"}
|
||||||
globalConfig.ApiUrl = oldVal
|
|
||||||
})
|
})
|
||||||
|
|
||||||
globalConfig.ApiUrl = "https://test-api-from-config.safedep.io"
|
|
||||||
assert.Equal(t, "https://test-api-from-config.safedep.io", ApiUrl())
|
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) {
|
t.Run("should return default API URL when no config is set", func(t *testing.T) {
|
||||||
// Clear global config temporarily
|
withGlobalConfig(t, func() *Config {
|
||||||
oldConfig := globalConfig
|
return nil
|
||||||
globalConfig = nil
|
|
||||||
t.Cleanup(func() {
|
|
||||||
globalConfig = oldConfig
|
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.Equal(t, defaultApiUrl, ApiUrl())
|
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) {
|
t.Run("should fallback to default from global config", func(t *testing.T) {
|
||||||
oldVal := globalConfig.Community
|
withGlobalConfig(t, func() *Config {
|
||||||
t.Cleanup(func() {
|
return &Config{Community: true}
|
||||||
globalConfig.Community = oldVal
|
|
||||||
})
|
})
|
||||||
|
|
||||||
globalConfig.Community = true
|
|
||||||
assert.True(t, CommunityMode())
|
assert.True(t, CommunityMode())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should return false when no config is set", func(t *testing.T) {
|
t.Run("should return false when no config is set", func(t *testing.T) {
|
||||||
// Clear global config temporarily
|
withGlobalConfig(t, func() *Config {
|
||||||
oldConfig := globalConfig
|
return nil
|
||||||
globalConfig = nil
|
|
||||||
t.Cleanup(func() {
|
|
||||||
globalConfig = oldConfig
|
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.False(t, CommunityMode())
|
assert.False(t, CommunityMode())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user