test(config): add JSON round-trip test for environment config map

This commit is contained in:
2026-05-12 13:00:31 +01:00
parent ff12eb1cb1
commit 4beb769a2b
2 changed files with 22 additions and 0 deletions
+1
View File
@@ -34,6 +34,7 @@ func RunConvert(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
ctx := context.Background() ctx := context.Background()
var m map[string]any var m map[string]any
var err error var err error
if *input == "environ" { if *input == "environ" {
if *from != "env" { if *from != "env" {
_, _ = fmt.Fprintln(stderr, "convert: --input environ only works with --from env") _, _ = fmt.Fprintln(stderr, "convert: --input environ only works with --from env")
+21
View File
@@ -1,6 +1,8 @@
package config_test package config_test
import ( import (
"context"
"encoding/json"
"testing" "testing"
"github.com/eslider/go-config/env" "github.com/eslider/go-config/env"
@@ -38,6 +40,25 @@ DATABASE_URL=postgres://dotenv
env.WithCurrentEnvironment(), 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 var cfg motivationConfig
if err := yamlCfg.Unmarshal(&cfg); err != nil { if err := yamlCfg.Unmarshal(&cfg); err != nil {
t.Fatalf("yaml unmarshal: %v", err) t.Fatalf("yaml unmarshal: %v", err)