-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence_test.go
More file actions
205 lines (186 loc) · 5.83 KB
/
Copy pathsequence_test.go
File metadata and controls
205 lines (186 loc) · 5.83 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
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
package env_test
import (
"reflect"
"testing"
"github.com/goloop/env/v2"
)
// TestSliceReplace checks that decoding replaces a slice (like encoding/json)
// instead of appending, so Unmarshal is idempotent and overrides defaults.
func TestSliceReplace(t *testing.T) {
type cfg struct {
A []int `env:"A" sep:","`
}
// Repeated decode is idempotent.
var s cfg
env.UnmarshalMap(map[string]string{"A": "1,2,3"}, &s)
env.UnmarshalMap(map[string]string{"A": "1,2,3"}, &s)
if !reflect.DeepEqual(s.A, []int{1, 2, 3}) {
t.Errorf("repeated decode: got %v, want [1 2 3]", s.A)
}
// An in-code default is replaced, not appended to.
d := cfg{A: []int{9, 9}}
env.UnmarshalMap(map[string]string{"A": "1,2"}, &d)
if !reflect.DeepEqual(d.A, []int{1, 2}) {
t.Errorf("default override: got %v, want [1 2]", d.A)
}
}
// TestPointerSliceDecode checks that []*scalar decodes (a nil element for an
// empty value) and round-trips.
func TestPointerSliceDecode(t *testing.T) {
type cfg struct {
A []*int `env:"A" sep:","`
}
var s cfg
if err := env.UnmarshalMap(map[string]string{"A": "1,,3"}, &s); err != nil {
t.Fatal(err)
}
if len(s.A) != 3 || *s.A[0] != 1 || s.A[1] != nil || *s.A[2] != 3 {
t.Fatalf("decode []*int: got %v", s.A)
}
// Round-trip: marshal -> "1,,3" -> decode back to the same shape.
m, err := env.MarshalMap(s)
if err != nil {
t.Fatal(err)
}
if m["A"] != "1,,3" {
t.Errorf("marshal []*int: got %q, want %q", m["A"], "1,,3")
}
var back cfg
env.UnmarshalMap(m, &back)
if len(back.A) != 3 || *back.A[0] != 1 || back.A[1] != nil || *back.A[2] != 3 {
t.Errorf("round-trip []*int: got %v", back.A)
}
}
// TestPresenceSemantics checks the encoding/json-style presence rules: an
// absent key leaves the field untouched (preserving in-code defaults), while a
// present but empty value sets the zero value.
func TestPresenceSemantics(t *testing.T) {
type cfg struct {
Port int `env:"PORT"`
Arr [3]int `env:"ARR" sep:","`
Sl []int `env:"SL" sep:","`
}
// Absent keys: every field keeps its in-code default.
def := cfg{Port: 8080, Arr: [3]int{9, 9, 9}, Sl: []int{7}}
if err := env.UnmarshalMap(map[string]string{}, &def); err != nil {
t.Fatal(err)
}
if def.Port != 8080 || def.Arr != [3]int{9, 9, 9} || !reflect.DeepEqual(def.Sl, []int{7}) {
t.Errorf("absent keys must leave fields untouched, got %+v", def)
}
// Present but empty: fields are zeroed/cleared.
empty := cfg{Port: 8080, Arr: [3]int{9, 9, 9}, Sl: []int{7}}
err := env.UnmarshalMap(map[string]string{"PORT": "", "ARR": "", "SL": ""}, &empty)
if err != nil {
t.Fatal(err)
}
if empty.Port != 0 || empty.Arr != [3]int{0, 0, 0} || len(empty.Sl) != 0 {
t.Errorf("present-empty must clear fields, got %+v", empty)
}
}
// TestNestedStructDefaultsPreserved checks that decoding a nested struct in
// place keeps sub-fields absent from the source at their existing values.
func TestNestedStructDefaultsPreserved(t *testing.T) {
type Inner struct {
A int `env:"A"`
B int `env:"B"`
}
type cfg struct {
In Inner `env:"IN"`
}
c := cfg{In: Inner{A: 1, B: 2}}
if err := env.UnmarshalMap(map[string]string{"IN_A": "10"}, &c); err != nil {
t.Fatal(err)
}
if c.In.A != 10 || c.In.B != 2 {
t.Errorf("nested defaults: got %+v, want {A:10 B:2}", c.In)
}
}
// TestFieldCacheRespectsOptions checks that the per-type field cache does not
// leak call-level settings: the same struct type decoded with different
// prefixes and separators must behave correctly each time.
func TestFieldCacheRespectsOptions(t *testing.T) {
type cfg struct {
Items []string `env:"ITEMS"`
Port int `env:"PORT"`
}
var a cfg
err := env.UnmarshalMap(
map[string]string{"A_ITEMS": "x:y", "A_PORT": "1"},
&a, env.WithPrefix("A"), env.WithSeparator(":"),
)
if err != nil {
t.Fatal(err)
}
if len(a.Items) != 2 || a.Items[0] != "x" || a.Port != 1 {
t.Errorf("call A (prefix A, sep ':'): got %+v", a)
}
var b cfg
err = env.UnmarshalMap(
map[string]string{"B_ITEMS": "p,q,r", "B_PORT": "2"},
&b, env.WithPrefix("B"),
)
if err != nil {
t.Fatal(err)
}
if len(b.Items) != 3 || b.Items[0] != "p" || b.Port != 2 {
t.Errorf("call B (prefix B, default sep): got %+v", b)
}
}
// TestSequenceRoundTrip checks that list values round-trip through both the map
// and the file paths, including elements that contain the separator (BUG-N1).
func TestSequenceRoundTrip(t *testing.T) {
type cfg struct {
V []string `env:"V" sep:","`
}
cases := [][]string{
{"a", "b"},
{"a,b", "c"}, // element contains the separator
{"a,b,c", "d"},
{"plain"},
{"x", "y,z", "w"},
{`x"y`, "z"}, // element contains a quote
{"(a", "b)"}, // elements contain brackets
{`a,"b`, "c"}, // element contains both the separator and a quote
{"", "x"}, // empty leading element
}
for _, want := range cases {
// Map round-trip.
m, err := env.MarshalMap(cfg{V: want})
if err != nil {
t.Fatalf("MarshalMap %v: %v", want, err)
}
var mb cfg
if err := env.UnmarshalMap(m, &mb); err != nil {
t.Fatalf("UnmarshalMap: %v", err)
}
if !reflect.DeepEqual(mb.V, want) {
t.Errorf("map round-trip: wire=%q got %v want %v", m["V"], mb.V, want)
}
// File round-trip.
path := t.TempDir() + "/seq.env"
if err := env.MarshalFile(path, cfg{V: want}); err != nil {
t.Fatal(err)
}
var fb cfg
if err := env.UnmarshalFile(path, &fb); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(fb.V, want) {
t.Errorf("file round-trip: got %v want %v", fb.V, want)
}
}
}
// TestSequenceManualQuoting checks that a hand-written quoted element decodes.
func TestSequenceManualQuoting(t *testing.T) {
type cfg struct {
Tags []string `env:"TAGS" sep:","`
}
var c cfg
if err := env.UnmarshalMap(map[string]string{"TAGS": `"a,b",c`}, &c); err != nil {
t.Fatal(err)
}
if len(c.Tags) != 2 || c.Tags[0] != "a,b" || c.Tags[1] != "c" {
t.Errorf(`TAGS="a,b",c -> %v, want ["a,b" "c"]`, c.Tags)
}
}