-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf_test.go
More file actions
154 lines (144 loc) · 3.69 KB
/
conf_test.go
File metadata and controls
154 lines (144 loc) · 3.69 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
package confstore
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/go-sphere/confstore/codec"
"github.com/go-sphere/confstore/provider"
)
type appConf struct {
Addr string `json:"addr"`
Mode string `json:"mode"`
}
func TestLoadWithFileJSON(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "config.json")
content := []byte(`{"addr":"127.0.0.1:8080","mode":"dev"}`)
if err := os.WriteFile(p, content, 0o644); err != nil {
t.Fatalf("write temp file: %v", err)
}
cfg, err := Load[appConf](provider.ReaderFunc(func(ctx context.Context) ([]byte, error) {
return os.ReadFile(p)
}), codec.JsonCodec())
if err != nil {
t.Fatalf("Load error: %v", err)
}
if cfg.Addr != "127.0.0.1:8080" || cfg.Mode != "dev" {
t.Fatalf("unexpected config: %+v", cfg)
}
}
func TestLoadWithContextCases(t *testing.T) {
decodeErr := errors.New("decode failed")
readErr := errors.New("read failed")
tests := []struct {
name string
reader provider.Provider
codec codec.Codec
want appConf
wantErr error
}{
{
name: "success",
reader: provider.ReaderFunc(func(ctx context.Context) ([]byte, error) {
return []byte(`{"addr":"127.0.0.1:8080","mode":"dev"}`), nil
}),
codec: codec.JsonCodec(),
want: appConf{Addr: "127.0.0.1:8080", Mode: "dev"},
},
{
name: "provider-error",
reader: provider.ReaderFunc(func(ctx context.Context) ([]byte, error) {
return nil, readErr
}),
codec: codec.JsonCodec(),
wantErr: readErr,
},
{
name: "codec-error",
reader: provider.ReaderFunc(func(ctx context.Context) ([]byte, error) {
return []byte("bad"), nil
}),
codec: codec.NewCodec(func(val any) ([]byte, error) { return nil, nil }, func(data []byte, val any) error {
return decodeErr
}),
wantErr: decodeErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := LoadWithContext[appConf](context.Background(), tt.reader, tt.codec)
if tt.wantErr != nil {
if err == nil {
t.Fatalf("expected error, got nil")
}
if !errors.Is(err, tt.wantErr) {
t.Fatalf("expected error %v, got %v", tt.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg == nil {
t.Fatal("expected config, got nil")
}
if *cfg != tt.want {
t.Fatalf("got %+v, want %+v", *cfg, tt.want)
}
})
}
}
func TestFillWithContextCases(t *testing.T) {
decodeErr := errors.New("decode failed")
tests := []struct {
name string
reader provider.Provider
codec codec.Codec
cfg *appConf
want appConf
wantErr error
}{
{
name: "success",
reader: provider.ReaderFunc(func(ctx context.Context) ([]byte, error) {
return []byte(`{"addr":"127.0.0.1:8080","mode":"prod"}`), nil
}),
codec: codec.JsonCodec(),
cfg: &appConf{},
want: appConf{Addr: "127.0.0.1:8080", Mode: "prod"},
},
{
name: "codec-error",
reader: provider.ReaderFunc(func(ctx context.Context) ([]byte, error) {
return []byte("bad"), nil
}),
codec: codec.NewCodec(func(val any) ([]byte, error) { return nil, nil }, func(data []byte, val any) error {
return decodeErr
}),
cfg: &appConf{},
wantErr: decodeErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := FillWithContext(context.Background(), tt.reader, tt.codec, tt.cfg)
if tt.wantErr != nil {
if err == nil {
t.Fatalf("expected error, got nil")
}
if !errors.Is(err, tt.wantErr) {
t.Fatalf("expected error %v, got %v", tt.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if *tt.cfg != tt.want {
t.Fatalf("got %+v, want %+v", *tt.cfg, tt.want)
}
})
}
}