-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
env_generic_test.go
46 lines (39 loc) · 936 Bytes
/
env_generic_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
//go:build go1.18
// +build go1.18
package env
import "testing"
type Conf struct {
Foo string `env:"FOO" envDefault:"bar"`
}
func TestParseAs(t *testing.T) {
config, err := ParseAs[Conf]()
isNoErr(t, err)
isEqual(t, "bar", config.Foo)
}
func TestParseAsWithOptions(t *testing.T) {
config, err := ParseAsWithOptions[Conf](Options{
Environment: map[string]string{
"FOO": "not bar",
},
})
isNoErr(t, err)
isEqual(t, "not bar", config.Foo)
}
type ConfRequired struct {
Foo string `env:"FOO,required"`
}
func TestMust(t *testing.T) {
t.Run("error", func(t *testing.T) {
defer func() {
err := recover()
isErrorWithMessage(t, err.(error), `env: required environment variable "FOO" is not set`)
}()
conf := Must(ParseAs[ConfRequired]())
isEqual(t, "", conf.Foo)
})
t.Run("success", func(t *testing.T) {
t.Setenv("FOO", "bar")
conf := Must(ParseAs[ConfRequired]())
isEqual(t, "bar", conf.Foo)
})
}