This repository has been archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch_test.go
822 lines (796 loc) · 12.2 KB
/
patch_test.go
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
package yamlpatch_test
import (
"bytes"
yamlpatch "github.com/krishicks/yaml-patch"
yaml "gopkg.in/yaml.v2"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("Patch", func() {
Describe("Apply", func() {
DescribeTable(
"positive cases",
func(doc, ops, expectedYAML string) {
patch, err := yamlpatch.DecodePatch([]byte(ops))
Expect(err).NotTo(HaveOccurred())
actualBytes, err := patch.Apply([]byte(doc))
Expect(err).NotTo(HaveOccurred())
// Create decoder for both actual patched bytes, and expectedYAML string passed on input
actualDecoder := yaml.NewDecoder(bytes.NewReader(actualBytes))
expectedDecoder := yaml.NewDecoder(bytes.NewReader([]byte(expectedYAML)))
// Loop through each decoders a document at time
for {
var actualIface interface{}
var expectedIface interface{}
// Decode document from byte string
actualErr := actualDecoder.Decode(&actualIface)
expectedErr := expectedDecoder.Decode(&expectedIface)
// Check for no more documents
if (actualIface == nil) && (expectedIface == nil) {
break
}
// Both input and output should be error free
Expect(actualErr).NotTo(HaveOccurred())
Expect(expectedErr).NotTo(HaveOccurred())
// Expect actual and expected outputs to be identical
Expect(actualIface).To(Equal(expectedIface))
}
},
Entry("adding an element to an object",
`---
foo: bar
`,
`---
- op: add
path: /baz
value: qux
`,
`---
foo: bar
baz: qux
`,
),
Entry("adding an element to an array",
`---
foo: [bar,baz]
`,
`---
- op: add
path: /foo/1
value: qux
`,
`---
foo: [bar,qux,baz]
`,
),
Entry("removing an element from an object",
`---
foo: bar
baz: qux
`,
`---
- op: remove
path: /baz
`,
`---
foo: bar
`,
),
Entry("removing an element from an array",
`---
foo: [bar,qux,baz]
`,
`---
- op: remove
path: /foo/1
`,
`---
foo: [bar,baz]
`,
),
Entry("removing an element from an array (multiple documents)",
`---
foo: [bar,qux,baz]
---
foo: [mal,baz,goof]
`,
`---
- op: remove
path: /foo/1
`,
`---
foo: [bar,baz]
---
foo: [mal,goof]
`,
),
Entry("replacing an element in an object",
`---
foo: bar
baz: qux
`,
`---
- op: replace
path: /baz
value: boo
`,
`---
foo: bar
baz: boo
`,
),
Entry("moving an element in an object",
`---
foo:
bar: baz
waldo: fred
qux:
corge: grault
`,
`---
- op: move
from: /foo/waldo
path: /qux/thud
`,
`---
foo:
bar: baz
qux:
corge: grault
thud: fred
`,
),
Entry("moving an element in an object to the root",
`---
foo:
bar: baz
waldo: fred
qux:
corge: grault
`,
`---
- op: move
from: /foo
path: /
`,
`---
bar: baz
waldo: fred
qux:
corge: grault
`,
),
Entry("moving an element in an array",
`---
foo: [all, grass, cows, eat]
`,
`---
- op: move
from: /foo/1
path: /foo/3
`,
`---
foo: [all, cows, eat, grass]
`,
),
Entry("adding an object to an object",
`---
foo: bar
`,
`---
- op: add
path: /child
value:
grandchild: {}
`,
`---
foo: bar
child:
grandchild: {}
`,
),
Entry("adding an object to an object (multiple documents)",
`---
foo: bar
---
mal: goof
`,
`---
- op: add
path: /child
value:
grandchild: {}
`,
`---
foo: bar
child:
grandchild: {}
---
mal: goof
child:
grandchild: {}
`,
),
Entry("appending an element to an array",
`---
foo: [bar]
`,
`---
- op: add
path: /foo/-
value: [abc,def]
`,
`---
foo: [bar, [abc, def]]
`,
),
Entry("removing a nil element from an object",
`---
foo: bar
qux:
baz: 1
bar: ~
`,
`---
- op: remove
path: /qux/bar
`,
`---
foo: bar
qux:
baz: 1
`,
),
Entry("adding a nil element to an object",
`---
foo: bar
`,
`---
- op: add
path: /baz
value: ~
`,
`---
foo: bar
baz: ~
`,
),
Entry("replacing the sole element in an array",
`---
foo: [bar]
`,
`---
- op: replace
path: /foo/0
value: baz
`,
`---
foo: [baz]
`,
),
Entry("replacing an element in an array within a root array",
`---
- foo: [bar, qux, baz]
`,
`---
- op: replace
path: /0/foo/0
value: bum
`,
`---
- foo: [bum, qux, baz]
`,
),
Entry("replacing an element in the root object",
`---
foo: bar
`,
`---
- op: replace
path: /foo
value: qux
`,
`---
foo: qux
`,
),
Entry("copying an element in an array within a root array with an index",
`---
- foo: [bar, qux, baz]
bar: [qux, baz]
`,
`---
- op: copy
from: /0/foo/0
path: /0/bar/0
`,
`---
- foo: [bar, qux, baz]
bar: [bar, baz]
`,
),
Entry("testing for the existence of a nil value in an object",
`---
baz: ~
`,
`---
- op: test
path: /baz
value: ~
`,
`---
baz: ~
`,
),
Entry("testing for the existence of a nil key in an object",
`---
baz: ~
`,
`---
- op: test
path: /foo
value: ~
`,
`---
baz: ~
`,
),
Entry("testing for the existence of a nil value in an object",
`---
baz: qux
`,
`---
- op: test
path: /baz
value: qux
`,
`---
baz: qux
`,
),
Entry("testing for the existence of an element in an array",
`---
foo: [a, 2, c]
`,
`---
- op: test
path: /foo/1
value: 2
`,
`---
foo: [a, 2, c]
`,
),
Entry("testing for the existence of an element in an object using escape ordering",
`---
baz/foo: qux
`,
`---
- op: test
path: /baz~1foo
value: qux
`,
`---
baz/foo: qux
`,
),
Entry("testing for the existence of an object",
`---
foo:
- bar: baz
qux: corge
`,
`---
- op: test
path: /foo/0
value:
bar: baz
qux: corge
`,
`---
foo:
- bar: baz
qux: corge
`,
),
XEntry("copying an element in an array within a root array to a destination without an index",
// this is in jsonpatch, but I'd like confirmation from the spec that this is intended
`---
- foo: [bar, qux, baz]
bar: [qux, baz]
`,
`---
- op: copy
from: /0/foo/0
path: /0/bar
`,
`---
- foo: [bar, qux, baz]
bar: [bar, qux, baz]
`,
),
XEntry("copying an element in an array within a root array to a destination without an index",
`---
- foo:
bar: [qux, baz]
baz:
qux: bum
`,
`---
- op: copy
from: /0/foo/bar
path: /0/baz/bar
`,
`---
- foo: [bar, qux, baz]
bar: [bar, qux, baz]
`,
),
Entry("adding an element to the root of a document",
`---
foo: bar
`,
`---
- op: add
path: /
value:
baz: qux
`,
`---
foo: bar
baz: qux
`,
),
)
DescribeTable(
"with extended syntax",
func(doc, ops, expectedYAML string) {
patch, err := yamlpatch.DecodePatch([]byte(ops))
Expect(err).NotTo(HaveOccurred())
actualBytes, err := patch.Apply([]byte(doc))
Expect(err).NotTo(HaveOccurred())
// Create decoder for both actual patched bytes, and expectedYAML string passed on input
actualDecoder := yaml.NewDecoder(bytes.NewReader(actualBytes))
expectedDecoder := yaml.NewDecoder(bytes.NewReader([]byte(expectedYAML)))
// Loop through each decoders a document at time
for {
var actualIface interface{}
var expectedIface interface{}
// Decode document from byte string
actualErr := actualDecoder.Decode(&actualIface)
expectedErr := expectedDecoder.Decode(&expectedIface)
// Check for no more documents
if (actualIface == nil) && (expectedIface == nil) {
break
}
// Both input and output should be error free
Expect(actualErr).NotTo(HaveOccurred())
Expect(expectedErr).NotTo(HaveOccurred())
// Expect actual and expected outputs to be identical
Expect(actualIface).To(Equal(expectedIface))
}
},
Entry("a path that begins with a composite key",
`---
- foo: bar
`,
`---
- op: replace
path: /foo=bar
value:
baz: quux
`,
`---
- baz: quux
`,
),
Entry("a path that begins with an array index and ends with a composite key",
`---
- waldo:
- thud: boo
- foo: bar
- corge: grault
`,
`---
- op: replace
path: /0/foo=bar
value:
baz: quux
`,
`---
- waldo:
- thud: boo
- baz: quux
- corge: grault
`,
),
Entry("a path that begins with an array index and ends with a composite key (multiple documents)",
`---
- waldo:
- thud: boo
- foo: bar
- corge: grault
---
- water:
- mal: goof
- foo: bar
- bear: bacon
`,
`---
- op: replace
path: /0/foo=bar
value:
baz: quux
`,
`---
- waldo:
- thud: boo
- baz: quux
- corge: grault
---
- water:
- mal: goof
- baz: quux
- bear: bacon
`,
),
Entry("a path that begins with an object key and ends with a composite key",
`---
waldo:
- thud: boo
- foo: bar
corge: grault
`,
`---
- op: replace
path: /waldo/foo=bar
value:
baz: quux
`,
`---
waldo:
- thud: boo
- baz: quux
corge: grault
`,
),
Entry("a path that doesn't end with a composite key",
`---
jobs:
- name: upgrade-opsmgr
serial: true
plan:
- get: pivnet-opsmgr
- put: something-else
`,
`---
- op: replace
path: /jobs/name=upgrade-opsmgr/plan/1
value:
get: something-else
`,
`---
jobs:
- name: upgrade-opsmgr
serial: true
plan:
- get: pivnet-opsmgr
- get: something-else
`,
),
Entry("removes multiple entries in a single op",
`---
foo:
- bar: baz
- waldo: fred
qux:
corge: grault
thud:
- waldo: fred
- bar: baz
`,
`---
- op: remove
path: /waldo=fred
`,
`---
foo:
- bar: baz
qux:
corge: grault
thud:
- bar: baz
`,
),
Entry("removes multiple entries from an array in a single op",
`---
foo:
- bar: baz
- bar: baz
- waldo: fred
---
foo:
- waldo: fred
- bar: baz
- bar: baz
---
foo:
- bar: baz
- waldo: fred
- bar: baz
`,
`---
- op: remove
path: /foo/bar=baz
`,
`---
foo:
- waldo: fred
---
foo:
- waldo: fred
---
foo:
- waldo: fred
`,
),
)
DescribeTable(
"failure cases",
func(doc, ops string) {
patch, err := yamlpatch.DecodePatch([]byte(ops))
Expect(err).NotTo(HaveOccurred())
_, err = patch.Apply([]byte(doc))
Expect(err).To(HaveOccurred())
},
Entry("adding an element to an object with a bad pointer",
`---
foo: bar
`,
`---
- op: add
path: /baz/bat
value: qux
`,
),
Entry("removing an element from an object with a bad pointer",
`---
a:
b:
d: 1
`,
`---
- op: remove
path: /a/b/c
`,
),
Entry("moving an element in an object with a bad pointer",
`---
a:
b:
d: 1
`,
`---
- op: move
from: /a/b/c
path: /a/b/e
`,
),
Entry("removing an element from an array with a bad pointer",
`---
a:
b: [1]
`,
`---
- op: remove
path: /a/b/1
`,
),
Entry("moving an element from an array with a bad pointer",
`---
a:
b: [1]
`,
`---
- op: move
from: /a/b/1
path: /a/b/2
`,
),
Entry("moving an element from an array with a bad pointer (multiple documents, second with error case)",
`---
a:
b: ["ateste1", "1"]
---
a:
b: [1]
`,
`---
- op: move
from: /a/b/1
path: /a/b/2
`,
),
Entry("an operation with an invalid pathz field",
`---
foo: bar
`,
`---
- op: add
pathz: /baz
value: qux
`,
),
Entry("an add operation with an empty path",
`---
foo: bar
`,
`---
- op: add
path: ''
value: qux
`,
),
Entry("a replace operation on an array with an invalid path",
`---
name:
foo:
bat
qux:
bum
`,
`---
- op: replace
path: /foo/2
value: bum
`,
),
Entry("a replace operation on an array with an invalid path (multiple documents, first with error case)",
`---
name:
foo:
mal: goof
qux:
bum
---
name:
foo:
bat: bam
qux:
bum
`,
`---
- op: replace
path: /name/foo/bat
value: bum
`,
),
)
})
Describe("DecodePatch", func() {
It("returns an empty patch when given nil", func() {
patch, err := yamlpatch.DecodePatch(nil)
Expect(err).NotTo(HaveOccurred())
Expect(patch).To(HaveLen(0))
})
It("returns a patch with a single op when given a single op", func() {
ops := []byte(
`---
- op: add
path: /baz
value: qux`)
patch, err := yamlpatch.DecodePatch(ops)
Expect(err).NotTo(HaveOccurred())
var v interface{} = "qux"
value := yamlpatch.NewNode(&v)
Expect(patch).To(Equal(yamlpatch.Patch{
{
Op: "add",
Path: "/baz",
Value: value,
},
}))
})
})
})