diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index 7a93c379..b2e79c12 100644 --- a/pkg/model/workflow.go +++ b/pkg/model/workflow.go @@ -390,8 +390,6 @@ func (j *Job) Matrix() map[string][]interface{} { // GetMatrixes returns the matrix cross product // It skips includes and hard fails excludes for non-existing keys -// -//nolint:gocyclo func (j *Job) GetMatrixes() ([]map[string]interface{}, error) { matrixes := make([]map[string]interface{}, 0) if j.Strategy != nil { @@ -406,31 +404,11 @@ func (j *Job) GetMatrixes() ([]map[string]interface{}, error) { case []interface{}: for _, i := range t { i := i.(map[string]interface{}) - extraInclude := true - for k := range i { - if _, ok := m[k]; ok { - includes = append(includes, i) - extraInclude = false - break - } - } - if extraInclude { - extraIncludes = append(extraIncludes, i) - } + includes = append(includes, i) } case interface{}: v := v.(map[string]interface{}) - extraInclude := true - for k := range v { - if _, ok := m[k]; ok { - includes = append(includes, v) - extraInclude = false - break - } - } - if extraInclude { - extraIncludes = append(extraIncludes, v) - } + includes = append(includes, v) } } delete(m, "include") diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go index adf7ab4f..7a98ce7c 100644 --- a/pkg/model/workflow_test.go +++ b/pkg/model/workflow_test.go @@ -5,6 +5,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestReadWorkflow_StringEvent(t *testing.T) { @@ -367,10 +369,9 @@ func TestReadWorkflow_Strategy(t *testing.T) { assert.NoError(t, err) assert.Equal(t, matrixes, []map[string]interface{}{ - {"datacenter": "site-c", "node-version": "14.x", "site": "staging"}, - {"datacenter": "site-c", "node-version": "16.x", "site": "staging"}, - {"datacenter": "site-d", "node-version": "16.x", "site": "staging"}, - {"php-version": 5.4}, + {"datacenter": "site-c", "node-version": "14.x", "site": "staging", "php-version": 5.4}, + {"datacenter": "site-c", "node-version": "16.x", "site": "staging", "php-version": 5.4}, + {"datacenter": "site-d", "node-version": "16.x", "site": "staging", "php-version": 5.4}, {"datacenter": "site-a", "node-version": "10.x", "site": "prod"}, {"datacenter": "site-b", "node-version": "12.x", "site": "dev"}, }, @@ -394,6 +395,32 @@ func TestReadWorkflow_Strategy(t *testing.T) { assert.Equal(t, job.Strategy.FailFast, false) } +func TestMatrixOnlyIncludes(t *testing.T) { + matrix := map[string][]interface{}{ + "include": []interface{}{ + map[string]interface{}{"a": "1", "b": "2"}, + map[string]interface{}{"a": "3", "b": "4"}, + }, + } + rN := yaml.Node{} + err := rN.Encode(matrix) + require.NoError(t, err, "encoding matrix should succeed") + job := &Job{ + Strategy: &Strategy{ + RawMatrix: rN, + }, + } + assert.Equal(t, job.Matrix(), matrix) + matrixes, err := job.GetMatrixes() + require.NoError(t, err) + assert.Equal(t, matrixes, + []map[string]interface{}{ + {"a": "1", "b": "2"}, + {"a": "3", "b": "4"}, + }, + ) +} + func TestStep_ShellCommand(t *testing.T) { tests := []struct { shell string