feat refactor config for default application

This commit is contained in:
П$iх 2022-08-06 23:40:55 +03:00
parent cda5d0cff2
commit d87aed14f4
2 changed files with 41 additions and 45 deletions

View File

@ -15,10 +15,12 @@ type Config struct {
} }
} }
var cfg Config var (
var once sync.Once cfg Config //nolint:gochecknoglobals // singleton globals
once sync.Once //nolint:gochecknoglobals // singleton globals
)
// panic // GetConfig can panic
func GetConfig() Config { func GetConfig() Config {
once.Do(func() { once.Do(func() {
err := cleanenv.ReadEnv(&cfg) err := cleanenv.ReadEnv(&cfg)
@ -26,5 +28,6 @@ func GetConfig() Config {
panic(err) panic(err)
} }
}) })
return cfg return cfg
} }

View File

@ -2,50 +2,47 @@ package config
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"os"
"strconv" "strconv"
"sync" "sync"
"testing" "testing"
) )
func TestGetConfig(t *testing.T) { const (
telegramToken := ":: telegram telegram token ::" telegramToken = ":: telegram telegram token ::" //nolint:gosec // it is not real credentials
telegramOwner := 1234567890 telegramOwner = 1234567890
pathDictionaries := `docs/dictionaries` pathDictionaries = `docs/dictionaries`
)
initTelegramBotEnvironment(telegramToken, telegramOwner, pathDictionaries) func TestGetConfig(t *testing.T) { //nolint:paralleltest // because this test mock globals
defer cleanTelegramBotEnvironment() initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries)
defer resetConfig(t)
var config Config var config Config
assert.NotPanics(t, func() { config = GetConfig() }) assert.NotPanics(t, func() { config = GetConfig() })
assert.NotEmpty(t, config) assert.NotNil(t, config)
assert.IsType(t, Config{}, config)
assert.Equal(t, telegramToken, config.API.Token) assert.Equal(t, telegramToken, config.API.Token)
assert.Equal(t, telegramOwner, config.API.Owner) assert.Equal(t, telegramOwner, config.API.Owner)
assert.Equal(t, pathDictionaries, config.Paths.Docs) assert.Equal(t, pathDictionaries, config.Paths.Docs)
} }
func TestGetConfigTwice(t *testing.T) { func TestGetConfigTwice(t *testing.T) { //nolint:paralleltest // because this test mock globals
initTelegramBotEnvironment(":: telegram telegram token ::", 1234567890, `docs/dictionaries`) initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries)
defer cleanTelegramBotEnvironment() defer resetConfig(t)
config1 := GetConfig() config1 := GetConfig()
config2 := GetConfig() config2 := GetConfig()
assert.NotEmpty(t, config1) assert.NotNil(t, config1)
assert.NotEmpty(t, config2) assert.NotNil(t, config2)
assert.Equal(t, config1, config2) assert.Equal(t, config1, config2)
assert.NotSame(t, config1, config2) assert.NotSame(t, config1, config2)
} }
func TestGetConfigAndSingletonIsImmutable(t *testing.T) { func TestGetConfigAndSingletonIsImmutable(t *testing.T) { //nolint:paralleltest // because this test mock globals
telegramToken := ":: telegram telegram token ::" initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries)
telegramOwner := 1234567890 defer resetConfig(t)
pathDictionaries := `docs/dictionaries`
initTelegramBotEnvironment(telegramToken, telegramOwner, pathDictionaries)
defer cleanTelegramBotEnvironment()
config1 := GetConfig() config1 := GetConfig()
@ -63,13 +60,12 @@ func TestGetConfigAndSingletonIsImmutable(t *testing.T) {
assert.Equal(t, pathDictionaries, config2.Paths.Docs) assert.Equal(t, pathDictionaries, config2.Paths.Docs)
} }
func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals
telegramToken := ":: telegram telegram token ::" t.Setenv("TOKEN", telegramToken)
telegramOwner := 1234567890 t.Setenv("OWNER", strconv.Itoa(telegramOwner))
_ = os.Setenv("TOKEN", telegramToken) resetConfig(t)
_ = os.Setenv("OWNER", strconv.Itoa(telegramOwner)) defer resetConfig(t)
defer cleanTelegramBotEnvironment()
config := GetConfig() config := GetConfig()
@ -80,27 +76,24 @@ func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) {
assert.Equal(t, `docs`, config.Paths.Docs) assert.Equal(t, `docs`, config.Paths.Docs)
} }
func TestGetConfigWithEmptyEnvironment(t *testing.T) { func TestGetConfigWithEmptyEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals
cleanTelegramBotEnvironment() resetConfig(t)
defer resetConfig(t)
assert.Panics(t, func() { GetConfig() }) assert.Panics(t, func() { GetConfig() })
} }
func initTelegramBotEnvironment(telegramToken string, telegramOwner int, pathDictionaries string) { func initTelegramBotEnvironment(t *testing.T, telegramToken string, telegramOwner int, pathDictionaries string) {
resetConfig() t.Helper()
_ = os.Setenv("TOKEN", telegramToken) t.Setenv("TOKEN", telegramToken)
_ = os.Setenv("OWNER", strconv.Itoa(telegramOwner)) t.Setenv("OWNER", strconv.Itoa(telegramOwner))
_ = os.Setenv("DOCS_PATH", pathDictionaries) t.Setenv("DOCS_PATH", pathDictionaries)
resetConfig(t)
} }
func cleanTelegramBotEnvironment() { func resetConfig(t *testing.T) {
_ = os.Unsetenv("TOKEN") t.Helper()
_ = os.Unsetenv("OWNER")
_ = os.Unsetenv("DOCS_PATH")
resetConfig()
}
func resetConfig() {
cfg = Config{} cfg = Config{}
once = sync.Once{} once = sync.Once{}
} }