@@ -1090,57 +1090,57 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
1090
1090
func TestRemoveUnstructuredKeys (t * testing.T ) {
1091
1091
t .Run ("remove single key" , run_TestRemoveUnstructuredKeys (tc_RemoveUnstructuredKeys {
1092
1092
givenPath : []string {"metadata" , "annotations" },
1093
- givenExclude : []string {"^key1 $" },
1093
+ givenExclude : []string {"^toexclude $" },
1094
1094
givenObj : map [string ]interface {}{
1095
1095
"metadata" : map [string ]interface {}{
1096
- "name" : "foo" ,
1097
1096
"annotations" : map [string ]interface {}{
1098
- "key1 " : "value1 " ,
1099
- "key2 " : "value2 " ,
1097
+ "toexclude " : "foo " ,
1098
+ "tokeep " : "bar " ,
1100
1099
},
1101
1100
},
1102
1101
},
1103
1102
expectObj : map [string ]interface {}{
1104
1103
"metadata" : map [string ]interface {}{
1105
- "name" : "foo" ,
1104
+ "annotations" : map [string ]interface {}{
1105
+ "tokeep" : "bar" ,
1106
+ },
1106
1107
},
1107
1108
},
1108
1109
}))
1109
1110
1110
1111
t .Run ("remove keys using multiple regexes" , run_TestRemoveUnstructuredKeys (tc_RemoveUnstructuredKeys {
1111
1112
givenPath : []string {"metadata" , "annotations" },
1112
- givenExclude : []string {"^key1 $" , "^key2 $" },
1113
+ givenExclude : []string {"^toexclude1 $" , "^toexclude2 $" },
1113
1114
givenObj : map [string ]interface {}{
1114
1115
"metadata" : map [string ]interface {}{
1115
- "name" : "foo" ,
1116
1116
"annotations" : map [string ]interface {}{
1117
- "key1 " : "value1 " ,
1118
- "key2 " : "value2 " ,
1117
+ "toexclude1 " : "foo " ,
1118
+ "toexclude2 " : "bar " ,
1119
1119
},
1120
1120
},
1121
1121
},
1122
1122
expectObj : map [string ]interface {}{
1123
- "metadata" : map [string ]interface {}{
1124
- "name" : "foo" ,
1125
- },
1123
+ "metadata" : map [string ]interface {}{"annotations" : map [string ]interface {}{}},
1126
1124
},
1127
1125
}))
1128
1126
1129
1127
t .Run ("remove multiple keys with a single regex" , run_TestRemoveUnstructuredKeys (tc_RemoveUnstructuredKeys {
1130
1128
givenPath : []string {"metadata" , "annotations" },
1131
- givenExclude : []string {"key .*" },
1129
+ givenExclude : []string {"toexclude .*" },
1132
1130
givenObj : map [string ]interface {}{
1133
1131
"metadata" : map [string ]interface {}{
1134
- "name" : "foo" ,
1135
1132
"annotations" : map [string ]interface {}{
1136
- "key1" : "value1" ,
1137
- "key2" : "value2" ,
1133
+ "toexclude1" : "foo" ,
1134
+ "toexclude2" : "bar" ,
1135
+ "tokeep" : "baz" ,
1138
1136
},
1139
1137
},
1140
1138
},
1141
1139
expectObj : map [string ]interface {}{
1142
1140
"metadata" : map [string ]interface {}{
1143
- "name" : "foo" ,
1141
+ "annotations" : map [string ]interface {}{
1142
+ "tokeep" : "baz" ,
1143
+ },
1144
1144
},
1145
1145
},
1146
1146
}))
@@ -1150,19 +1150,15 @@ func TestRemoveUnstructuredKeys(t *testing.T) {
1150
1150
givenExclude : []string {},
1151
1151
givenObj : map [string ]interface {}{
1152
1152
"metadata" : map [string ]interface {}{
1153
- "name" : "foo" ,
1154
1153
"annotations" : map [string ]interface {}{
1155
- "key1" : "value1" ,
1156
- "key2" : "value2" ,
1154
+ "tokeep1" : "foo" ,
1157
1155
},
1158
1156
},
1159
1157
},
1160
1158
expectObj : map [string ]interface {}{
1161
1159
"metadata" : map [string ]interface {}{
1162
- "name" : "foo" ,
1163
1160
"annotations" : map [string ]interface {}{
1164
- "key1" : "value1" ,
1165
- "key2" : "value2" ,
1161
+ "tokeep1" : "foo" ,
1166
1162
},
1167
1163
},
1168
1164
},
@@ -1181,13 +1177,8 @@ func TestRemoveUnstructuredKeys(t *testing.T) {
1181
1177
t .Run ("works when the leaf field is nil" , run_TestRemoveUnstructuredKeys (tc_RemoveUnstructuredKeys {
1182
1178
givenPath : []string {"metadata" , "annotations" },
1183
1179
givenExclude : []string {},
1184
- givenObj : map [string ]interface {}{
1185
- "metadata" : map [string ]interface {}{
1186
- "name" : "foo" ,
1187
- "annotations" : nil ,
1188
- },
1189
- },
1190
- expectObj : map [string ]interface {}{"metadata" : map [string ]interface {}{"name" : "foo" }},
1180
+ givenObj : map [string ]interface {}{"metadata" : map [string ]interface {}{"annotations" : nil }},
1181
+ expectObj : map [string ]interface {}{"metadata" : map [string ]interface {}{"annotations" : nil }},
1191
1182
}))
1192
1183
1193
1184
t .Run ("works when leaf field is unexpectedly not nil and not a known map" , run_TestRemoveUnstructuredKeys (tc_RemoveUnstructuredKeys {
@@ -1208,6 +1199,7 @@ func TestRemoveUnstructuredKeys(t *testing.T) {
1208
1199
t .Run ("works when the intermediate field is nil" , run_TestRemoveUnstructuredKeys (tc_RemoveUnstructuredKeys {
1209
1200
givenPath : []string {"metadata" , "annotations" },
1210
1201
givenObj : map [string ]interface {}{"metadata" : nil },
1202
+ expectObj : map [string ]interface {}{"metadata" : nil },
1211
1203
}))
1212
1204
1213
1205
t .Run ("works when the intermediate field is unexpectedly not nil and not a map" , run_TestRemoveUnstructuredKeys (tc_RemoveUnstructuredKeys {
@@ -1228,36 +1220,37 @@ func run_TestRemoveUnstructuredKeys(tc tc_RemoveUnstructuredKeys) func(*testing.
1228
1220
return func (t * testing.T ) {
1229
1221
t .Helper ()
1230
1222
RemoveUnstructuredKeys (toRegexps (tc .givenExclude ), & unstructured.Unstructured {Object : tc .givenObj }, tc .givenPath ... )
1223
+ assert .Equal (t , tc .expectObj , tc .givenObj )
1231
1224
}
1232
1225
}
1233
1226
1234
1227
func TestRemoveTypedKeys (t * testing.T ) {
1235
1228
t .Run ("remove single key" , run_TestRemoveTypedKeys (tc_TestRemoveTypedKeys {
1236
- givenExclude : []string {"^key1 $" },
1237
- given : map [string ]string {"key1 " : "value1 " , "key2 " : "value2 " },
1238
- expected : map [string ]string {"key2 " : "value2 " },
1229
+ givenExclude : []string {"^toexclude $" },
1230
+ given : map [string ]string {"toexclude " : "foo " , "tokeep " : "bar " },
1231
+ expected : map [string ]string {"tokeep " : "bar " },
1239
1232
}))
1240
1233
1241
1234
t .Run ("remove keys using multiple regexes" , run_TestRemoveTypedKeys (tc_TestRemoveTypedKeys {
1242
- givenExclude : []string {"^key1 $" , "^key2 $" },
1243
- given : map [string ]string {"key1 " : "value1 " , "key2 " : "value2 " },
1244
- expected : map [string ]string {},
1235
+ givenExclude : []string {"^toexclude1 $" , "^toexclude2 $" },
1236
+ given : map [string ]string {"toexclude1 " : "foo " , "toexclude2 " : "bar" , "tokeep" : "baz " },
1237
+ expected : map [string ]string {"tokeep" : "baz" },
1245
1238
}))
1246
1239
1247
1240
t .Run ("remove multiple keys with a single regex" , run_TestRemoveTypedKeys (tc_TestRemoveTypedKeys {
1248
- givenExclude : []string {"key .*" },
1249
- given : map [string ]string {"key1 " : "value1 " , "key2 " : "value2 " },
1250
- expected : map [string ]string {},
1241
+ givenExclude : []string {"^toexclude .*" },
1242
+ given : map [string ]string {"toexclude1 " : "foo " , "toexclude2 " : "bar" , "tokeep" : "baz " },
1243
+ expected : map [string ]string {"tokeep" : "baz" },
1251
1244
}))
1252
1245
1253
1246
t .Run ("with no regex, the object is untouched" , run_TestRemoveTypedKeys (tc_TestRemoveTypedKeys {
1254
1247
givenExclude : []string {},
1255
- given : map [string ]string {"key1 " : "value1 " , "key2 " : "value2 " },
1256
- expected : map [string ]string {"key1 " : "value1 " , "key2 " : "value2 " },
1248
+ given : map [string ]string {"tokeep1 " : "foo " , "tokeep2 " : "bar " },
1249
+ expected : map [string ]string {"tokeep1 " : "foo " , "tokeep2 " : "bar " },
1257
1250
}))
1258
1251
1259
1252
t .Run ("works when the map is nil" , run_TestRemoveTypedKeys (tc_TestRemoveTypedKeys {
1260
- givenExclude : []string {"^key1 $" },
1253
+ givenExclude : []string {"^toexclude $" },
1261
1254
given : nil ,
1262
1255
expected : nil ,
1263
1256
}))
@@ -1271,6 +1264,7 @@ type tc_TestRemoveTypedKeys struct {
1271
1264
1272
1265
func run_TestRemoveTypedKeys (tc tc_TestRemoveTypedKeys ) func (t * testing.T ) {
1273
1266
return func (t * testing.T ) {
1267
+ t .Helper ()
1274
1268
RemoveTypedKeys (toRegexps (tc .givenExclude ), tc .given )
1275
1269
assert .Equal (t , tc .expected , tc .given )
1276
1270
}
0 commit comments