Skip to content

Commit e374ef9

Browse files
fix: matrix expansion logic (#115) (#5294)
* add another test * remove //nolint:gocyclo Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 2dea26b commit e374ef9

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

pkg/model/workflow.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,6 @@ func (j *Job) Matrix() map[string][]interface{} {
390390

391391
// GetMatrixes returns the matrix cross product
392392
// It skips includes and hard fails excludes for non-existing keys
393-
//
394-
//nolint:gocyclo
395393
func (j *Job) GetMatrixes() ([]map[string]interface{}, error) {
396394
matrixes := make([]map[string]interface{}, 0)
397395
if j.Strategy != nil {
@@ -406,31 +404,11 @@ func (j *Job) GetMatrixes() ([]map[string]interface{}, error) {
406404
case []interface{}:
407405
for _, i := range t {
408406
i := i.(map[string]interface{})
409-
extraInclude := true
410-
for k := range i {
411-
if _, ok := m[k]; ok {
412-
includes = append(includes, i)
413-
extraInclude = false
414-
break
415-
}
416-
}
417-
if extraInclude {
418-
extraIncludes = append(extraIncludes, i)
419-
}
407+
includes = append(includes, i)
420408
}
421409
case interface{}:
422410
v := v.(map[string]interface{})
423-
extraInclude := true
424-
for k := range v {
425-
if _, ok := m[k]; ok {
426-
includes = append(includes, v)
427-
extraInclude = false
428-
break
429-
}
430-
}
431-
if extraInclude {
432-
extraIncludes = append(extraIncludes, v)
433-
}
411+
includes = append(includes, v)
434412
}
435413
}
436414
delete(m, "include")

pkg/model/workflow_test.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
"gopkg.in/yaml.v3"
810
)
911

1012
func TestReadWorkflow_StringEvent(t *testing.T) {
@@ -367,10 +369,9 @@ func TestReadWorkflow_Strategy(t *testing.T) {
367369
assert.NoError(t, err)
368370
assert.Equal(t, matrixes,
369371
[]map[string]interface{}{
370-
{"datacenter": "site-c", "node-version": "14.x", "site": "staging"},
371-
{"datacenter": "site-c", "node-version": "16.x", "site": "staging"},
372-
{"datacenter": "site-d", "node-version": "16.x", "site": "staging"},
373-
{"php-version": 5.4},
372+
{"datacenter": "site-c", "node-version": "14.x", "site": "staging", "php-version": 5.4},
373+
{"datacenter": "site-c", "node-version": "16.x", "site": "staging", "php-version": 5.4},
374+
{"datacenter": "site-d", "node-version": "16.x", "site": "staging", "php-version": 5.4},
374375
{"datacenter": "site-a", "node-version": "10.x", "site": "prod"},
375376
{"datacenter": "site-b", "node-version": "12.x", "site": "dev"},
376377
},
@@ -394,6 +395,32 @@ func TestReadWorkflow_Strategy(t *testing.T) {
394395
assert.Equal(t, job.Strategy.FailFast, false)
395396
}
396397

398+
func TestMatrixOnlyIncludes(t *testing.T) {
399+
matrix := map[string][]interface{}{
400+
"include": []interface{}{
401+
map[string]interface{}{"a": "1", "b": "2"},
402+
map[string]interface{}{"a": "3", "b": "4"},
403+
},
404+
}
405+
rN := yaml.Node{}
406+
err := rN.Encode(matrix)
407+
require.NoError(t, err, "encoding matrix should succeed")
408+
job := &Job{
409+
Strategy: &Strategy{
410+
RawMatrix: rN,
411+
},
412+
}
413+
assert.Equal(t, job.Matrix(), matrix)
414+
matrixes, err := job.GetMatrixes()
415+
require.NoError(t, err)
416+
assert.Equal(t, matrixes,
417+
[]map[string]interface{}{
418+
{"a": "1", "b": "2"},
419+
{"a": "3", "b": "4"},
420+
},
421+
)
422+
}
423+
397424
func TestStep_ShellCommand(t *testing.T) {
398425
tests := []struct {
399426
shell string

0 commit comments

Comments
 (0)