Repository navigation
Expand file tree
/
Copy pathutils_test.go
More file actions
259 lines (243 loc) · 4.47 KB
/
Copy pathutils_test.go
File metadata and controls
259 lines (243 loc) · 4.47 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import "testing"
func Test_parseEnvLine(t *testing.T) {
tests := []struct {
name string
line string
wantKey string
wantValue string
wantErr bool
}{
{
name: "empty",
line: "",
wantKey: "",
wantValue: "",
},
{
name: "comment",
line: "# comment",
wantKey: "",
wantValue: "",
},
{
name: "comment with space",
line: " # comment",
wantKey: "",
wantValue: "",
},
{
name: "key lowercase and value",
line: "key=value",
wantKey: "KEY",
wantValue: "value",
},
{
name: "key uppercase and value",
line: "KEY=value",
wantKey: "KEY",
wantValue: "value",
},
{
name: "multi-equals",
line: "KEY=value=1",
wantKey: "KEY",
wantValue: "value=1",
},
{
name: "no key",
line: "=value1",
wantErr: true,
},
{
name: "no separator",
line: "KEYvalue1",
wantErr: true,
},
{
name: "no value",
line: "KEY=",
wantKey: "KEY",
wantValue: "",
},
{
name: "no value with space",
line: "KEY= ",
wantKey: "KEY",
wantValue: "",
},
{
name: "quoted value",
line: "KEY=\"value1\"",
wantKey: "KEY",
wantValue: "value1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotKey, gotValue, err := parseEnvLine(tt.line)
if (err != nil) != tt.wantErr {
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotKey != tt.wantKey {
t.Errorf("got key = %q, want %q", gotKey, tt.wantKey)
}
if gotValue != tt.wantValue {
t.Errorf("got value = %q, want %q", gotValue, tt.wantValue)
}
})
}
}
func TestMergeMap(t *testing.T) {
tests := []struct {
name string
dest map[string]any
src map[string]any
expected map[string]any
}{
{
name: "merge simple maps",
dest: map[string]any{
"key1": "value1",
},
src: map[string]any{
"key2": "value2",
},
expected: map[string]any{
"key1": "value1",
"key2": "value2",
},
},
{
name: "override existing key",
dest: map[string]any{
"key": "oldvalue",
},
src: map[string]any{
"key": "newvalue",
},
expected: map[string]any{
"key": "newvalue",
},
},
{
name: "merge nested maps",
dest: map[string]any{
"config": map[string]any{
"existing": "value",
},
},
src: map[string]any{
"config": map[string]any{
"new": "value",
},
},
expected: map[string]any{
"config": map[string]any{
"existing": "value",
"new": "value",
},
},
},
{
name: "override nested value",
dest: map[string]any{
"config": map[string]any{
"key": "oldvalue",
},
},
src: map[string]any{
"config": map[string]any{
"key": "newvalue",
},
},
expected: map[string]any{
"config": map[string]any{
"key": "newvalue",
},
},
},
{
name: "replace non-map with map",
dest: map[string]any{
"key": "stringvalue",
},
src: map[string]any{
"key": map[string]any{
"nested": "value",
},
},
expected: map[string]any{
"key": map[string]any{
"nested": "value",
},
},
},
{
name: "nil dest map",
dest: nil,
src: map[string]any{
"key": "value",
},
expected: map[string]any{
"key": "value",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := mergeMap(tt.dest, tt.src)
if !mapsEqual(result, tt.expected) {
t.Errorf("mergeMap() = %v, want %v", result, tt.expected)
}
})
}
}
// mapsEqual compares two maps for equality, handling nested maps
func mapsEqual(a, b map[string]any) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
bv, exists := b[k]
if !exists {
return false
}
if !valuesEqual(v, bv) {
return false
}
}
return true
}
// valuesEqual compares two values for equality, handling nested maps and slices
func valuesEqual(a, b any) bool {
switch av := a.(type) {
case map[string]any:
if bv, ok := b.(map[string]any); ok {
return mapsEqual(av, bv)
}
return false
case []any:
if bv, ok := b.([]any); ok {
return slicesEqual(av, bv)
}
return false
case nil:
return b == nil
default:
return a == b
}
}
// slicesEqual compares two slices for equality
func slicesEqual(a, b []any) bool {
if len(a) != len(b) {
return false
}
for i, av := range a {
if !valuesEqual(av, b[i]) {
return false
}
}
return true
}