pr: more unit tests

This commit is contained in:
Adam Majer 2024-11-11 15:52:34 +01:00
parent c757b50c65
commit dc96392b40
5 changed files with 158 additions and 14 deletions

View File

@ -30,6 +30,8 @@ import (
"sync"
)
//go:generate mockgen -source=git_utils.go -destination=mock/git_utils.go -typed
type GitHandler struct {
DebugLogger bool
@ -38,7 +40,13 @@ type GitHandler struct {
GitEmail string
}
func CreateGitHandler(git_author, email, name string) (*GitHandler, error) {
type GitHandlerGenerator interface {
CreateGitHandler(git_author, email, name string) (*GitHandler, error);
}
type GitHandlerImpl struct {}
func (*GitHandlerImpl) CreateGitHandler(git_author, email, name string) (*GitHandler, error) {
var err error
git := new(GitHandler)

View File

@ -110,7 +110,9 @@ nextSubmodule:
event.Repository = common.RepositoryFromModel(pr.Base.Repo)
event.Sender = *common.UserFromModel(pr.User)
event.Requested_reviewer = nil
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
g := &common.GitHandlerImpl{}
git, err := g.CreateGitHandler(GitAuthor, GitEmail, AppName)
if err != nil {
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
}
@ -178,7 +180,8 @@ func checkRepos(processor *RequestProcessor) {
}
log.Printf(" ++ starting verification, org: `%s` config: `%s`\n", org, config.GitProjectName)
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
g := &common.GitHandlerImpl{}
git, err := g.CreateGitHandler(GitAuthor, GitEmail, AppName)
if err != nil {
log.Println("Faield to allocate GitHandler:", err)
return
@ -257,13 +260,13 @@ func main() {
}
req.Synced = &PullRequestSynced{
gitea: req.gitea,
gitea: req.gitea,
}
req.Opened = &PullRequestOpened{
gitea: req.gitea,
gitea: req.gitea,
}
req.Closed = &PullRequestClosed{
gitea: req.gitea,
gitea: req.gitea,
}
req.gitea = common.AllocateGiteaTransport(*giteaHost)

View File

@ -17,6 +17,7 @@ type RequestProcessor struct {
configuredRepos map[string][]*common.AutogitConfig
gitea common.Gitea
git common.GitHandlerGenerator
}
func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
@ -44,7 +45,7 @@ func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
return fmt.Errorf("Cannot find config for branch '%s'", req.Pull_Request.Base.Ref)
}
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
git, err := w.git.CreateGitHandler(GitAuthor, GitEmail, AppName)
if err != nil {
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
}

View File

@ -23,8 +23,13 @@ func (o *PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git *co
This commit was autocreated by %s
referencing
PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number)
PullRequest: %s/%s#%d`,
req.Repository.Owner.Username,
req.Repository.Name,
GitAuthor,
req.Repository.Name,
req.Pull_Request.Number,
)
prjGit, err := o.gitea.CreateRepositoryIfNotExist(git, *req.Repository.Owner, config.GitProjectName)
if err != nil {
@ -55,5 +60,6 @@ referencing the following pull request:
return fmt.Errorf("Failed to create reviewer '%s' for request: %s/%s/%d Err: %w", reviewer, PR.Base.Repo.Owner.UserName, PR.Base.Repo.Name, PR.Index, err)
}
}
return err
return nil
}

View File

@ -2,16 +2,15 @@ package main
import (
"bytes"
"fmt"
"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_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) {
@ -29,6 +28,7 @@ func TestPRProcessor(t *testing.T) {
req := &RequestProcessor{
configuredRepos: testConfiguration,
git: &common.GitHandlerImpl{},
}
event := &common.PullRequestWebhookEvent{
@ -74,6 +74,42 @@ func TestPRProcessor(t *testing.T) {
}
})
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)
@ -87,7 +123,97 @@ func TestPRProcessor(t *testing.T) {
})
if err != nil {
t.Error("Error processing open PR:", err)
t.Error("Error processing close PR:", err)
t.Error(logBuf.String())
}
})
t.Run("Edit PR handling", 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 = "edited"
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
t.Error("Error processing edit PR:", err)
t.Error(logBuf.String())
}
})
t.Run("Unknown PR-type handling", func(t *testing.T) {
event.Action = "not existing action"
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err == nil {
t.Error(logBuf.String())
}
})
t.Run("Missing branch in config present in PR", func(t *testing.T) {
baseRef := event.Pull_Request.Base.Ref
event.Pull_Request.Base.Ref = "not present"
err := req.ProcessFunc(&common.Request{
Data: event,
})
event.Pull_Request.Base.Ref = baseRef
if err == nil {
t.Error(logBuf.String())
}
})
t.Run("Invalid data present in PR", func(t *testing.T) {
baseConfig := req.configuredRepos
req.configuredRepos = make(map[string][]*common.AutogitConfig)
err := req.ProcessFunc(&common.Request{
Data: nil,
})
req.configuredRepos = baseConfig
if err == nil {
t.Error(logBuf.String())
}
})
t.Run("Ignoring requests against unconfigured repos", func(t *testing.T) {
baseConfig := req.configuredRepos
req.configuredRepos = make(map[string][]*common.AutogitConfig)
err := req.ProcessFunc(&common.Request{
Data: event,
})
req.configuredRepos = baseConfig
if err != nil {
t.Error(logBuf.String())
}
})
t.Run("Failures of git handler creation", func(t *testing.T) {
ctl := gomock.NewController(t)
gitHandler := mock_common.NewMockGitHandlerGenerator(ctl)
gitHandler.EXPECT().CreateGitHandler(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("some error"))
origHandler := req.git
req.git = gitHandler
err := req.ProcessFunc(&common.Request{
Data: event,
})
req.git = origHandler
if err == nil {
t.Error(logBuf.String())
}
})