Skip to content

Commit

Permalink
fix: alias values were not being set properly
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroEduardo committed Oct 25, 2024
1 parent a738047 commit 26a946f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
9 changes: 0 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
89 changes: 89 additions & 0 deletions internal/config/integration_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}

0 comments on commit 26a946f

Please sign in to comment.