This commit is contained in:
2025-01-21 17:19:18 +01:00
parent f77e35731c
commit 92747f0913
11 changed files with 83 additions and 1142 deletions

View File

@@ -14,6 +14,48 @@ import (
)
func TestPRProcessor(t *testing.T) {
tests := []struct {
title string
action string
req func(req *RequestProcessor, mock PullRequestProcessor)
}{
{
title: "Open routine called for PR opening",
action: "opened",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Opened = mock
},
},
{
title: "Re-Open routine called for PR reopening",
action: "reopened",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Opened = mock
},
},
{
title: "Sync routine called for PR sync requests",
action: "synchronized",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Synced = mock
},
},
{
title: "Close routine called for PR closing",
action: "closed",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Closed = mock
},
},
{
title: "Close routine called for PR closing",
action: "reviewed",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Review = mock
},
},
}
var logBuf bytes.Buffer
oldOut := log.Writer()
log.SetOutput(&logBuf)
@@ -26,11 +68,6 @@ func TestPRProcessor(t *testing.T) {
Branch: "branch",
}
req := &RequestProcessor{
configuredRepos: testConfiguration,
git: &common.GitHandlerGeneratorImpl{},
}
event := &common.PullRequestWebhookEvent{
// Action: "opened",
Number: 1,
@@ -56,77 +93,35 @@ func TestPRProcessor(t *testing.T) {
},
}
t.Run("Open routine called for PR opening", func(t *testing.T) {
ctl := gomock.NewController(t)
openedMock := mock_main.NewMockPullRequestProcessor(ctl)
openedMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
ctl := gomock.NewController(t)
mock := mock_main.NewMockPullRequestProcessor(ctl)
mock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
req.Opened = openedMock
event.Action = "opened"
req := &RequestProcessor{
configuredRepos: testConfiguration,
git: &common.GitHandlerGeneratorImpl{},
}
test.req(req, mock)
err := req.ProcessFunc(&common.Request{
Data: event,
event.Action = test.action
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
t.Error("Error processing open PR:", err)
t.Error(logBuf.String())
}
})
}
if err != nil {
t.Error("Error processing open PR:", err)
t.Error(logBuf.String())
}
})
t.Run("Re-Open routine called for PR reopening", func(t *testing.T) {
ctl := gomock.NewController(t)
openedMock := mock_main.NewMockPullRequestProcessor(ctl)
openedMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
req.Opened = openedMock
event.Action = "reopened"
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
t.Error("Error processing open PR:", err)
t.Error(logBuf.String())
}
})
t.Run("Sync routine called for PR sync requests", func(t *testing.T) {
ctl := gomock.NewController(t)
syncMock := mock_main.NewMockPullRequestProcessor(ctl)
syncMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
req.Synced = syncMock
event.Action = "synchronized"
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
t.Error("Error processing sync PR:", err)
t.Error(logBuf.String())
}
})
t.Run("Close routine called for PR closing", func(t *testing.T) {
ctl := gomock.NewController(t)
closedMock := mock_main.NewMockPullRequestProcessor(ctl)
closedMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
req.Closed = closedMock
event.Action = "closed"
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
t.Error("Error processing close PR:", err)
t.Error(logBuf.String())
}
})
req := &RequestProcessor{
configuredRepos: testConfiguration,
git: &common.GitHandlerGeneratorImpl{},
}
t.Run("Edit PR handling", func(t *testing.T) {
/* ctl := gomock.NewController(t)