Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions compiler/types/yaml/yaml/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ func (r *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error {
})

// attempt to unmarshal simple ruleset
//nolint:errcheck // intentionally not handling error
unmarshal(simple)
err := unmarshal(simple)
if err != nil {
return err
}
// attempt to unmarshal advanced ruleset
//nolint:errcheck // intentionally not handling error
unmarshal(advanced)
err = unmarshal(advanced)
if err != nil {
return err
}

// set ruleset `unless` to advanced `unless` rules
r.Unless = advanced.Unless
Expand Down
44 changes: 40 additions & 4 deletions compiler/types/yaml/yaml/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"os"
"reflect"
"testing"

Check failure on line 9 in compiler/types/yaml/yaml/ruleset_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/types/yaml/yaml/ruleset_test.go#L9

File is not properly formatted (gci)
Raw output
compiler/types/yaml/yaml/ruleset_test.go:9:1: File is not properly formatted (gci)

^
"gopkg.in/yaml.v3"

"github.com/go-vela/server/compiler/types/pipeline"
"github.com/google/go-cmp/cmp"
)

func TestYaml_Ruleset_ToPipeline(t *testing.T) {
Expand Down Expand Up @@ -95,8 +96,9 @@
func TestYaml_Ruleset_UnmarshalYAML(t *testing.T) {
// setup tests
tests := []struct {
file string
want *Ruleset
file string
want *Ruleset
wantErr bool
}{
{
file: "testdata/ruleset_simple.yml",
Expand Down Expand Up @@ -148,6 +150,27 @@
Matcher: "regex",
},
},
{
file: "testdata/ruleset_unknown_field.yml",
want: &Ruleset{
If: Rules{
Branch: []string{"main"},
Event: []string{"push"},
},
Matcher: "filepath",
Operator: "and",
},
},
{
file: "testdata/ruleset_collide.yml",
want: nil,
wantErr: true,
},
{
file: "testdata/ruleset_collide_adv.yml",
want: nil,
wantErr: true,
},
}

// run tests
Expand All @@ -161,12 +184,20 @@

err = yaml.Unmarshal(b, got)

if test.wantErr {
if err == nil {
t.Errorf("UnmarshalYAML should have returned err")
}

continue
}

if err != nil {
t.Errorf("UnmarshalYAML returned err: %v", err)
}

if !reflect.DeepEqual(got, test.want) {
t.Errorf("UnmarshalYAML is %v, want %v", got, test.want)
if diff := cmp.Diff(got, test.want); diff != "" {
t.Errorf("UnmarshalYAML mismatch (-got +want):\n%s", diff)
}
}
}
Expand Down Expand Up @@ -247,6 +278,11 @@
Target: []string{"production"},
},
},
{
failure: true,
file: "testdata/ruleset_collide.yml",
want: nil,
},
{
failure: true,
file: "",
Expand Down
4 changes: 4 additions & 0 deletions compiler/types/yaml/yaml/testdata/ruleset_collide.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
branch: main
branch:
event: push
8 changes: 8 additions & 0 deletions compiler/types/yaml/yaml/testdata/ruleset_collide_adv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
if:
event: push
branch: main
event:
unless:
tag: v3*
matcher: filepath
4 changes: 4 additions & 0 deletions compiler/types/yaml/yaml/testdata/ruleset_unknown_field.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
event: push
branch: main
user: octocat
Loading