1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-24 08:02:18 +02:00

Fix slice unmarshaling (#3097)

closes #3055
This commit is contained in:
Anbraten 2024-01-01 11:03:31 +01:00 committed by GitHub
parent b61013c7a5
commit 6fbf98f1b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 8 deletions

View File

@ -43,7 +43,7 @@ func newDAGCompiler(steps []*dagCompilerStep, prefix string) dagCompiler {
func (c dagCompiler) isDAG() bool { func (c dagCompiler) isDAG() bool {
for _, v := range c.steps { for _, v := range c.steps {
if len(v.dependsOn) != 0 { if v.dependsOn != nil {
return true return true
} }
} }

View File

@ -149,3 +149,22 @@ func TestConvertDAGToStages(t *testing.T) {
}}, }},
}}, stages) }}, stages)
} }
func TestIsDag(t *testing.T) {
steps := []*dagCompilerStep{
{
step: &backend_types.Step{},
},
}
c := newDAGCompiler(steps, "")
assert.False(t, c.isDAG())
steps = []*dagCompilerStep{
{
step: &backend_types.Step{},
dependsOn: []string{},
},
}
c = newDAGCompiler(steps, "")
assert.True(t, c.isDAG())
}

View File

@ -249,3 +249,38 @@ steps:
when: when:
event: success event: success
` `
var sampleSliceYaml = `
steps:
nil_slice:
image: plugins/slack
empty_slice:
image: plugins/slack
depends_on: []
`
func TestSlice(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Parser", func() {
g.It("should marshal a not set slice to nil", func() {
out, err := ParseString(sampleSliceYaml)
if err != nil {
g.Fail(err)
}
g.Assert(out.Steps.ContainerList[0].DependsOn).IsNil()
g.Assert(len(out.Steps.ContainerList[0].DependsOn)).Equal(0)
})
g.It("should marshal an empty slice", func() {
out, err := ParseString(sampleSliceYaml)
if err != nil {
g.Fail(err)
}
g.Assert(out.Steps.ContainerList[1].DependsOn).IsNotNil()
g.Assert(len(out.Steps.ContainerList[1].DependsOn)).Equal(0)
})
})
}

View File

@ -49,20 +49,27 @@ type StructStringOrSlice struct {
} }
func TestStringOrSliceYaml(t *testing.T) { func TestStringOrSliceYaml(t *testing.T) {
str := `{foo: [bar, baz]}` str := `{foo: [bar, "baz"]}`
s := StructStringOrSlice{} s := StructStringOrSlice{}
assert.NoError(t, yaml.Unmarshal([]byte(str), &s)) assert.NoError(t, yaml.Unmarshal([]byte(str), &s))
assert.Equal(t, StringOrSlice{"bar", "baz"}, s.Foo) assert.Equal(t, StringOrSlice{"bar", "baz"}, s.Foo)
d, err := yaml.Marshal(&s) d, err := yaml.Marshal(&s)
assert.NoError(t, err) assert.NoError(t, err)
s2 := StructStringOrSlice{} s = StructStringOrSlice{}
assert.NoError(t, yaml.Unmarshal(d, &s2)) assert.NoError(t, yaml.Unmarshal(d, &s))
assert.Equal(t, StringOrSlice{"bar", "baz"}, s.Foo)
assert.Equal(t, StringOrSlice{"bar", "baz"}, s2.Foo) str = `{foo: []}`
s = StructStringOrSlice{}
assert.NoError(t, yaml.Unmarshal([]byte(str), &s))
assert.Equal(t, StringOrSlice{}, s.Foo)
str = `{}`
s = StructStringOrSlice{}
assert.NoError(t, yaml.Unmarshal([]byte(str), &s))
assert.Nil(t, s.Foo)
} }
type StructSliceorMap struct { type StructSliceorMap struct {

View File

@ -45,7 +45,7 @@ func (s *StringOrSlice) UnmarshalYAML(unmarshal func(any) error) error {
} }
func toStrings(s []any) ([]string, error) { func toStrings(s []any) ([]string, error) {
if len(s) == 0 { if s == nil {
return nil, nil return nil, nil
} }
r := make([]string, len(s)) r := make([]string, len(s))