Skip to content

Commit 21129d6

Browse files
authored
acc: Add EnvMatrixExclude config option (#4178)
## Why In #4134 I added special case for engine/readplan vars directly into acceptance_test.go. This generalizes that, making this feature available to other env var combinations.
1 parent bb774b8 commit 21129d6

File tree

4 files changed

+128
-23
lines changed

4 files changed

+128
-23
lines changed

acceptance/acceptance_test.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func testAccept(t *testing.T, inprocessMode bool, singleTest string) int {
331331
t.Parallel()
332332
}
333333

334-
expanded := internal.ExpandEnvMatrix(config.EnvMatrix)
334+
expanded := internal.ExpandEnvMatrix(config.EnvMatrix, config.EnvMatrixExclude)
335335

336336
if len(expanded) == 1 {
337337
// env vars aren't part of the test case name, so log them for debugging
@@ -341,9 +341,6 @@ func testAccept(t *testing.T, inprocessMode bool, singleTest string) int {
341341
runTest(t, dir, 0, coverDir, repls.Clone(), config, expanded[0], envFilters)
342342
} else {
343343
for ind, envset := range expanded {
344-
if forbiddenEnvSet(envset) {
345-
continue
346-
}
347344
envname := strings.Join(envset, "/")
348345
t.Run(envname, func(t *testing.T) {
349346
if !inprocessMode {
@@ -361,23 +358,6 @@ func testAccept(t *testing.T, inprocessMode bool, singleTest string) int {
361358
return selectedDirs - skippedDirs
362359
}
363360

364-
func forbiddenEnvSet(envset []string) bool {
365-
hasTerraform := false
366-
hasReadplan := false
367-
368-
for _, pair := range envset {
369-
if pair == "DATABRICKS_BUNDLE_ENGINE=terraform" {
370-
hasTerraform = true
371-
}
372-
if pair == "READPLAN=1" {
373-
hasReadplan = true
374-
}
375-
}
376-
377-
// Do not run terraform tests with --plan option:
378-
return hasTerraform && hasReadplan
379-
}
380-
381361
func getEnvFilters(t *testing.T) []string {
382362
envFilterValue := os.Getenv(EnvFilterVar)
383363
if envFilterValue == "" {

acceptance/internal/config.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ type TestConfig struct {
100100
// similar to github actions matrix strategy.
101101
EnvMatrix map[string][]string
102102

103+
// Environment variables matrix exclusion list.
104+
// Exclude certain configuration from the grid generated by EnvMatrix.
105+
// They key is arbitrary string and the value is a list of KEY=value pairs to exclude.
106+
// For example:
107+
// EnvMatrixExclude.noplantf = ["READPLAN=1", "DATABRICKS_BUNDLE_ENGINE=terraform"]
108+
EnvMatrixExclude map[string][]string
109+
103110
// List of keys for which to do string replacement value -> [KEY]. If not set, defaults to true.
104111
EnvRepl map[string]bool
105112

@@ -269,7 +276,8 @@ func (t mapTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Valu
269276
// output: [["KEY=A", "OTHER=VALUE"], ["KEY=B", "OTHER=VALUE"]]
270277
//
271278
// If any entries is an empty list, that variable is dropped from the matrix before processing.
272-
func ExpandEnvMatrix(matrix map[string][]string) [][]string {
279+
// The exclude parameter specifies combinations to exclude from the result.
280+
func ExpandEnvMatrix(matrix, exclude map[string][]string) [][]string {
273281
result := [][]string{{}}
274282

275283
if len(matrix) == 0 {
@@ -314,5 +322,51 @@ func ExpandEnvMatrix(matrix map[string][]string) [][]string {
314322
result = newResult
315323
}
316324

325+
// Filter out excluded combinations
326+
if len(exclude) > 0 {
327+
result = filterExcludedEnvSets(result, exclude)
328+
}
329+
317330
return result
318331
}
332+
333+
// filterExcludedEnvSets removes environment sets that match exclusion rules.
334+
// An environment set is excluded if it contains all KEY=value pairs from any exclusion rule.
335+
func filterExcludedEnvSets(envSets [][]string, exclude map[string][]string) [][]string {
336+
var filtered [][]string
337+
338+
for _, envSet := range envSets {
339+
excluded := false
340+
341+
// Check each exclusion rule
342+
for _, excludeRule := range exclude {
343+
if matchesExclusionRule(envSet, excludeRule) {
344+
excluded = true
345+
break
346+
}
347+
}
348+
349+
if !excluded {
350+
filtered = append(filtered, envSet)
351+
}
352+
}
353+
354+
return filtered
355+
}
356+
357+
// matchesExclusionRule returns true if envSet contains all KEY=value pairs from excludeRule.
358+
func matchesExclusionRule(envSet, excludeRule []string) bool {
359+
for _, excludePair := range excludeRule {
360+
found := false
361+
for _, envPair := range envSet {
362+
if envPair == excludePair {
363+
found = true
364+
break
365+
}
366+
}
367+
if !found {
368+
return false
369+
}
370+
}
371+
return true
372+
}

acceptance/internal/config_test.go

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func TestExpandEnvMatrix(t *testing.T) {
1010
tests := []struct {
1111
name string
1212
matrix map[string][]string
13+
exclude map[string][]string
1314
expected [][]string
1415
}{
1516
{
@@ -90,11 +91,80 @@ func TestExpandEnvMatrix(t *testing.T) {
9091
{"KEY=B", "OTHER=VALUE"},
9192
},
9293
},
94+
{
95+
name: "exclude single combination",
96+
matrix: map[string][]string{
97+
"KEY1": {"A", "B"},
98+
"KEY2": {"C", "D"},
99+
},
100+
exclude: map[string][]string{
101+
"rule1": {"KEY1=A", "KEY2=C"},
102+
},
103+
expected: [][]string{
104+
{"KEY1=A", "KEY2=D"},
105+
{"KEY1=B", "KEY2=C"},
106+
{"KEY1=B", "KEY2=D"},
107+
},
108+
},
109+
{
110+
name: "exclude multiple combinations",
111+
matrix: map[string][]string{
112+
"KEY1": {"A", "B"},
113+
"KEY2": {"C", "D"},
114+
},
115+
exclude: map[string][]string{
116+
"rule1": {"KEY1=A", "KEY2=C"},
117+
"rule2": {"KEY1=B", "KEY2=D"},
118+
},
119+
expected: [][]string{
120+
{"KEY1=A", "KEY2=D"},
121+
{"KEY1=B", "KEY2=C"},
122+
},
123+
},
124+
{
125+
name: "exclude with terraform and readplan example",
126+
matrix: map[string][]string{
127+
"DATABRICKS_BUNDLE_ENGINE": {"terraform", "direct"},
128+
"READPLAN": {"0", "1"},
129+
},
130+
exclude: map[string][]string{
131+
"noplantf": {"READPLAN=1", "DATABRICKS_BUNDLE_ENGINE=terraform"},
132+
},
133+
expected: [][]string{
134+
{"DATABRICKS_BUNDLE_ENGINE=terraform", "READPLAN=0"},
135+
{"DATABRICKS_BUNDLE_ENGINE=direct", "READPLAN=0"},
136+
{"DATABRICKS_BUNDLE_ENGINE=direct", "READPLAN=1"},
137+
},
138+
},
139+
{
140+
name: "exclude rule with subset of keys matches",
141+
matrix: map[string][]string{
142+
"KEY1": {"A"},
143+
"KEY2": {"B"},
144+
"KEY3": {"C"},
145+
},
146+
exclude: map[string][]string{
147+
"rule1": {"KEY1=A", "KEY2=B"},
148+
},
149+
expected: nil,
150+
},
151+
{
152+
name: "exclude rule with more keys than envset does not match",
153+
matrix: map[string][]string{
154+
"KEY1": {"A"},
155+
},
156+
exclude: map[string][]string{
157+
"rule1": {"KEY1=A", "KEY2=B"},
158+
},
159+
expected: [][]string{
160+
{"KEY1=A"},
161+
},
162+
},
93163
}
94164

95165
for _, tt := range tests {
96166
t.Run(tt.name, func(t *testing.T) {
97-
result := ExpandEnvMatrix(tt.matrix)
167+
result := ExpandEnvMatrix(tt.matrix, tt.exclude)
98168
assert.Equal(t, tt.expected, result)
99169
})
100170
}

acceptance/test.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Env.PYTHONUNBUFFERED = "1"
1616
Env.PYTHONUTF8 = "1"
1717

1818
EnvMatrix.DATABRICKS_BUNDLE_ENGINE = ["terraform", "direct"]
19+
EnvMatrixExclude.noplantf = ["DATABRICKS_BUNDLE_ENGINE=terraform", "READPLAN=1"]
1920
EnvRepl.DATABRICKS_BUNDLE_ENGINE = false
2021

2122
# >>> datetime.datetime.fromtimestamp(18000000000)

0 commit comments

Comments
 (0)