diff --git a/config/Config.go b/config/Config.go index 827d757..0ec4bc4 100644 --- a/config/Config.go +++ b/config/Config.go @@ -15,10 +15,12 @@ type Config struct { } } -var cfg Config -var once sync.Once +var ( + cfg Config //nolint:gochecknoglobals // singleton globals + once sync.Once //nolint:gochecknoglobals // singleton globals +) -// panic +// GetConfig can panic func GetConfig() Config { once.Do(func() { err := cleanenv.ReadEnv(&cfg) @@ -26,5 +28,6 @@ func GetConfig() Config { panic(err) } }) + return cfg } diff --git a/config/Config_test.go b/config/Config_test.go index ec6f8b9..ca73b53 100644 --- a/config/Config_test.go +++ b/config/Config_test.go @@ -2,50 +2,47 @@ package config import ( "github.com/stretchr/testify/assert" - "os" "strconv" "sync" "testing" ) -func TestGetConfig(t *testing.T) { - telegramToken := ":: telegram telegram token ::" - telegramOwner := 1234567890 - pathDictionaries := `docs/dictionaries` +const ( + telegramToken = ":: telegram telegram token ::" //nolint:gosec // it is not real credentials + telegramOwner = 1234567890 + pathDictionaries = `docs/dictionaries` +) - initTelegramBotEnvironment(telegramToken, telegramOwner, pathDictionaries) - defer cleanTelegramBotEnvironment() +func TestGetConfig(t *testing.T) { //nolint:paralleltest // because this test mock globals + initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries) + defer resetConfig(t) var config Config + assert.NotPanics(t, func() { config = GetConfig() }) - assert.NotEmpty(t, config) - assert.IsType(t, Config{}, config) + assert.NotNil(t, config) assert.Equal(t, telegramToken, config.API.Token) assert.Equal(t, telegramOwner, config.API.Owner) assert.Equal(t, pathDictionaries, config.Paths.Docs) } -func TestGetConfigTwice(t *testing.T) { - initTelegramBotEnvironment(":: telegram telegram token ::", 1234567890, `docs/dictionaries`) - defer cleanTelegramBotEnvironment() +func TestGetConfigTwice(t *testing.T) { //nolint:paralleltest // because this test mock globals + initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries) + defer resetConfig(t) config1 := GetConfig() config2 := GetConfig() - assert.NotEmpty(t, config1) - assert.NotEmpty(t, config2) + assert.NotNil(t, config1) + assert.NotNil(t, config2) assert.Equal(t, config1, config2) assert.NotSame(t, config1, config2) } -func TestGetConfigAndSingletonIsImmutable(t *testing.T) { - telegramToken := ":: telegram telegram token ::" - telegramOwner := 1234567890 - pathDictionaries := `docs/dictionaries` - - initTelegramBotEnvironment(telegramToken, telegramOwner, pathDictionaries) - defer cleanTelegramBotEnvironment() +func TestGetConfigAndSingletonIsImmutable(t *testing.T) { //nolint:paralleltest // because this test mock globals + initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries) + defer resetConfig(t) config1 := GetConfig() @@ -63,13 +60,12 @@ func TestGetConfigAndSingletonIsImmutable(t *testing.T) { assert.Equal(t, pathDictionaries, config2.Paths.Docs) } -func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { - telegramToken := ":: telegram telegram token ::" - telegramOwner := 1234567890 +func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals + t.Setenv("TOKEN", telegramToken) + t.Setenv("OWNER", strconv.Itoa(telegramOwner)) - _ = os.Setenv("TOKEN", telegramToken) - _ = os.Setenv("OWNER", strconv.Itoa(telegramOwner)) - defer cleanTelegramBotEnvironment() + resetConfig(t) + defer resetConfig(t) config := GetConfig() @@ -80,27 +76,24 @@ func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { assert.Equal(t, `docs`, config.Paths.Docs) } -func TestGetConfigWithEmptyEnvironment(t *testing.T) { - cleanTelegramBotEnvironment() +func TestGetConfigWithEmptyEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals + resetConfig(t) + defer resetConfig(t) assert.Panics(t, func() { GetConfig() }) } -func initTelegramBotEnvironment(telegramToken string, telegramOwner int, pathDictionaries string) { - resetConfig() - _ = os.Setenv("TOKEN", telegramToken) - _ = os.Setenv("OWNER", strconv.Itoa(telegramOwner)) - _ = os.Setenv("DOCS_PATH", pathDictionaries) +func initTelegramBotEnvironment(t *testing.T, telegramToken string, telegramOwner int, pathDictionaries string) { + t.Helper() + t.Setenv("TOKEN", telegramToken) + t.Setenv("OWNER", strconv.Itoa(telegramOwner)) + t.Setenv("DOCS_PATH", pathDictionaries) + resetConfig(t) } -func cleanTelegramBotEnvironment() { - _ = os.Unsetenv("TOKEN") - _ = os.Unsetenv("OWNER") - _ = os.Unsetenv("DOCS_PATH") - resetConfig() -} +func resetConfig(t *testing.T) { + t.Helper() -func resetConfig() { cfg = Config{} once = sync.Once{} -} \ No newline at end of file +}