5
5
"os"
6
6
"testing"
7
7
8
+ "github.com/aryann/difflib"
8
9
"github.com/mgutz/ansi"
9
10
"github.com/stretchr/testify/require"
10
11
@@ -260,7 +261,7 @@ spec:
260
261
261
262
t .Run ("OnChange" , func (t * testing.T ) {
262
263
var buf1 bytes.Buffer
263
- diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.0 }
264
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.0 , [] string {} }
264
265
265
266
if changesSeen := Manifests (specBeta , specRelease , & diffOptions , & buf1 ); ! changesSeen {
266
267
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -277,9 +278,21 @@ spec:
277
278
` , buf1 .String ())
278
279
})
279
280
281
+ t .Run ("OnChangeWithSuppress" , func (t * testing.T ) {
282
+ var buf1 bytes.Buffer
283
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.0 , []string {"apiVersion" }}
284
+
285
+ if changesSeen := Manifests (specBeta , specRelease , & diffOptions , & buf1 ); ! changesSeen {
286
+ t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
287
+ }
288
+
289
+ require .Equal (t , `default, nginx, Deployment (apps) has changed:
290
+ ` , buf1 .String ())
291
+ })
292
+
280
293
t .Run ("OnChangeRename" , func (t * testing.T ) {
281
294
var buf1 bytes.Buffer
282
- diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 }
295
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 , [] string {} }
283
296
284
297
if changesSeen := Manifests (specReleaseSpec , specReleaseRenamed , & diffOptions , & buf1 ); ! changesSeen {
285
298
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -300,7 +313,7 @@ spec:
300
313
301
314
t .Run ("OnChangeRenameAndUpdate" , func (t * testing.T ) {
302
315
var buf1 bytes.Buffer
303
- diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 }
316
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 , [] string {} }
304
317
305
318
if changesSeen := Manifests (specReleaseSpec , specReleaseRenamedAndUpdated , & diffOptions , & buf1 ); ! changesSeen {
306
319
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -322,7 +335,7 @@ spec:
322
335
323
336
t .Run ("OnChangeRenameAndAdded" , func (t * testing.T ) {
324
337
var buf1 bytes.Buffer
325
- diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 }
338
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 , [] string {} }
326
339
327
340
if changesSeen := Manifests (specReleaseSpec , specReleaseRenamedAndAdded , & diffOptions , & buf1 ); ! changesSeen {
328
341
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -341,12 +354,35 @@ spec:
341
354
+ matchLabels:
342
355
+ app: nginx-renamed
343
356
357
+ ` , buf1 .String ())
358
+ })
359
+
360
+ t .Run ("OnChangeRenameAndAddedWithPartialSuppress" , func (t * testing.T ) {
361
+ var buf1 bytes.Buffer
362
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 , []string {"app: " }}
363
+
364
+ if changesSeen := Manifests (specReleaseSpec , specReleaseRenamedAndAdded , & diffOptions , & buf1 ); ! changesSeen {
365
+ t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
366
+ }
367
+
368
+ require .Equal (t , `default, nginx, Deployment (apps) has changed:
369
+
370
+ apiVersion: apps/v1
371
+ kind: Deployment
372
+ metadata:
373
+ - name: nginx
374
+ + name: nginx-renamed
375
+ spec:
376
+ replicas: 3
377
+ + selector:
378
+ + matchLabels:
379
+
344
380
` , buf1 .String ())
345
381
})
346
382
347
383
t .Run ("OnChangeRenameAndRemoved" , func (t * testing.T ) {
348
384
var buf1 bytes.Buffer
349
- diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 }
385
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 , [] string {} }
350
386
351
387
if changesSeen := Manifests (specReleaseRenamedAndAdded , specReleaseSpec , & diffOptions , & buf1 ); ! changesSeen {
352
388
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -365,12 +401,35 @@ spec:
365
401
- matchLabels:
366
402
- app: nginx-renamed
367
403
404
+ ` , buf1 .String ())
405
+ })
406
+
407
+ t .Run ("OnChangeRenameAndRemovedWithPartialSuppress" , func (t * testing.T ) {
408
+ var buf1 bytes.Buffer
409
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.5 , []string {"app: " }}
410
+
411
+ if changesSeen := Manifests (specReleaseRenamedAndAdded , specReleaseSpec , & diffOptions , & buf1 ); ! changesSeen {
412
+ t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
413
+ }
414
+
415
+ require .Equal (t , `default, nginx-renamed, Deployment (apps) has changed:
416
+
417
+ apiVersion: apps/v1
418
+ kind: Deployment
419
+ metadata:
420
+ - name: nginx-renamed
421
+ + name: nginx
422
+ spec:
423
+ replicas: 3
424
+ - selector:
425
+ - matchLabels:
426
+
368
427
` , buf1 .String ())
369
428
})
370
429
371
430
t .Run ("OnNoChange" , func (t * testing.T ) {
372
431
var buf2 bytes.Buffer
373
- diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.0 }
432
+ diffOptions := Options {"diff" , 10 , false , true , []string {}, 0.0 , [] string {} }
374
433
375
434
if changesSeen := Manifests (specRelease , specRelease , & diffOptions , & buf2 ); changesSeen {
376
435
t .Error ("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`" )
@@ -381,7 +440,7 @@ spec:
381
440
382
441
t .Run ("OnChangeSimple" , func (t * testing.T ) {
383
442
var buf1 bytes.Buffer
384
- diffOptions := Options {"simple" , 10 , false , true , []string {}, 0.0 }
443
+ diffOptions := Options {"simple" , 10 , false , true , []string {}, 0.0 , [] string {} }
385
444
386
445
if changesSeen := Manifests (specBeta , specRelease , & diffOptions , & buf1 ); ! changesSeen {
387
446
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -394,7 +453,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
394
453
395
454
t .Run ("OnNoChangeSimple" , func (t * testing.T ) {
396
455
var buf2 bytes.Buffer
397
- diffOptions := Options {"simple" , 10 , false , true , []string {}, 0.0 }
456
+ diffOptions := Options {"simple" , 10 , false , true , []string {}, 0.0 , [] string {} }
398
457
if changesSeen := Manifests (specRelease , specRelease , & diffOptions , & buf2 ); changesSeen {
399
458
t .Error ("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`" )
400
459
}
@@ -404,7 +463,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
404
463
405
464
t .Run ("OnChangeTemplate" , func (t * testing.T ) {
406
465
var buf1 bytes.Buffer
407
- diffOptions := Options {"template" , 10 , false , true , []string {}, 0.0 }
466
+ diffOptions := Options {"template" , 10 , false , true , []string {}, 0.0 , [] string {} }
408
467
409
468
if changesSeen := Manifests (specBeta , specRelease , & diffOptions , & buf1 ); ! changesSeen {
410
469
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -422,7 +481,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
422
481
423
482
t .Run ("OnChangeJSON" , func (t * testing.T ) {
424
483
var buf1 bytes.Buffer
425
- diffOptions := Options {"json" , 10 , false , true , []string {}, 0.0 }
484
+ diffOptions := Options {"json" , 10 , false , true , []string {}, 0.0 , [] string {} }
426
485
427
486
if changesSeen := Manifests (specBeta , specRelease , & diffOptions , & buf1 ); ! changesSeen {
428
487
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -440,7 +499,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
440
499
441
500
t .Run ("OnNoChangeTemplate" , func (t * testing.T ) {
442
501
var buf2 bytes.Buffer
443
- diffOptions := Options {"template" , 10 , false , true , []string {}, 0.0 }
502
+ diffOptions := Options {"template" , 10 , false , true , []string {}, 0.0 , [] string {} }
444
503
445
504
if changesSeen := Manifests (specRelease , specRelease , & diffOptions , & buf2 ); changesSeen {
446
505
t .Error ("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`" )
@@ -452,7 +511,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
452
511
t .Run ("OnChangeCustomTemplate" , func (t * testing.T ) {
453
512
var buf1 bytes.Buffer
454
513
os .Setenv ("HELM_DIFF_TPL" , "testdata/customTemplate.tpl" )
455
- diffOptions := Options {"template" , 10 , false , true , []string {}, 0.0 }
514
+ diffOptions := Options {"template" , 10 , false , true , []string {}, 0.0 , [] string {} }
456
515
457
516
if changesSeen := Manifests (specBeta , specRelease , & diffOptions , & buf1 ); ! changesSeen {
458
517
t .Error ("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`" )
@@ -535,7 +594,7 @@ stringData:
535
594
536
595
t .Run ("OnChangeSecretWithByteData" , func (t * testing.T ) {
537
596
var buf1 bytes.Buffer
538
- diffOptions := Options {"diff" , 10 , false , false , []string {}, 0.5 } //NOTE: ShowSecrets = false
597
+ diffOptions := Options {"diff" , 10 , false , false , []string {}, 0.5 , [] string {} } //NOTE: ShowSecrets = false
539
598
540
599
if changesSeen := Manifests (specSecretWithByteData , specSecretWithByteDataChanged , & diffOptions , & buf1 ); ! changesSeen {
541
600
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -560,7 +619,7 @@ stringData:
560
619
561
620
t .Run ("OnChangeSecretWithStringData" , func (t * testing.T ) {
562
621
var buf1 bytes.Buffer
563
- diffOptions := Options {"diff" , 10 , false , false , []string {}, 0.5 } //NOTE: ShowSecrets = false
622
+ diffOptions := Options {"diff" , 10 , false , false , []string {}, 0.5 , [] string {} } //NOTE: ShowSecrets = false
564
623
565
624
if changesSeen := Manifests (specSecretWithStringData , specSecretWithStringDataChanged , & diffOptions , & buf1 ); ! changesSeen {
566
625
t .Error ("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`" )
@@ -582,3 +641,85 @@ stringData:
582
641
` , buf1 .String ())
583
642
})
584
643
}
644
+
645
+ func TestDoSuppress (t * testing.T ) {
646
+ for _ , tt := range []struct {
647
+ name string
648
+ input Report
649
+ supressRegex []string
650
+ expected Report
651
+ }{
652
+ {
653
+ name : "noop" ,
654
+ input : Report {},
655
+ supressRegex : []string {},
656
+ expected : Report {},
657
+ },
658
+ {
659
+ name : "simple" ,
660
+ input : Report {
661
+ entries : []ReportEntry {
662
+ {
663
+ diffs : diffStrings ("hello: world" , "hello: world2" , false ),
664
+ },
665
+ },
666
+ },
667
+ supressRegex : []string {},
668
+ expected : Report {
669
+ entries : []ReportEntry {
670
+ {
671
+ diffs : diffStrings ("hello: world" , "hello: world2" , false ),
672
+ },
673
+ },
674
+ },
675
+ },
676
+ {
677
+ name : "ignore all" ,
678
+ input : Report {
679
+ entries : []ReportEntry {
680
+ {
681
+ diffs : diffStrings ("hello: world" , "hello: world2" , false ),
682
+ },
683
+ },
684
+ },
685
+ supressRegex : []string {".*world2?" },
686
+ expected : Report {
687
+ entries : []ReportEntry {
688
+ {
689
+ diffs : []difflib.DiffRecord {},
690
+ },
691
+ },
692
+ },
693
+ },
694
+ {
695
+ name : "ignore partial" ,
696
+ input : Report {
697
+ entries : []ReportEntry {
698
+ {
699
+ diffs : diffStrings ("hello: world" , "hello: world2" , false ),
700
+ },
701
+ },
702
+ },
703
+ supressRegex : []string {".*world2" },
704
+ expected : Report {
705
+ entries : []ReportEntry {
706
+ {
707
+ diffs : []difflib.DiffRecord {
708
+ {
709
+ Payload : "hello: world" ,
710
+ Delta : difflib .LeftOnly ,
711
+ },
712
+ },
713
+ },
714
+ },
715
+ },
716
+ },
717
+ } {
718
+ t .Run (tt .name , func (t * testing.T ) {
719
+ report , err := doSuppress (tt .input , tt .supressRegex )
720
+ require .NoError (t , err )
721
+
722
+ require .Equal (t , tt .expected , report )
723
+ })
724
+ }
725
+ }
0 commit comments