autogits/workflow-pr/pr_test.go
2024-11-10 23:19:23 +01:00

95 lines
2.1 KiB
Go

package main
import (
"bytes"
"log"
"testing"
// "go.uber.org/mock/gomock"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
// mock_common "src.opensuse.org/autogits/common/mock"
mock_main "src.opensuse.org/workflow-pr/mock"
// "src.opensuse.org/autogits/common/mock"
)
func TestPRProcessor(t *testing.T) {
var logBuf bytes.Buffer
oldOut := log.Writer()
log.SetOutput(&logBuf)
defer log.SetOutput(oldOut)
testConfiguration := make(map[string][]*common.AutogitConfig)
testConfiguration["test"] = make([]*common.AutogitConfig, 1, 1)
testConfiguration["test"][0] = &common.AutogitConfig{
Branch: "branch",
}
req := &RequestProcessor{
configuredRepos: testConfiguration,
}
event := &common.PullRequestWebhookEvent{
// Action: "opened",
Number: 1,
Pull_Request: &common.PullRequest{
Id: 1,
Base: common.Head{
Ref: "branch",
Repo: &common.Repository{
Name: "testRepo",
},
},
Head: common.Head{
Ref: "branch",
Repo: &common.Repository{
Name: "testRepo",
},
},
},
Repository: &common.Repository{
Owner: &common.Organization{
Username: "test",
},
},
}
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)
req.Opened = openedMock
event.Action = "opened"
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
t.Error("Error processing open 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 open PR:", err)
t.Error(logBuf.String())
}
})
}