@@ -14,12 +14,19 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package expansion
17
+ package expansion_test
18
18
19
19
import (
20
20
"testing"
21
+
22
+ . "sigs.k8s.io/kustomize/pkg/expansion"
21
23
)
22
24
25
+ type expected struct {
26
+ count int
27
+ edited string
28
+ }
29
+
23
30
func TestMapReference (t * testing.T ) {
24
31
type env struct {
25
32
Name string
@@ -46,21 +53,23 @@ func TestMapReference(t *testing.T) {
46
53
"BLU" : "$(ZOO)-2" ,
47
54
}
48
55
49
- mapping := MappingFuncFor (declaredEnv )
56
+ counts := make (map [string ]int )
57
+ mapping := MappingFuncFor (counts , declaredEnv )
50
58
51
59
for _ , env := range envs {
52
60
declaredEnv [env .Name ] = Expand (env .Value , mapping )
53
61
}
54
62
55
- expectedEnv := map [string ]string {
56
- "FOO" : "bar" ,
57
- "ZOO" : "bar-1" ,
58
- "BLU" : "bar-1-2" ,
63
+ expectedEnv := map [string ]expected {
64
+ "FOO" : { count : 1 , edited : "bar" } ,
65
+ "ZOO" : { count : 1 , edited : "bar-1" } ,
66
+ "BLU" : { count : 0 , edited : "bar-1-2" } ,
59
67
}
60
68
61
69
for k , v := range expectedEnv {
62
- if e , a := v , declaredEnv [k ]; e != a {
63
- t .Errorf ("Expected %v, got %v" , e , a )
70
+ if e , a := v , declaredEnv [k ]; e .edited != a || e .count != counts [k ] {
71
+ t .Errorf ("Expected %v count=%d, got %v count=%d" ,
72
+ e .edited , e .count , a , counts [k ])
64
73
} else {
65
74
delete (declaredEnv , k )
66
75
}
@@ -79,9 +88,7 @@ func TestMapping(t *testing.T) {
79
88
"VAR_REF" : "$(VAR_A)" ,
80
89
"VAR_EMPTY" : "" ,
81
90
}
82
- mapping := MappingFuncFor (context )
83
-
84
- doExpansionTest (t , mapping )
91
+ doExpansionTest (t , context )
85
92
}
86
93
87
94
func TestMappingDual (t * testing.T ) {
@@ -94,51 +101,64 @@ func TestMappingDual(t *testing.T) {
94
101
"VAR_C" : "C" ,
95
102
"VAR_REF" : "$(VAR_A)" ,
96
103
}
97
- mapping := MappingFuncFor (context , context2 )
98
104
99
- doExpansionTest (t , mapping )
105
+ doExpansionTest (t , context , context2 )
100
106
}
101
107
102
- func doExpansionTest (t * testing.T , mapping func ( string ) string ) {
108
+ func doExpansionTest (t * testing.T , context ... map [ string ] string ) {
103
109
cases := []struct {
104
110
name string
105
111
input string
106
112
expected string
113
+ counts map [string ]int
107
114
}{
108
115
{
109
116
name : "whole string" ,
110
117
input : "$(VAR_A)" ,
111
118
expected : "A" ,
119
+ counts : map [string ]int {"VAR_A" : 1 },
112
120
},
113
121
{
114
122
name : "repeat" ,
115
123
input : "$(VAR_A)-$(VAR_A)" ,
116
124
expected : "A-A" ,
125
+ counts : map [string ]int {"VAR_A" : 2 },
126
+ },
127
+ {
128
+ name : "multiple repeats" ,
129
+ input : "$(VAR_A)-$(VAR_B)-$(VAR_B)-$(VAR_B)-$(VAR_A)" ,
130
+ expected : "A-B-B-B-A" ,
131
+ counts : map [string ]int {"VAR_A" : 2 , "VAR_B" : 3 },
117
132
},
118
133
{
119
134
name : "beginning" ,
120
135
input : "$(VAR_A)-1" ,
121
136
expected : "A-1" ,
137
+ counts : map [string ]int {"VAR_A" : 1 },
122
138
},
123
139
{
124
140
name : "middle" ,
125
141
input : "___$(VAR_B)___" ,
126
142
expected : "___B___" ,
143
+ counts : map [string ]int {"VAR_B" : 1 },
127
144
},
128
145
{
129
146
name : "end" ,
130
147
input : "___$(VAR_C)" ,
131
148
expected : "___C" ,
149
+ counts : map [string ]int {"VAR_C" : 1 },
132
150
},
133
151
{
134
152
name : "compound" ,
135
153
input : "$(VAR_A)_$(VAR_B)_$(VAR_C)" ,
136
154
expected : "A_B_C" ,
155
+ counts : map [string ]int {"VAR_A" : 1 , "VAR_B" : 1 , "VAR_C" : 1 },
137
156
},
138
157
{
139
158
name : "escape & expand" ,
140
159
input : "$$(VAR_B)_$(VAR_A)" ,
141
160
expected : "$(VAR_B)_A" ,
161
+ counts : map [string ]int {"VAR_A" : 1 },
142
162
},
143
163
{
144
164
name : "compound escape" ,
@@ -154,16 +174,19 @@ func doExpansionTest(t *testing.T, mapping func(string) string) {
154
174
name : "backslash escape ignored" ,
155
175
input : "foo\\ $(VAR_C)bar" ,
156
176
expected : "foo\\ Cbar" ,
177
+ counts : map [string ]int {"VAR_C" : 1 },
157
178
},
158
179
{
159
180
name : "backslash escape ignored" ,
160
181
input : "foo\\ \\ $(VAR_C)bar" ,
161
182
expected : "foo\\ \\ Cbar" ,
183
+ counts : map [string ]int {"VAR_C" : 1 },
162
184
},
163
185
{
164
186
name : "lots of backslashes" ,
165
187
input : "foo\\ \\ \\ \\ $(VAR_A)bar" ,
166
188
expected : "foo\\ \\ \\ \\ Abar" ,
189
+ counts : map [string ]int {"VAR_A" : 1 },
167
190
},
168
191
{
169
192
name : "nested var references" ,
@@ -179,16 +202,19 @@ func doExpansionTest(t *testing.T, mapping func(string) string) {
179
202
name : "value is a reference" ,
180
203
input : "$(VAR_REF)" ,
181
204
expected : "$(VAR_A)" ,
205
+ counts : map [string ]int {"VAR_REF" : 1 },
182
206
},
183
207
{
184
208
name : "value is a reference x 2" ,
185
209
input : "%%$(VAR_REF)--$(VAR_REF)%%" ,
186
210
expected : "%%$(VAR_A)--$(VAR_A)%%" ,
211
+ counts : map [string ]int {"VAR_REF" : 2 },
187
212
},
188
213
{
189
214
name : "empty var" ,
190
215
input : "foo$(VAR_EMPTY)bar" ,
191
216
expected : "foobar" ,
217
+ counts : map [string ]int {"VAR_EMPTY" : 1 },
192
218
},
193
219
{
194
220
name : "unterminated expression" ,
@@ -234,6 +260,7 @@ func doExpansionTest(t *testing.T, mapping func(string) string) {
234
260
name : "multiple (odd) operators, var defined" ,
235
261
input : "$$$$$$$(VAR_A)" ,
236
262
expected : "$$$A" ,
263
+ counts : map [string ]int {"VAR_A" : 1 },
237
264
},
238
265
{
239
266
name : "missing open expression" ,
@@ -249,16 +276,19 @@ func doExpansionTest(t *testing.T, mapping func(string) string) {
249
276
name : "trailing incomplete expression not consumed" ,
250
277
input : "$(VAR_B)_______$(A" ,
251
278
expected : "B_______$(A" ,
279
+ counts : map [string ]int {"VAR_B" : 1 },
252
280
},
253
281
{
254
282
name : "trailing incomplete expression, no content, is not consumed" ,
255
283
input : "$(VAR_C)_______$(" ,
256
284
expected : "C_______$(" ,
285
+ counts : map [string ]int {"VAR_C" : 1 },
257
286
},
258
287
{
259
288
name : "operator at end of input string is preserved" ,
260
289
input : "$(VAR_A)foobarzab$" ,
261
290
expected : "Afoobarzab$" ,
291
+ counts : map [string ]int {"VAR_A" : 1 },
262
292
},
263
293
{
264
294
name : "shell escaped incomplete expr" ,
@@ -293,9 +323,31 @@ func doExpansionTest(t *testing.T, mapping func(string) string) {
293
323
}
294
324
295
325
for _ , tc := range cases {
326
+ counts := make (map [string ]int )
327
+ mapping := MappingFuncFor (counts , context ... )
296
328
expanded := Expand (tc .input , mapping )
297
329
if e , a := tc .expected , expanded ; e != a {
298
330
t .Errorf ("%v: expected %q, got %q" , tc .name , e , a )
299
331
}
332
+ if len (counts ) != len (tc .counts ) {
333
+ t .Errorf ("%v: len(counts)=%d != len(tc.counts)=%d" ,
334
+ tc .name , len (counts ), len (tc .counts ))
335
+ }
336
+ if len (tc .counts ) > 0 {
337
+ for k , expectedCount := range tc .counts {
338
+ c , ok := counts [k ]
339
+ if ok {
340
+ if c != expectedCount {
341
+ t .Errorf (
342
+ "%v: k=%s, expected count %d, got %d" ,
343
+ tc .name , k , expectedCount , c )
344
+ }
345
+ } else {
346
+ t .Errorf (
347
+ "%v: k=%s, expected count %d, got zero" ,
348
+ tc .name , k , expectedCount )
349
+ }
350
+ }
351
+ }
300
352
}
301
353
}
0 commit comments