This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_test.go
199 lines (163 loc) · 4.64 KB
/
env_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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package env_test
import (
"encoding"
"encoding/base64"
"encoding/json"
"strings"
"testing"
"time"
"github.com/davidmdm/env"
"github.com/stretchr/testify/require"
)
type Custom struct {
Value any
}
func (c *Custom) UnmarshalText(data []byte) error {
return json.Unmarshal(data, &c.Value)
}
func TestVar(t *testing.T) {
var cfg struct {
int int
uint uint
float64 float64
boolean bool
duration time.Duration
string string
stringslice []string
custom Custom
mapint map[string]int
}
environment := env.MakeEnvSet(func() env.LookupFunc {
e := map[string]string{
"i": "-1",
"ui": "1",
"f": "3.14",
"b": "T",
"d": "5m",
"s": "hello",
"ss": "hello,world",
"custom": `[1,2,3]`,
"mapint": `x=3,y=1`,
}
return func(s string) (string, bool) {
value, ok := e[s]
return value, ok
}
}())
env.FlagVar(environment, &cfg.int, "i")
env.FlagVar(environment, &cfg.uint, "ui")
env.FlagVar(environment, &cfg.float64, "f")
env.FlagVar(environment, &cfg.boolean, "b")
env.FlagVar(environment, &cfg.string, "s")
env.FlagVar(environment, &cfg.duration, "d")
env.FlagVar(environment, &cfg.stringslice, "ss")
env.FlagVar(environment, &cfg.custom, "custom")
env.FlagVar(environment, &cfg.mapint, "mapint")
require.NoError(t, environment.Parse())
require.Equal(t, -1, cfg.int)
require.Equal(t, uint(1), cfg.uint)
require.Equal(t, 3.14, cfg.float64)
require.Equal(t, true, cfg.boolean)
require.Equal(t, "hello", cfg.string)
require.Equal(t, []string{"hello", "world"}, cfg.stringslice)
require.Equal(t, 5*time.Minute, cfg.duration)
require.EqualValues(t, []any{1.0, 2.0, 3.0}, cfg.custom.Value)
require.EqualValues(t, map[string]int{"x": 3, "y": 1}, cfg.mapint)
}
func TestMapParsingErrors(t *testing.T) {
e1 := env.MakeEnvSet(func(name string) (string, bool) {
return "3=4", true
})
var boolint map[bool]int
env.FlagVar(e1, &boolint, "BOOLINT")
var intbool map[int]bool
env.FlagVar(e1, &intbool, "INTBOOL")
errText := e1.Parse().Error()
require.Contains(
t,
errText,
`failed to parse BOOLINT: failed to parse key: 3: strconv.ParseBool: parsing "3": invalid syntax`,
)
require.Contains(
t,
errText,
`failed to parse INTBOOL: failed to parse value at key: 3: strconv.ParseBool: parsing "4": invalid syntax`,
)
}
type CapText string
var _ encoding.TextUnmarshaler = new(CapText)
func (text *CapText) UnmarshalText(data []byte) error {
*text = CapText(strings.ToUpper(string(data)))
return nil
}
func TestTextUnmarshaler(t *testing.T) {
var text CapText
environment := env.MakeEnvSet(func(s string) (string, bool) { return "value", true })
env.FlagVar(environment, &text, "VAR")
require.NoError(t, environment.Parse())
require.Equal(t, "VALUE", string(text))
}
type Base64Text string
var _ encoding.BinaryUnmarshaler = new(Base64Text)
func (text *Base64Text) UnmarshalBinary(data []byte) error {
result, err := base64.RawStdEncoding.DecodeString(string(data))
if err != nil {
return err
}
*text = Base64Text(result)
return nil
}
func TestBinaryUnmarshaler(t *testing.T) {
var text Base64Text
environment := env.MakeEnvSet(func(s string) (string, bool) { return "aGVsbG8gd29ybGQK", true })
env.FlagVar(environment, &text, "VAR")
require.NoError(t, environment.Parse())
require.Equal(t, "hello world\n", string(text))
}
func TestCommandLineArgsSource(t *testing.T) {
environment := env.MakeEnvSet(
env.CommandLineArgs("--database-url", "db", "-force", "--filter=*.sql", "-count", "42", "-input", "a", "--input", "b"),
)
var (
databaseURL string
force bool
filter string
count int
other string
input []string
)
env.FlagVar(environment, &databaseURL, "DATABASE_URL")
env.FlagVar(environment, &force, "FORCE")
env.FlagVar(environment, &filter, "FILTER")
env.FlagVar(environment, &count, "COUNT")
env.FlagVar(environment, &other, "OTHER")
env.FlagVar(environment, &input, "INPUT")
require.NoError(t, environment.Parse())
require.True(t, force)
require.Equal(t, "db", databaseURL)
require.Equal(t, "*.sql", filter)
require.Equal(t, 42, count)
require.Equal(t, "", other)
require.Equal(t, []string{"a", "b"}, input)
}
func TestMultipleLookups(t *testing.T) {
environment := env.MakeEnvSet(
env.CommandLineArgs("--max=42"),
func(name string) (string, bool) {
value, ok := map[string]string{
"MAX": "10",
"NAME": "bob",
}[name]
return value, ok
},
)
var (
max int
name string
)
env.FlagVar(environment, &max, "MAX")
env.FlagVar(environment, &name, "NAME")
require.NoError(t, environment.Parse())
require.Equal(t, 42, max)
require.Equal(t, "bob", name)
}