mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-03-24 15:55:03 +01:00
fix: matrix expansion logic (#115)
* add another test * remove //nolint:gocyclo
This commit is contained in:
@@ -390,8 +390,6 @@ func (j *Job) Matrix() map[string][]interface{} {
|
|||||||
|
|
||||||
// GetMatrixes returns the matrix cross product
|
// GetMatrixes returns the matrix cross product
|
||||||
// It skips includes and hard fails excludes for non-existing keys
|
// It skips includes and hard fails excludes for non-existing keys
|
||||||
//
|
|
||||||
//nolint:gocyclo
|
|
||||||
func (j *Job) GetMatrixes() ([]map[string]interface{}, error) {
|
func (j *Job) GetMatrixes() ([]map[string]interface{}, error) {
|
||||||
matrixes := make([]map[string]interface{}, 0)
|
matrixes := make([]map[string]interface{}, 0)
|
||||||
if j.Strategy != nil {
|
if j.Strategy != nil {
|
||||||
@@ -406,31 +404,11 @@ func (j *Job) GetMatrixes() ([]map[string]interface{}, error) {
|
|||||||
case []interface{}:
|
case []interface{}:
|
||||||
for _, i := range t {
|
for _, i := range t {
|
||||||
i := i.(map[string]interface{})
|
i := i.(map[string]interface{})
|
||||||
extraInclude := true
|
includes = append(includes, i)
|
||||||
for k := range i {
|
|
||||||
if _, ok := m[k]; ok {
|
|
||||||
includes = append(includes, i)
|
|
||||||
extraInclude = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if extraInclude {
|
|
||||||
extraIncludes = append(extraIncludes, i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case interface{}:
|
case interface{}:
|
||||||
v := v.(map[string]interface{})
|
v := v.(map[string]interface{})
|
||||||
extraInclude := true
|
includes = append(includes, v)
|
||||||
for k := range v {
|
|
||||||
if _, ok := m[k]; ok {
|
|
||||||
includes = append(includes, v)
|
|
||||||
extraInclude = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if extraInclude {
|
|
||||||
extraIncludes = append(extraIncludes, v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete(m, "include")
|
delete(m, "include")
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReadWorkflow_StringEvent(t *testing.T) {
|
func TestReadWorkflow_StringEvent(t *testing.T) {
|
||||||
@@ -367,10 +369,9 @@ func TestReadWorkflow_Strategy(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, matrixes,
|
assert.Equal(t, matrixes,
|
||||||
[]map[string]interface{}{
|
[]map[string]interface{}{
|
||||||
{"datacenter": "site-c", "node-version": "14.x", "site": "staging"},
|
{"datacenter": "site-c", "node-version": "14.x", "site": "staging", "php-version": 5.4},
|
||||||
{"datacenter": "site-c", "node-version": "16.x", "site": "staging"},
|
{"datacenter": "site-c", "node-version": "16.x", "site": "staging", "php-version": 5.4},
|
||||||
{"datacenter": "site-d", "node-version": "16.x", "site": "staging"},
|
{"datacenter": "site-d", "node-version": "16.x", "site": "staging", "php-version": 5.4},
|
||||||
{"php-version": 5.4},
|
|
||||||
{"datacenter": "site-a", "node-version": "10.x", "site": "prod"},
|
{"datacenter": "site-a", "node-version": "10.x", "site": "prod"},
|
||||||
{"datacenter": "site-b", "node-version": "12.x", "site": "dev"},
|
{"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)
|
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) {
|
func TestStep_ShellCommand(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
shell string
|
shell string
|
||||||
|
|||||||
Reference in New Issue
Block a user