forked from ryankurte/go-structparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envmapper_test.go
53 lines (39 loc) · 993 Bytes
/
envmapper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* go-structparse
* Copyright 2017 Ryan Kurte
*/
package structparse
import (
"fmt"
"io/ioutil"
"os"
"testing"
"gopkg.in/yaml.v2"
"github.com/stretchr/testify/assert"
)
func TestEnvMap(t *testing.T) {
prefix := "TEST_"
delimiter := "$"
t.Run("Infills strings in structure from environment", func(t *testing.T) {
os.Setenv(fmt.Sprintf("%s%s", prefix, "NAME"), "APP_NAME")
os.Setenv(fmt.Sprintf("%s%s", prefix, "HOSTNAME"), "localhost")
os.Setenv(fmt.Sprintf("%s%s", prefix, "PORT"), "9009")
c := struct {
Name string
Host struct {
Name string
Port string
}
}{}
// Load configuration file
data, err := ioutil.ReadFile("./example.yml")
assert.Nil(t, err)
// Unmarshal from yaml
err = yaml.Unmarshal(data, &c)
assert.Nil(t, err)
Strings(NewEnvironmentMapper(delimiter, prefix), &c)
assert.EqualValues(t, "APP_NAME", c.Name)
assert.EqualValues(t, "localhost", c.Host.Name)
assert.EqualValues(t, "9009", c.Host.Port)
})
}