You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
887 B
Go

package envutil
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestString(t *testing.T) {
t.Setenv("APP_VALUE", "configured")
require.Equal(t, "configured", String("APP_VALUE", "fallback"))
}
func TestStringFallback(t *testing.T) {
t.Setenv("APP_VALUE", "")
require.Equal(t, "fallback", String("APP_VALUE", "fallback"))
}
func TestInt(t *testing.T) {
t.Setenv("APP_PORT", "8080")
require.Equal(t, 8080, Int("APP_PORT", 3000))
}
func TestIntFallbackInvalid(t *testing.T) {
t.Setenv("APP_PORT", "oops")
require.Equal(t, 3000, Int("APP_PORT", 3000))
}
func TestDuration(t *testing.T) {
t.Setenv("APP_TIMEOUT", "7s")
require.Equal(t, 7*time.Second, Duration("APP_TIMEOUT", time.Second))
}
func TestDurationFallbackInvalid(t *testing.T) {
t.Setenv("APP_TIMEOUT", "oops")
require.Equal(t, 2*time.Second, Duration("APP_TIMEOUT", 2*time.Second))
}