Skip to content

Commit 045d0bc

Browse files
authored
Merge pull request #14 from pusher/properly-deal-with-non-existent-ignored-paths
Properly deal with ignored paths in #getTemplateInput
2 parents a429e3a + 80d4110 commit 045d0bc

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

pkg/quack/quack.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ func getTemplateInput(data []byte, ignoredPaths []string) ([]byte, error) {
212212
patch := []byte(fmt.Sprintf(`[
213213
{"op": "remove", "path": "%s"}
214214
]`, path))
215-
data, err = applyPatch(data, patch)
215+
newData, err := applyPatch(data, patch)
216216
if err != nil && !nonExistentPath(err) {
217217
return nil, fmt.Errorf("error removing %s: %v", path, err)
218+
} else if newData != nil {
219+
data = newData
218220
}
219221
}
220222

pkg/quack/quack_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ func TestGetTemplateInputRemovesIgnoredPaths(t *testing.T) {
233233
object := testObject{
234234
ObjectMeta: metav1.ObjectMeta{
235235
Annotations: map[string]string{
236-
"annotation": "value",
237-
"other/annotation": "bar",
236+
"annotation": "value",
237+
"quack.pusher.com/template": "true",
238+
"other/annotation": "bar",
238239
},
239240
},
240241
Foo: "bar",
@@ -253,18 +254,38 @@ func TestGetTemplateInputRemovesIgnoredPaths(t *testing.T) {
253254
if err != nil {
254255
assert.FailNowf(t, "jsonError", "Failed to marshal input: %v", err)
255256
}
257+
objectNoOtherRaw, err := json.Marshal(objectNoOtherAnnotation)
258+
if err != nil {
259+
assert.FailNowf(t, "jsonError", "Failed to marshal input: %v", err)
260+
}
256261

257262
template, err := getTemplateInput(objectRaw, ignoredPaths)
258263
if err != nil {
259264
assert.FailNowf(t, "methodError", "Error in getTemplateInput: %v", err)
260265
}
261266

267+
assert.NotNil(t, template, "template should not be nil")
268+
262269
templateObject := testObject{}
263270
err = json.Unmarshal(template, &templateObject)
264271
if err != nil {
265272
assert.FailNowf(t, "jsonError", "Error in unmarshall: %v", err)
266273
}
267274
assert.Equal(t, objectNoOtherAnnotation, templateObject, "Object should have no ignored paths")
275+
276+
template, err = getTemplateInput(objectNoOtherRaw, ignoredPaths)
277+
if err != nil {
278+
assert.FailNowf(t, "methodError", "Error in getTemplateInput: %v", err)
279+
}
280+
281+
assert.NotNil(t, template, "template should not be nil")
282+
283+
err = json.Unmarshal(template, &templateObject)
284+
if err != nil {
285+
assert.FailNowf(t, "jsonError", "Error in unmarshall: %v", err)
286+
}
287+
288+
assert.Equal(t, objectNoOtherAnnotation, templateObject, "Object should have no ignored paths")
268289
}
269290

270291
func TestGetTemplateInputRemovesStatus(t *testing.T) {

0 commit comments

Comments
 (0)