2020-12-04 02:37:48 +03:00
|
|
|
package env_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2020-12-20 00:26:18 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-01-16 02:12:21 +03:00
|
|
|
env "github.com/unistack-org/micro-config-env/v3"
|
2020-12-20 00:26:18 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config"
|
2020-12-04 02:37:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-12-20 00:26:18 +03:00
|
|
|
StringValue string `env:"STRING_VALUE"`
|
|
|
|
BoolValue bool `env:"BOOL_VALUE"`
|
|
|
|
StringSlice []string `env:"STRING_SLICE"`
|
|
|
|
IntSlice []int `env:"INT_SLICE"`
|
2020-12-04 22:41:46 +03:00
|
|
|
MapStringValue map[string]string `env:"MAP_STRING"`
|
2020-12-20 00:26:18 +03:00
|
|
|
MapIntValue map[string]int `env:"MAP_INT"`
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnv(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
conf := &Config{StringValue: "before_load"}
|
|
|
|
cfg := env.NewConfig(config.Struct(conf))
|
|
|
|
|
|
|
|
if err := cfg.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-12-20 00:26:18 +03:00
|
|
|
if err := cfg.Load(ctx); err != nil {
|
2020-12-04 02:37:48 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.StringValue != "before_load" {
|
2020-12-20 00:26:18 +03:00
|
|
|
t.Fatalf("something wrong with env config: %#+v", conf)
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 00:26:18 +03:00
|
|
|
os.Setenv("STRING_VALUE", "STRING_VALUE")
|
|
|
|
os.Setenv("BOOL_VALUE", "true")
|
2020-12-04 21:52:21 +03:00
|
|
|
os.Setenv("STRING_SLICE", "STRING_SLICE1,STRING_SLICE2;STRING_SLICE3")
|
|
|
|
os.Setenv("INT_SLICE", "1,2,3,4,5")
|
2020-12-04 22:41:46 +03:00
|
|
|
os.Setenv("MAP_STRING", "key1=val1,key2=val2")
|
2020-12-04 22:44:00 +03:00
|
|
|
os.Setenv("MAP_INT", "key1=1,key2=2")
|
2020-12-04 02:37:48 +03:00
|
|
|
|
2020-12-20 00:26:18 +03:00
|
|
|
if err := cfg.Load(ctx); err != nil {
|
2020-12-04 02:37:48 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if conf.StringValue != "STRING_VALUE" {
|
2020-12-20 00:26:18 +03:00
|
|
|
t.Fatalf("something wrong with env config: %#+v", conf)
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !conf.BoolValue {
|
2020-12-20 00:26:18 +03:00
|
|
|
t.Fatalf("something wrong with env config: %#+v", conf)
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 21:52:21 +03:00
|
|
|
if len(conf.StringSlice) != 3 {
|
2020-12-20 00:26:18 +03:00
|
|
|
t.Fatalf("something wrong with env config: %#+v", conf.StringSlice)
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|
2020-12-04 21:52:21 +03:00
|
|
|
|
2020-12-04 22:44:00 +03:00
|
|
|
if len(conf.MapStringValue) != 2 {
|
2020-12-20 00:26:18 +03:00
|
|
|
t.Fatalf("something wrong with env config: %#+v", conf.MapStringValue)
|
2020-12-04 22:44:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(conf.MapIntValue) != 2 {
|
2020-12-20 00:26:18 +03:00
|
|
|
t.Fatalf("something wrong with env config: %#+v", conf.MapIntValue)
|
2020-12-04 22:44:00 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 00:26:18 +03:00
|
|
|
t.Logf("cfg %#+v", conf)
|
2020-12-04 02:37:48 +03:00
|
|
|
}
|