fix: matrix expansion logic (#115)

* add another test

* remove //nolint:gocyclo
This commit is contained in:
ChristopherHX
2025-06-08 13:23:33 +02:00
committed by GitHub
parent 3293d725a7
commit 4c7216f69b
2 changed files with 33 additions and 28 deletions

View File

@@ -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")

View File

@@ -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