Skip to content

Commit

Permalink
Merge pull request #9 from cbrgm/cbrgm-patch-e33f0a
Browse files Browse the repository at this point in the history
tests: increase test coverage, add unit tests
  • Loading branch information
cbrgm authored Apr 28, 2022
2 parents 9fe3743 + 14ede89 commit eadeb31
Show file tree
Hide file tree
Showing 101 changed files with 23,362 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[![release](https://img.shields.io/github/release-pre/cbrgm/githubevents.svg)](https://github.com/cbrgm/githubevents/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/cbrgm/githubevents)](https://goreportcard.com/report/github.com/cbrgm/githubevents)
[![license](https://img.shields.io/badge/Coverage-81.7%25-green.svg)](https://pkg.go.dev/github.com/cbrgm/githubevents/githubevents)
[![license](https://img.shields.io/badge/Coverage-88.3%25-green.svg)](https://pkg.go.dev/github.com/cbrgm/githubevents/githubevents)
[![license](https://img.shields.io/badge/Docs-pkg.go.dev-blue.svg)](https://pkg.go.dev/github.com/cbrgm/githubevents/githubevents)
[![license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/cbrgm/githubevents/blob/master/LICENSE)
![GitHub stars](https://img.shields.io/github/stars/cbrgm/githubevents.svg?label=github%20stars)
Expand Down
208 changes: 208 additions & 0 deletions gen/template_webhook_event_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"errors"
"github.com/google/go-github/v43/github"
"testing"
"sync"
)
{{ range $_, $webhook := .Webhooks }}
Expand Down Expand Up @@ -373,5 +374,212 @@ func TestHandle{{ $action.Handler }}(t *testing.T) {
{{ end }}
func Test{{ $webhook.Event }}(t *testing.T) {
type fields struct {
handler *EventHandler
}
type args struct {
deliveryID string
eventName string
event *github.{{ $webhook.Event }}
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "must trigger {{ $webhook.Event }}Any with unknown event action",
fields: fields{
handler: &EventHandler{
WebhookSecret: "fake",
onBeforeAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onBeforeAny called")
return nil
},
},
},
onAfterAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onAfterAny called")
return nil
},
},
},
on{{ $webhook.Event }}: map[string][]{{ $webhook.Event }}HandleFunc{
{{ $webhook.Event }}AnyAction: {
func(deliveryID string, eventName string, event *github.{{ $webhook.Event }}) error {
t.Log("onAny action called")
return nil
},
},
},
},
},
args: args{
deliveryID: "42",
eventName: {{ $webhook.Event }},
{{ if $webhook.HasActions }}
event: &github.{{ $webhook.Event }}{Action: ptrString("unknown")},
{{ else }}
event: &github.{{ $webhook.Event }}{},
{{ end }}
},
wantErr: false,
},
{{ if $webhook.HasActions }}
{{ range $_, $action := $webhook.Actions }}
{
name: "must trigger {{ $action.Handler }}",
fields: fields{
handler: &EventHandler{
WebhookSecret: "fake",
onBeforeAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onBeforeAny called")
return nil
},
},
},
onAfterAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onAfterAny called")
return nil
},
},
},
on{{ $webhook.Event }}: map[string][]{{ $webhook.Event }}HandleFunc{
{{ $webhook.Event }}AnyAction: {
func(deliveryID string, eventName string, event *github.{{ $webhook.Event }}) error {
t.Log("onAny action called")
return nil
},
},
{{ $action.Handler }}Action: {
func(deliveryID string, eventName string, event *github.{{ $webhook.Event }}) error {
t.Logf("%s action called", {{ $action.Handler }}Action)
return nil
},
},
},
},
},
args: args{
deliveryID: "42",
eventName: "{{ $webhook.Name }}",
event: &github.{{ $webhook.Event }}{Action: ptrString({{ $action.Handler }}Action)},
},
wantErr: false,
},
{
name: "must fail {{ $action.Handler }} with empty action",
fields: fields{
handler: &EventHandler{
WebhookSecret: "fake",
onBeforeAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onBeforeAny called")
return nil
},
},
},
onAfterAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onAfterAny called")
return nil
},
},
},
on{{ $webhook.Event }}: map[string][]{{ $webhook.Event }}HandleFunc{
{{ $webhook.Event }}AnyAction: {
func(deliveryID string, eventName string, event *github.{{ $webhook.Event }}) error {
t.Log("onAny action called")
return nil
},
},
{{ $action.Handler }}Action: {
func(deliveryID string, eventName string, event *github.{{ $webhook.Event }}) error {
t.Logf("%s action called", {{ $action.Handler }}Action)
return nil
},
},
},
},
},
args: args{
deliveryID: "42",
eventName: "{{ $webhook.Name }}",
event: &github.{{ $webhook.Event }}{Action: ptrString("")},
},
wantErr: true,
},
{
name: "must fail {{ $action.Handler }} with nil action",
fields: fields{
handler: &EventHandler{
WebhookSecret: "fake",
onBeforeAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onBeforeAny called")
return nil
},
},
},
onAfterAny: map[string][]EventHandleFunc{
EventAnyAction: {
func(deliveryID string, eventName string, event interface{}) error {
t.Log("onAfterAny called")
return nil
},
},
},
on{{ $webhook.Event }}: map[string][]{{ $webhook.Event }}HandleFunc{
{{ $webhook.Event }}AnyAction: {
func(deliveryID string, eventName string, event *github.{{ $webhook.Event }}) error {
t.Log("onAny action called")
return nil
},
},
{{ $action.Handler }}Action: {
func(deliveryID string, eventName string, event *github.{{ $webhook.Event }}) error {
t.Logf("%s action called", {{ $action.Handler }}Action)
return nil
},
},
},
},
},
args: args{
deliveryID: "42",
eventName: "{{ $webhook.Name }}",
event: &github.{{ $webhook.Event }}{Action: nil},
},
wantErr: true,
},
{{ end }}
{{ end }}
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := &EventHandler{
WebhookSecret: "fake",
mu: sync.RWMutex{},
}
if err := g.{{ $webhook.Event }}(tt.args.deliveryID, tt.args.eventName, tt.args.event); (err != nil) != tt.wantErr {
t.Errorf("{{ $webhook.Event }}() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
{{ end }}
`
3 changes: 3 additions & 0 deletions gen/template_webhook_event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
// Actions are used to identify registered callbacks.
const (
// {{ $webhook.Event }} is the event name of github.{{ $webhook.Event }}'s
{{ $webhook.Event }} = "{{ $webhook.Name }}"
// {{ $webhook.Event }}AnyAction is used to identify callbacks
// listening to all events of type github.{{ $webhook.Event }}
{{ $webhook.Event }}AnyAction = "*"
Expand Down
5 changes: 5 additions & 0 deletions gen/template_webook_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,9 @@ func (g *EventHandler) HandleEventRequest(req *http.Request) error {
}
return nil
}
// ptrString returns a string pointer.
func ptrString(s string) *string {
return &s
}
`
5 changes: 5 additions & 0 deletions githubevents/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,8 @@ func (g *EventHandler) HandleEventRequest(req *http.Request) error {
}
return nil
}

// ptrString returns a string pointer.
func ptrString(s string) *string {
return &s
}
3 changes: 3 additions & 0 deletions githubevents/events_branch_protection_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (

// Actions are used to identify registered callbacks.
const (
// BranchProtectionRuleEvent is the event name of github.BranchProtectionRuleEvent's
BranchProtectionRuleEvent = "branch_protection_rule"

// BranchProtectionRuleEventAnyAction is used to identify callbacks
// listening to all events of type github.BranchProtectionRuleEvent
BranchProtectionRuleEventAnyAction = "*"
Expand Down
Loading

0 comments on commit eadeb31

Please sign in to comment.