Skip to content

Commit 1bfac4c

Browse files
authored
acc: handle EnvMatrix overrides (#2578)
## Changes Handle EnvMatrix overrides in tests. Implement transformer for mergo. Introducing a new type like EnvMatrix and only overriding behavior for that type doesn't seem to be possible because `tool` doesn't support `inline` declaration. ## Tests Add self-tests
1 parent edd9bdb commit 1bfac4c

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

acceptance/internal/config.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package internal
33
import (
44
"os"
55
"path/filepath"
6+
"reflect"
67
"slices"
78
"sort"
89
"strings"
@@ -145,7 +146,14 @@ func LoadConfig(t *testing.T, dir string) (TestConfig, string) {
145146

146147
for _, cfgName := range configs[1:] {
147148
cfg := DoLoadConfig(t, cfgName)
148-
err := mergo.Merge(&result, cfg, mergo.WithOverride, mergo.WithoutDereference, mergo.WithAppendSlice)
149+
err := mergo.Merge(
150+
&result,
151+
cfg,
152+
mergo.WithOverride,
153+
mergo.WithoutDereference,
154+
mergo.WithAppendSlice,
155+
mergo.WithTransformers(mapTransformer{}),
156+
)
149157
if err != nil {
150158
t.Fatalf("Error during config merge: %s: %s", cfgName, err)
151159
}
@@ -174,6 +182,27 @@ func DoLoadConfig(t *testing.T, path string) TestConfig {
174182
return config
175183
}
176184

185+
// mapTransformer is a mergo transformer that merges two maps
186+
// by overriding values in the destination map with values from the source map.
187+
//
188+
// In our case, source map is located in test directory, and destination map is located
189+
// in a parent directory.
190+
type mapTransformer struct{}
191+
192+
func (t mapTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
193+
if typ.Kind() == reflect.Map {
194+
return func(dst, src reflect.Value) error {
195+
if dst.IsNil() {
196+
dst.Set(reflect.MakeMap(typ))
197+
}
198+
199+
return mergo.Merge(dst.Addr().Interface(), src.Interface(), mergo.WithOverride)
200+
}
201+
}
202+
203+
return nil
204+
}
205+
177206
// This function takes EnvMatrix and expands into a slice of environment configurations.
178207
// Each environment configuration is a slice of env vars in standard Golang format.
179208
// For example,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
FIRST=[FIRST]
22
SECOND=[SECOND]
3+
THIRD=[THIRD]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ "$FIRST" = "one" ] || [ "$FIRST" = "two" ]
2+
[ "$SECOND" = "variantA" ] || [ "$SECOND" = "variantB" ]
3+
[ "$THIRD" = "three" ]
4+
5+
echo "FIRST=$FIRST"
6+
echo "SECOND=$SECOND"
7+
echo "THIRD=$THIRD"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[EnvMatrix]
2+
FIRST = ["one", "two"]
3+
SECOND = ["variantA", "variantB"]

acceptance/selftest/envmatrix/script

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[EnvMatrix]
2-
FIRST = ["one", "two"]
3-
SECOND = ["variantA", "variantB"]
2+
FIRST = ["overriden-in-inner"]
3+
THIRD = ["three"]

0 commit comments

Comments
 (0)