test(config): add JSON round-trip test for environment config map
This commit is contained in:
@@ -34,6 +34,7 @@ func RunConvert(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
ctx := context.Background()
|
||||
var m map[string]any
|
||||
var err error
|
||||
|
||||
if *input == "environ" {
|
||||
if *from != "env" {
|
||||
_, _ = fmt.Fprintln(stderr, "convert: --input environ only works with --from env")
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/eslider/go-config/env"
|
||||
@@ -38,6 +40,25 @@ DATABASE_URL=postgres://dotenv
|
||||
env.WithCurrentEnvironment(),
|
||||
)
|
||||
|
||||
ctx := context.Background()
|
||||
m, err := envCfg.Map(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("env marshal: %v", err)
|
||||
}
|
||||
if m == nil {
|
||||
t.Fatalf("env marshal returned nil map")
|
||||
}
|
||||
|
||||
// convert m to json and back to map
|
||||
js, err := json.MarshalIndent(m, "", " ")
|
||||
if err != nil {
|
||||
t.Fatalf("json marshal: %v", err)
|
||||
}
|
||||
m2 := make(map[string]interface{})
|
||||
if err := json.Unmarshal(js, &m2); err != nil {
|
||||
t.Fatalf("json unmarshal: %v", err)
|
||||
}
|
||||
|
||||
var cfg motivationConfig
|
||||
if err := yamlCfg.Unmarshal(&cfg); err != nil {
|
||||
t.Fatalf("yaml unmarshal: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user