-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathtoml_test.go
244 lines (224 loc) · 5.77 KB
/
toml_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package toml_test
import (
"fmt"
"math"
"os/user"
"runtime"
"strconv"
"testing"
"time"
"github.com/google/go-cmp/cmp"
itoml "github.com/influxdata/influxdb/toml"
)
func TestSize_UnmarshalText(t *testing.T) {
var s itoml.Size
for _, test := range []struct {
str string
want uint64
}{
{"1", 1},
{"10", 10},
{"100", 100},
{"1k", 1 << 10},
{"10k", 10 << 10},
{"100k", 100 << 10},
{"1K", 1 << 10},
{"10K", 10 << 10},
{"100K", 100 << 10},
{"1m", 1 << 20},
{"10m", 10 << 20},
{"100m", 100 << 20},
{"1M", 1 << 20},
{"10M", 10 << 20},
{"100M", 100 << 20},
{"1g", 1 << 30},
{"1G", 1 << 30},
{fmt.Sprint(uint64(math.MaxUint64) - 1), math.MaxUint64 - 1},
} {
if err := s.UnmarshalText([]byte(test.str)); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if s != itoml.Size(test.want) {
t.Fatalf("wanted: %d got: %d", test.want, s)
}
}
for _, str := range []string{
fmt.Sprintf("%dk", uint64(math.MaxUint64-1)),
"10000000000000000000g",
"abcdef",
"1KB",
"√m",
"a1",
"",
} {
if err := s.UnmarshalText([]byte(str)); err == nil {
t.Fatalf("input should have failed: %s", str)
}
}
}
func TestFileMode_MarshalText(t *testing.T) {
for _, test := range []struct {
mode int
want string
}{
{mode: 0755, want: `0755`},
{mode: 0777, want: `0777`},
{mode: 01777, want: `1777`},
} {
mode := itoml.FileMode(test.mode)
if got, err := mode.MarshalText(); err != nil {
t.Errorf("unexpected error: %s", err)
} else if test.want != string(got) {
t.Errorf("wanted: %v got: %v", test.want, string(got))
}
}
}
func TestFileMode_UnmarshalText(t *testing.T) {
for _, test := range []struct {
str string
want uint32
}{
{str: ``, want: 0},
{str: `0777`, want: 0777},
{str: `777`, want: 0777},
{str: `1777`, want: 01777},
{str: `0755`, want: 0755},
} {
var mode itoml.FileMode
if err := mode.UnmarshalText([]byte(test.str)); err != nil {
t.Errorf("unexpected error: %s", err)
} else if mode != itoml.FileMode(test.want) {
t.Errorf("wanted: %04o got: %04o", test.want, mode)
}
}
}
func TestGroup_UnmarshalTOML(t *testing.T) {
// Skip this test on windows since it does not support setting the group anyway.
if runtime.GOOS == "windows" {
t.Skip("unsupported on windows")
}
// Find the current user ID so we can use that group name.
u, err := user.Current()
if err != nil {
t.Skipf("unable to find the current user: %s", err)
}
// Lookup the group by the group id.
gr, err := user.LookupGroupId(u.Gid)
if err == nil {
var group itoml.Group
if err := group.UnmarshalTOML(gr.Name); err != nil {
t.Fatalf("unexpected error: %s", err)
} else if got, want := u.Gid, strconv.Itoa(int(group)); got != want {
t.Fatalf("unexpected group id: %s != %s", got, want)
}
}
// Attempt to convert the group to an integer so we can test reading an integer.
gid, err := strconv.Atoi(u.Gid)
if err != nil {
t.Fatalf("group id is not an integer: %s", err)
}
var group itoml.Group
if err := group.UnmarshalTOML(int64(gid)); err != nil {
t.Fatalf("unexpected error: %s", err)
} else if int(group) != gid {
t.Fatalf("unexpected group id: %d != %d", gid, int(group))
}
}
func TestConfig_Encode(t *testing.T) {
t.Skip("TODO(jsternberg): rewrite this test to use something from platform")
//var c run.Config
//c.Coordinator.WriteTimeout = itoml.Duration(time.Minute)
//buf := new(bytes.Buffer)
//if err := toml.NewEncoder(buf).Encode(&c); err != nil {
// t.Fatal("Failed to encode: ", err)
//}
//got, search := buf.String(), `write-timeout = "1m0s"`
//if !strings.Contains(got, search) {
// t.Fatalf("Encoding config failed.\nfailed to find %s in:\n%s\n", search, got)
//}
}
func TestEnvOverride_Builtins(t *testing.T) {
envMap := map[string]string{
"X_STRING": "a string",
"X_DURATION": "1m1s",
"X_INT": "1",
"X_INT8": "2",
"X_INT16": "3",
"X_INT32": "4",
"X_INT64": "5",
"X_UINT": "6",
"X_UINT8": "7",
"X_UINT16": "8",
"X_UINT32": "9",
"X_UINT64": "10",
"X_BOOL": "true",
"X_FLOAT32": "11.5",
"X_FLOAT64": "12.5",
"X_NESTED_STRING": "a nested string",
"X_NESTED_INT": "13",
"X_ES": "an embedded string",
"X__": "-1", // This value should not be applied to the "ignored" field with toml tag -.
}
env := func(s string) string {
return envMap[s]
}
type nested struct {
Str string `toml:"string"`
Int int `toml:"int"`
}
type Embedded struct {
ES string `toml:"es"`
}
type all struct {
Str string `toml:"string"`
Dur itoml.Duration `toml:"duration"`
Int int `toml:"int"`
Int8 int8 `toml:"int8"`
Int16 int16 `toml:"int16"`
Int32 int32 `toml:"int32"`
Int64 int64 `toml:"int64"`
Uint uint `toml:"uint"`
Uint8 uint8 `toml:"uint8"`
Uint16 uint16 `toml:"uint16"`
Uint32 uint32 `toml:"uint32"`
Uint64 uint64 `toml:"uint64"`
Bool bool `toml:"bool"`
Float32 float32 `toml:"float32"`
Float64 float64 `toml:"float64"`
Nested nested `toml:"nested"`
Embedded
Ignored int `toml:"-"`
}
var got all
if err := itoml.ApplyEnvOverrides(env, "X", &got); err != nil {
t.Fatal(err)
}
exp := all{
Str: "a string",
Dur: itoml.Duration(time.Minute + time.Second),
Int: 1,
Int8: 2,
Int16: 3,
Int32: 4,
Int64: 5,
Uint: 6,
Uint8: 7,
Uint16: 8,
Uint32: 9,
Uint64: 10,
Bool: true,
Float32: 11.5,
Float64: 12.5,
Nested: nested{
Str: "a nested string",
Int: 13,
},
Embedded: Embedded{
ES: "an embedded string",
},
Ignored: 0,
}
if diff := cmp.Diff(got, exp); diff != "" {
t.Fatal(diff)
}
}