-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_test.go
More file actions
72 lines (64 loc) · 1.64 KB
/
Copy pathvalidator_test.go
File metadata and controls
72 lines (64 loc) · 1.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
package fastconf_test
import (
"context"
"errors"
"testing"
"testing/fstest"
"github.com/fastabc/fastconf"
)
type validatorCfg struct {
Server struct {
Addr string `yaml:"addr"`
} `yaml:"server"`
}
func validatorFS(addr string) fstest.MapFS {
return fstest.MapFS{
"conf.d/base/00-app.yaml": &fstest.MapFile{
Data: []byte("server:\n addr: \"" + addr + "\"\n"),
},
}
}
func TestWithValidator_BlocksBadConfig(t *testing.T) {
_, err := fastconf.New[validatorCfg](context.Background(),
fastconf.WithFS(validatorFS("")), fastconf.WithDir("conf.d"),
fastconf.WithValidator(func(c *validatorCfg) error {
if c.Server.Addr == "" {
return errors.New("server.addr required")
}
return nil
}),
)
if err == nil {
t.Fatalf("expected validator error, got nil")
}
if !errors.Is(err, fastconf.ErrValidator) {
t.Fatalf("want ErrValidator, got %v", err)
}
}
func TestWithValidator_AllowsGoodConfig(t *testing.T) {
cfg, err := fastconf.New[validatorCfg](context.Background(),
fastconf.WithFS(validatorFS(":8080")), fastconf.WithDir("conf.d"),
fastconf.WithValidator(func(c *validatorCfg) error {
if c.Server.Addr == "" {
return errors.New("required")
}
return nil
}),
)
if err != nil {
t.Fatalf("New: %v", err)
}
defer cfg.Close()
if got := cfg.Get().Server.Addr; got != ":8080" {
t.Fatalf("got %q", got)
}
}
func TestWithValidator_NilSafe(t *testing.T) {
_, err := fastconf.New[validatorCfg](context.Background(),
fastconf.WithFS(validatorFS(":1")), fastconf.WithDir("conf.d"),
fastconf.WithValidator[validatorCfg](nil),
)
if err != nil {
t.Fatalf("nil validator should be a no-op, got %v", err)
}
}