@@ -3,6 +3,7 @@ package cfg
33import (
44 "encoding/json"
55 "errors"
6+ "reflect"
67 "testing"
78 "time"
89
@@ -11,16 +12,24 @@ import (
1112 "github.com/stretchr/testify/require"
1213)
1314
14- func NewTestConfig (name string ) * Config {
15- return NewConfigFromFile ("../testdata/config/" + name )
15+ func NewTestConfig (names []string ) * Config {
16+ var configFiles []string
17+ for _ , name := range names {
18+ configFiles = append (configFiles , "../testdata/config/" + name )
19+ }
20+ return NewConfigFromFile (configFiles )
1621}
1722
1823func TestSimple (t * testing.T ) {
19- c := NewTestConfig ("e2e.yaml" )
24+ c := NewTestConfig ([] string { "e2e.yaml" , "e2e.override.yaml" } )
2025
2126 assert .NotNil (t , c , "config loading should't return nil" )
22-
2327 assert .Equal (t , 1 , len (c .Pipelines ), "pipelines count isn't match" )
28+
29+ // check override config
30+ outputType , err := c .Pipelines ["test" ].Raw .Get ("output" ).Get ("type" ).String ()
31+ assert .Nil (t , err , "cannot get output type" )
32+ assert .Equal (t , "devnull" , outputType , "output type is not overrided" )
2433}
2534
2635type intDefault struct {
@@ -643,3 +652,135 @@ func TestExpression_UnmarshalJSON(t *testing.T) {
643652 require .Equal (t , Expression ("2" ), val .E2 )
644653 require .Equal (t , Expression ("2+2" ), val .E3 )
645654}
655+
656+ func TestMergeYAMLs (t * testing.T ) {
657+ tests := []struct {
658+ name string
659+ a map [interface {}]interface {}
660+ b map [interface {}]interface {}
661+ expected map [interface {}]interface {}
662+ }{
663+ {
664+ name : "simple merge" ,
665+ a : map [interface {}]interface {}{
666+ "key1" : "value1" ,
667+ "key2" : "value2" ,
668+ },
669+ b : map [interface {}]interface {}{
670+ "key2" : "newValue2" ,
671+ "key3" : "value3" ,
672+ },
673+ expected : map [interface {}]interface {}{
674+ "key1" : "value1" ,
675+ "key2" : "newValue2" ,
676+ "key3" : "value3" ,
677+ },
678+ },
679+ {
680+ name : "nested maps" ,
681+ a : map [interface {}]interface {}{
682+ "key1" : map [interface {}]interface {}{
683+ "subkey1" : "subvalue1" ,
684+ },
685+ },
686+ b : map [interface {}]interface {}{
687+ "key1" : map [interface {}]interface {}{
688+ "subkey2" : "subvalue2" ,
689+ },
690+ "key2" : "value2" ,
691+ },
692+ expected : map [interface {}]interface {}{
693+ "key1" : map [interface {}]interface {}{
694+ "subkey1" : "subvalue1" ,
695+ "subkey2" : "subvalue2" ,
696+ },
697+ "key2" : "value2" ,
698+ },
699+ },
700+ {
701+ name : "overwriting nested maps" ,
702+ a : map [interface {}]interface {}{
703+ "key1" : map [interface {}]interface {}{
704+ "subkey1" : "subvalue1" ,
705+ },
706+ },
707+ b : map [interface {}]interface {}{
708+ "key1" : map [interface {}]interface {}{
709+ "subkey1" : "newSubvalue1" ,
710+ "subkey2" : "subvalue2" ,
711+ },
712+ },
713+ expected : map [interface {}]interface {}{
714+ "key1" : map [interface {}]interface {}{
715+ "subkey1" : "newSubvalue1" ,
716+ "subkey2" : "subvalue2" ,
717+ },
718+ },
719+ },
720+ {
721+ name : "empty maps" ,
722+ a : map [interface {}]interface {}{},
723+ b : map [interface {}]interface {}{},
724+ expected : map [interface {}]interface {}{
725+ // Expecting an empty map
726+ },
727+ },
728+ {
729+ name : "a is empty" ,
730+ a : map [interface {}]interface {}{},
731+ b : map [interface {}]interface {}{
732+ "key1" : "value1" ,
733+ },
734+ expected : map [interface {}]interface {}{
735+ "key1" : "value1" ,
736+ },
737+ },
738+ {
739+ name : "b is empty" ,
740+ a : map [interface {}]interface {}{
741+ "key1" : "value1" ,
742+ },
743+ b : map [interface {}]interface {}{},
744+ expected : map [interface {}]interface {}{
745+ "key1" : "value1" ,
746+ },
747+ },
748+ {
749+ name : "override slice" ,
750+ a : map [interface {}]interface {}{
751+ "key1" : []interface {}{"value1" , "value2" },
752+ },
753+ b : map [interface {}]interface {}{
754+ "key1" : []interface {}{"newValue1" , "newValue2" },
755+ },
756+ expected : map [interface {}]interface {}{
757+ "key1" : []interface {}{"newValue1" , "newValue2" },
758+ },
759+ },
760+ {
761+ name : "merge slice with map" ,
762+ a : map [interface {}]interface {}{
763+ "key1" : []interface {}{"value1" , "value2" },
764+ },
765+ b : map [interface {}]interface {}{
766+ "key1" : map [interface {}]interface {}{
767+ "subkey1" : "subvalue1" ,
768+ },
769+ },
770+ expected : map [interface {}]interface {}{
771+ "key1" : map [interface {}]interface {}{
772+ "subkey1" : "subvalue1" ,
773+ },
774+ },
775+ },
776+ }
777+
778+ for _ , tt := range tests {
779+ t .Run (tt .name , func (t * testing.T ) {
780+ result := mergeYAMLs (tt .a , tt .b )
781+ if ! reflect .DeepEqual (result , tt .expected ) {
782+ t .Errorf ("expected %v, got %v" , tt .expected , result )
783+ }
784+ })
785+ }
786+ }
0 commit comments