-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_options_test.go
127 lines (116 loc) · 3.69 KB
/
parser_options_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package envconf_test
import (
"errors"
"os"
"testing"
"github.com/antonmashko/envconf"
"github.com/antonmashko/envconf/option"
)
func TestPriority_FromFlag_Ok(t *testing.T) {
const expected = "from-flag-value"
os.Args = append(os.Args, "-ptest-field1="+expected)
os.Clearenv()
os.Setenv("TEST_FIELD", "from-env-value")
data := struct {
Field string `flag:"ptest-field1" env:"TEST_FIELD" default:"default-variable"`
}{}
ecfg := envconf.New()
if err := ecfg.Parse(&data, option.WithPriorityOrder(option.FlagVariable, option.EnvVariable, option.DefaultValue)); err != nil {
t.Fatal("Parse: ", err)
}
if data.Field != expected {
t.Fatalf("incorrect result. expected=%s actual=%s", expected, data.Field)
}
}
func TestPriority_FromEnv_Ok(t *testing.T) {
const expected = "from-env-value"
os.Args = append(os.Args, "-ptest-field2=from-flag-value")
os.Clearenv()
os.Setenv("TEST_FIELD", expected)
data := struct {
Field string `flag:"ptest-field2" env:"TEST_FIELD" default:"default-variable"`
}{}
ecfg := envconf.New()
if err := ecfg.Parse(&data, option.WithPriorityOrder(option.EnvVariable, option.FlagVariable, option.DefaultValue)); err != nil {
t.Fatal("Parse: ", err)
}
if data.Field != expected {
t.Fatalf("incorrect result. expected=%s actual=%s", expected, data.Field)
}
}
func TestPriority_FromDefault_Ok(t *testing.T) {
const expected = "default-variable"
os.Args = append(os.Args, "-ptest-field3=from-flag-value")
os.Clearenv()
os.Setenv("TEST_FIELD", "from-env-value")
data := struct {
Field string `flag:"ptest-field3" env:"TEST_FIELD" default:"default-variable"`
}{}
ecfg := envconf.New()
if err := ecfg.Parse(&data, option.WithPriorityOrder(option.DefaultValue, option.FlagVariable, option.EnvVariable)); err != nil {
t.Fatal("Parse: ", err)
}
if data.Field != expected {
t.Fatalf("incorrect result. expected=%s actual=%s", expected, data.Field)
}
}
func TestPriority_EmptyPriorityDefineFromFlag_Ok(t *testing.T) {
const expected = "from-flag-value"
os.Args = append(os.Args, "-ptest-field4=from-flag-value")
os.Clearenv()
os.Setenv("TEST_FIELD", "from-env-value")
data := struct {
Field string `flag:"ptest-field4" env:"TEST_FIELD" default:"default-variable"`
}{}
ecfg := envconf.New()
if err := ecfg.Parse(&data, option.WithPriorityOrder()); err != nil {
t.Fatal("Parse: ", err)
}
if data.Field != expected {
t.Fatalf("incorrect result. expected=%s actual=%s", expected, data.Field)
}
}
func TestPriority_InvalidConfigSourceDefineFromFlag_Ok(t *testing.T) {
const expected = "from-flag-value"
os.Args = append(os.Args, "-ptest-field5=from-flag-value")
os.Clearenv()
os.Setenv("TEST_FIELD", "from-env-value")
data := struct {
Field string `flag:"ptest-field5" env:"TEST_FIELD" default:"default-variable"`
}{}
ecfg := envconf.New()
if err := ecfg.Parse(&data, option.WithPriorityOrder(option.ConfigSource(123), option.ConfigSource(124))); err != nil {
t.Fatal("Parse: ", err)
}
if data.Field != expected {
t.Fatalf("incorrect result. expected=%s actual=%s", expected, data.Field)
}
}
func TestFlagParsed_Ok(t *testing.T) {
data := struct {
Field string `flag:"ptest-field6" env:"TEST_FIELD" default:"default-variable"`
}{}
var fb bool
err := envconf.Parse(&data, option.WithFlagParsed(func() error {
fb = true
return nil
}))
if err != nil {
t.Fatal("unexpected error: ", err)
}
if !fb {
t.Fatal("callback not invoked: ", err)
}
}
func TestFlagParsed_Err(t *testing.T) {
cErr := errors.New("custom error")
data := struct {
Field string `flag:"ptest-field7" env:"TEST_FIELD" default:"default-variable"`
}{}
err := envconf.Parse(&data, option.WithFlagParsed(func() error {
return cErr
}))
if err != cErr {
t.Fatal("wrong error:", err)
}
}