-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapi_test.go
169 lines (158 loc) · 3.55 KB
/
api_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
package config_test
import (
"net/url"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/JeremyLoy/config"
)
func Test_Api(t *testing.T) {
// cannot be Parallelized as it manipulates env vars.
// It must clear env afterwards to avoid bleeding env changes.
type D struct {
E bool
F bool `config:"feRdiNaND"` // case insensitive
J *int
}
type testConfig struct {
A int `config:" "` // no-op/ignored
B string `config:"B"` // no effect
C []int
D D `config:"dOg"` // case-insensitive for sub-configs
G []int
H uint8
I string
K string
L time.Duration
M int8
U *url.URL
}
wantUrl := "https://google.com/search?q=golang"
wantUrlParsed, err := url.Parse(wantUrl)
if err != nil {
t.Fatalf("could not parse wanted URL: %v", err)
}
file, err := os.CreateTemp("", "testenv")
if err != nil {
t.Fatalf("failed to create temporary file: %v", err)
}
defer os.Remove(file.Name())
eightHours, err := time.ParseDuration("8h")
if err != nil {
panic(err)
}
testData := strings.Join([]string{
"A=1",
"B=abc",
"C=4 5 6",
"DoG__E=true",
"DoG__FErDINANd=true",
// should NOT log dog_j as it is not provided
"G=1 y 2", // should log G[1] as it is an incorrect type, but still work with 0 and 2
"H=-84", // should log H as it is an incorrect type
"I=", // should NOT log I as there is no way to tell if it is missing or deliberately empty
"L=8h",
"M=128", // should fail as it is out of bounds for an int8
"U=" + wantUrl,
}, "\n")
_, err = file.Write([]byte(testData))
if err != nil {
t.Fatalf("failed to write test data to temp file: %v", err)
}
err = os.Setenv("B", "overridden")
if err != nil {
t.Fatalf("failed to set environ: %v", err)
}
err = os.Setenv("C", "") // this should NOT override as it is empty
if err != nil {
t.Fatalf("failed to set environ: %v", err)
}
got := testConfig{
K: "hardcoded",
}
var sub D
want := testConfig{
A: 1,
B: "overridden",
C: []int{4, 5, 6},
D: D{
E: true,
F: true,
J: nil,
},
G: []int{1, 2},
H: 0,
I: "",
K: "hardcoded",
L: eightHours,
M: 0,
U: wantUrlParsed,
}
wantError := "config: the following fields had errors: [file[nonexistfile] g[1] h m]"
builder := config.From(file.Name()).From("nonexistfile").FromOptional("non existent and I shouldn't be listed in err").FromEnv()
gotErr := builder.To(&got)
_ = builder.Sub(&sub, "dog")
if !reflect.DeepEqual(got, want) {
t.Errorf("Integration: got %+v, want %+v", got, want)
}
if !reflect.DeepEqual(sub, want.D) {
t.Errorf("Integration: got %+v, want %+v", sub, want.D)
}
if gotErr == nil {
t.Errorf("Integration: should have had an error")
}
if !reflect.DeepEqual(gotErr.Error(), wantError) {
t.Errorf("Integration: gotFailedFields %+v, wantFailedFields %+v", gotErr, wantError)
}
os.Clearenv()
}
func Test_shouldPanic(t *testing.T) {
t.Parallel()
var s struct{}
var i int
tests := []struct {
name string
target interface{}
wantPanic bool
}{
{
name: "*struct",
target: &s,
wantPanic: false,
},
{
name: "struct",
target: s,
wantPanic: true,
},
{
name: "*int",
target: &i,
wantPanic: true,
},
{
name: "int",
target: i,
wantPanic: true,
},
{
name: "nil",
target: nil,
wantPanic: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); (r == nil) == tt.wantPanic {
t.Errorf("should have caused a panic")
}
}()
_ = config.FromEnv().To(tt.target)
})
}
}