diff --git a/internal/config/config.go b/internal/config/config.go index deebba0..2c051e3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -50,14 +50,5 @@ func Configure(reset ...bool) { viper.SetEnvPrefix(project.Name) viper.AutomaticEnv() - // Default configuration values - for _, config := range defaultConfigs { - viper.SetDefault(config.GetKey(), config.GetDefault()) - - for _, alias := range config.GetAliases() { - viper.SetDefault(alias, config.GetDefault()) - } - } - configured = true } diff --git a/internal/config/integration_test.go b/internal/config/integration_test.go new file mode 100644 index 0000000..5a8ae15 --- /dev/null +++ b/internal/config/integration_test.go @@ -0,0 +1,89 @@ +package config + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestAlias(t *testing.T) { + t.Run("default value", func(t *testing.T) { + var TestVariable = Create(&ViperConfigKey{ + Key: "test.variable", + Aliases: []string{}, + Default: "take", + }) + Configure(true) + + require.Equal(t, "take", TestVariable.Get()) + }) + t.Run("default is nil", func(t *testing.T) { + var TestVariable = Create(&ViperConfigKey{ + Key: "test.variable", + Aliases: []string{}, + Default: nil, + }) + + Configure(true) + + require.Equal(t, "", TestVariable.Get()) + require.Equal(t, false, TestVariable.GetBool()) + require.Equal(t, time.Duration(0), TestVariable.GetDuration()) + require.Equal(t, 0, TestVariable.GetInt()) + }) + t.Run("main only", func(t *testing.T) { + t.Setenv("DECKARD_TEST_VARIABLE", "takenet") + + var TestVariable = Create(&ViperConfigKey{ + Key: "test.variable", + Aliases: []string{}, + Default: "take", + }) + + Configure(true) + + require.Equal(t, "takenet", TestVariable.Get()) + }) + t.Run("alias only", func(t *testing.T) { + t.Setenv("DECKARD_ANOTHER_TEST_VARIABLE", "blip") + + var TestVariable = Create(&ViperConfigKey{ + Key: "test.variable", + Aliases: []string{"another_test.variable"}, + Default: "take", + }) + + Configure(true) + + require.Equal(t, "blip", TestVariable.Get()) + }) + t.Run("main and alias", func(t *testing.T) { + t.Setenv("DECKARD_TEST_VARIABLE", "takenet") + t.Setenv("DECKARD_ANOTHER_TEST_VARIABLE", "blip") + + var TestVariable = Create(&ViperConfigKey{ + Key: "test.variable", + Aliases: []string{"another_test.variable"}, + Default: "take", + }) + + Configure(true) + + require.Equal(t, "takenet", TestVariable.Get()) + }) + t.Run("multiple aliases", func(t *testing.T) { + t.Setenv("DECKARD_ANOTHER_TEST_VARIABLE", "blip") + t.Setenv("DECKARD_THIS_TEST_VARIABLE", "takenet") + + var TestVariable = Create(&ViperConfigKey{ + Key: "test.variable", + Aliases: []string{"another_test.variable", "this_test.variable"}, + Default: "take", + }) + + Configure(true) + + require.Equal(t, "blip", TestVariable.Get()) + }) +}