2020-12-16 00:06:46 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-07-05 16:26:57 +03:00
|
|
|
"context"
|
2020-12-16 00:06:46 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
envconfig "go.unistack.org/micro-config-env/v3"
|
|
|
|
"go.unistack.org/micro/v3/config"
|
2020-12-16 00:06:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
String string `env:"MICRO_TEST" default:"default"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiple(t *testing.T) {
|
2021-07-05 16:26:57 +03:00
|
|
|
ctx := context.Background()
|
2020-12-16 00:06:46 +03:00
|
|
|
cfg := &Config{}
|
2021-07-05 16:26:57 +03:00
|
|
|
|
|
|
|
c1 := config.NewConfig(config.Struct(cfg))
|
|
|
|
c2 := envconfig.NewConfig(config.Struct(cfg))
|
|
|
|
|
|
|
|
if err := c1.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := c2.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c1.Load(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := c2.Load(ctx); err != nil {
|
2020-12-16 00:06:46 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-05 16:26:57 +03:00
|
|
|
|
2020-12-16 00:06:46 +03:00
|
|
|
if cfg.String != "default" {
|
|
|
|
t.Fatalf("config not parsed by default source: %#+v\n", cfg)
|
|
|
|
}
|
|
|
|
os.Setenv("MICRO_TEST", "non_default")
|
2021-07-05 16:26:57 +03:00
|
|
|
if err := c1.Load(ctx, config.LoadOverride(true)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := c2.Load(ctx, config.LoadOverride(true)); err != nil {
|
2020-12-16 00:06:46 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cfg.String == "default" {
|
|
|
|
t.Fatalf("config not parsed by default source: %#+v\n", cfg)
|
|
|
|
}
|
|
|
|
if cfg.String != "non_default" {
|
|
|
|
t.Fatalf("config not parsed by default source: %#+v\n", cfg)
|
|
|
|
}
|
|
|
|
}
|