-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_test.go
More file actions
156 lines (138 loc) · 3.64 KB
/
Copy pathenv_test.go
File metadata and controls
156 lines (138 loc) · 3.64 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package struct2pflag_test
import (
"errors"
"testing"
"time"
"github.com/goark/struct2pflag"
)
type envNested struct {
Name string `env:"APP_NAME"`
}
type envConfig struct {
Enabled bool `env:"ENABLED"`
Count int `env:"COUNT"`
Rate float64 `env:"RATE"`
Wait time.Duration `env:"WAIT"`
Label string `env:"LABEL"`
Nested envNested
}
func TestBindEnvBasic(t *testing.T) {
t.Setenv("ENABLED", "true")
t.Setenv("COUNT", "12")
t.Setenv("RATE", "1.25")
t.Setenv("WAIT", "3s")
t.Setenv("LABEL", "alpha")
t.Setenv("APP_NAME", "demo")
cfg := envConfig{}
if err := struct2pflag.BindEnv(&cfg); err != nil {
t.Fatalf("BindEnv() error = %v", err)
}
if cfg.Enabled != true {
t.Fatalf("Enabled = %v, want true", cfg.Enabled)
}
if cfg.Count != 12 {
t.Fatalf("Count = %v, want 12", cfg.Count)
}
if cfg.Rate != 1.25 {
t.Fatalf("Rate = %v, want 1.25", cfg.Rate)
}
if cfg.Wait != 3*time.Second {
t.Fatalf("Wait = %v, want 3s", cfg.Wait)
}
if cfg.Label != "alpha" {
t.Fatalf("Label = %q, want alpha", cfg.Label)
}
if cfg.Nested.Name != "demo" {
t.Fatalf("Nested.Name = %q, want demo", cfg.Nested.Name)
}
}
func TestBindEnvUnsetPreservesDefaults(t *testing.T) {
cfg := envConfig{
Enabled: true,
Count: 5,
Rate: 2.0,
Wait: time.Minute,
Label: "default",
}
if err := struct2pflag.BindEnv(&cfg); err != nil {
t.Fatalf("BindEnv() error = %v", err)
}
if cfg.Enabled != true || cfg.Count != 5 || cfg.Rate != 2.0 || cfg.Wait != time.Minute || cfg.Label != "default" {
t.Fatalf("defaults were unexpectedly changed: %#v", cfg)
}
}
func TestBindEnvOptions(t *testing.T) {
t.Setenv("APP_PORT", "8080")
t.Setenv("APP_DEBUG", "true")
type cfg struct {
Port int `conf:"PORT"`
Debug bool `conf:"DEBUG"`
}
c := cfg{}
err := struct2pflag.BindEnv(&c,
struct2pflag.WithEnvTag("conf"),
struct2pflag.WithEnvPrefix("APP_"),
)
if err != nil {
t.Fatalf("BindEnv() error = %v", err)
}
if c.Port != 8080 || c.Debug != true {
t.Fatalf("BindEnv() applied unexpected values: %#v", c)
}
}
func TestBindEnvParseErrors(t *testing.T) {
type cfg struct {
N int `env:"N"`
B bool `env:"B"`
D time.Duration `env:"D"`
}
t.Run("int", func(t *testing.T) {
t.Setenv("N", "oops")
c := cfg{}
err := struct2pflag.BindEnv(&c)
if err == nil || !errors.Is(err, struct2pflag.ErrEnvParse) {
t.Fatalf("BindEnv() error = %v, want ErrEnvParse", err)
}
})
t.Run("bool", func(t *testing.T) {
t.Setenv("B", "oops")
c := cfg{}
err := struct2pflag.BindEnv(&c)
if err == nil || !errors.Is(err, struct2pflag.ErrEnvParse) {
t.Fatalf("BindEnv() error = %v, want ErrEnvParse", err)
}
})
t.Run("duration", func(t *testing.T) {
t.Setenv("D", "oops")
c := cfg{}
err := struct2pflag.BindEnv(&c)
if err == nil || !errors.Is(err, struct2pflag.ErrEnvParse) {
t.Fatalf("BindEnv() error = %v, want ErrEnvParse", err)
}
})
}
func TestBindEnvInvalidTarget(t *testing.T) {
type cfg struct {
Value int `env:"VALUE"`
}
var c cfg
err := struct2pflag.BindEnv(c)
if err == nil || !errors.Is(err, struct2pflag.ErrInvalidEnvTarget) {
t.Fatalf("BindEnv() error = %v, want ErrInvalidEnvTarget", err)
}
}
func TestBindEnvStrictUnsupportedType(t *testing.T) {
t.Setenv("ITEMS", "a,b,c")
type cfg struct {
Items []string `env:"ITEMS"`
}
c1 := cfg{}
if err := struct2pflag.BindEnv(&c1); err != nil {
t.Fatalf("BindEnv() strict=false error = %v", err)
}
c2 := cfg{}
err := struct2pflag.BindEnv(&c2, struct2pflag.WithStrict(true))
if err == nil || !errors.Is(err, struct2pflag.ErrUnsupportedEnvType) {
t.Fatalf("BindEnv() strict=true error = %v, want ErrUnsupportedEnvType", err)
}
}