Skip to content

Commit 2c51af3

Browse files
feat: add more tests for env
1 parent e5192ff commit 2c51af3

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

env/env_test.go

+68-28
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,86 @@ package env
33
import (
44
"os"
55
"testing"
6+
7+
"github.com/go-playground/validator/v10"
68
)
79

10+
type Config struct {
11+
Config1 string `env:"CONFIG1"`
12+
Config2 int `env:"CONFIG2"`
13+
Config3 string `env:"CONFIG3"`
14+
}
15+
816
func TestLoad(t *testing.T) {
9-
// Make sure that the environment variables are not set.
10-
os.Clearenv()
1117

12-
t.Setenv("CONFIG1", "value1")
18+
t.Run("missing value", func(t *testing.T) {
19+
// Make sure that the environment variables are not set.
20+
os.Clearenv()
1321

14-
type Config struct {
15-
Config1 string `env:"CONFIG1"`
16-
Config2 int `env:"CONFIG2"`
17-
}
22+
cfg, err := Load[Config]()
23+
if err != nil {
24+
t.Fatal(err)
25+
}
1826

19-
cfg, err := Load[Config]("fixtures/test.env")
20-
if err != nil {
21-
t.Fatal(err)
22-
}
27+
validate(t, "CONFIG1", "", cfg.Config1)
28+
validate(t, "CONFIG2", 0, cfg.Config2)
29+
})
2330

24-
validate(t, "CONFIG1", "value1", cfg.Config1)
25-
validate(t, "CONFIG2", 2, cfg.Config2)
26-
}
31+
t.Run("load from env and file", func(t *testing.T) {
32+
// Make sure that the environment variables are not set.
33+
os.Clearenv()
2734

28-
func TestLoadMissingConfigFile(t *testing.T) {
29-
// Make sure that the environment variables are not set.
30-
os.Clearenv()
35+
t.Setenv("CONFIG1", "value1")
3136

32-
t.Setenv("CONFIG1", "value1")
37+
cfg, err := Load[Config]("fixtures/test.env")
38+
if err != nil {
39+
t.Fatal(err)
40+
}
3341

34-
type Config struct {
35-
Config1 string `env:"CONFIG1"`
36-
Config2 string `env:"CONFIG2"`
37-
}
42+
validate(t, "CONFIG1", "value1", cfg.Config1)
43+
validate(t, "CONFIG2", 2, cfg.Config2)
44+
})
3845

39-
cfg, err := Load[Config]("fixtures/missing.env")
40-
if err != nil {
41-
t.Fatal(err)
42-
}
46+
t.Run("env variables take precedence over file", func(t *testing.T) {
47+
// Make sure that the environment variables are not set.
48+
os.Clearenv()
49+
50+
t.Setenv("CONFIG2", "7")
51+
52+
cfg, err := Load[Config]("fixtures/test.env")
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
validate(t, "CONFIG2", 7, cfg.Config2)
58+
validate(t, "CONFIG3", "value3", cfg.Config3)
59+
})
60+
61+
t.Run("it fails on validation", func(t *testing.T) {
62+
// Make sure that the environment variables are not set.
63+
os.Clearenv()
64+
65+
t.Setenv("NON_ZERO_FLOAT", "1")
66+
67+
type Config struct {
68+
NonZeroFoat float32 `env:"NON_ZERO_FLOAT" validate:"required"`
69+
NonEmptyString string `env:"NON_EMPTY_STRING" validate:"required"`
70+
NonZeroInt int `env:"NON_ZERO_INT" validate:"required"`
71+
}
72+
73+
_, err := Load[Config]()
74+
if err == nil {
75+
t.Fatal("expected error")
76+
}
77+
78+
errs := err.(validator.ValidationErrors)
79+
if len(errs) != 2 {
80+
t.Fatalf("expected 2 errors, got %d", len(errs))
81+
}
4382

44-
validate(t, "CONFIG1", "value1", cfg.Config1)
45-
validate(t, "CONFIG2", "", cfg.Config2)
83+
validate(t, "NON_ZERO_INT", "Config.NonEmptyString", errs[0].StructNamespace())
84+
validate(t, "NON_EMPTY_STRING", "Config.NonZeroInt", errs[1].StructNamespace())
85+
})
4686
}
4787

4888
func validate[T comparable](t *testing.T, key string, expected T, got T) {

env/fixtures/test.env

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CONFIG2="2"
2+
CONFIG3="value3"

must/must.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package must provides a convenient way to panic on errors.
12
package must
23

34
// Must1 panics if err is not nil.

0 commit comments

Comments
 (0)