pr: more unit tests
This commit is contained in:
parent
c757b50c65
commit
dc96392b40
@ -30,6 +30,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:generate mockgen -source=git_utils.go -destination=mock/git_utils.go -typed
|
||||||
|
|
||||||
type GitHandler struct {
|
type GitHandler struct {
|
||||||
DebugLogger bool
|
DebugLogger bool
|
||||||
|
|
||||||
@ -38,7 +40,13 @@ type GitHandler struct {
|
|||||||
GitEmail string
|
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
|
var err error
|
||||||
|
|
||||||
git := new(GitHandler)
|
git := new(GitHandler)
|
||||||
|
@ -110,7 +110,9 @@ nextSubmodule:
|
|||||||
event.Repository = common.RepositoryFromModel(pr.Base.Repo)
|
event.Repository = common.RepositoryFromModel(pr.Base.Repo)
|
||||||
event.Sender = *common.UserFromModel(pr.User)
|
event.Sender = *common.UserFromModel(pr.User)
|
||||||
event.Requested_reviewer = nil
|
event.Requested_reviewer = nil
|
||||||
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
|
|
||||||
|
g := &common.GitHandlerImpl{}
|
||||||
|
git, err := g.CreateGitHandler(GitAuthor, GitEmail, AppName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
|
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)
|
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 {
|
if err != nil {
|
||||||
log.Println("Faield to allocate GitHandler:", err)
|
log.Println("Faield to allocate GitHandler:", err)
|
||||||
return
|
return
|
||||||
|
@ -17,6 +17,7 @@ type RequestProcessor struct {
|
|||||||
|
|
||||||
configuredRepos map[string][]*common.AutogitConfig
|
configuredRepos map[string][]*common.AutogitConfig
|
||||||
gitea common.Gitea
|
gitea common.Gitea
|
||||||
|
git common.GitHandlerGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
|
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)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
|
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,13 @@ func (o *PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git *co
|
|||||||
This commit was autocreated by %s
|
This commit was autocreated by %s
|
||||||
referencing
|
referencing
|
||||||
|
|
||||||
PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
|
PullRequest: %s/%s#%d`,
|
||||||
req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number)
|
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)
|
prjGit, err := o.gitea.CreateRepositoryIfNotExist(git, *req.Repository.Owner, config.GitProjectName)
|
||||||
if err != nil {
|
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 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
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
// "go.uber.org/mock/gomock"
|
|
||||||
"go.uber.org/mock/gomock"
|
"go.uber.org/mock/gomock"
|
||||||
"src.opensuse.org/autogits/common"
|
"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"
|
mock_main "src.opensuse.org/workflow-pr/mock"
|
||||||
// "src.opensuse.org/autogits/common/mock"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPRProcessor(t *testing.T) {
|
func TestPRProcessor(t *testing.T) {
|
||||||
@ -29,6 +28,7 @@ func TestPRProcessor(t *testing.T) {
|
|||||||
|
|
||||||
req := &RequestProcessor{
|
req := &RequestProcessor{
|
||||||
configuredRepos: testConfiguration,
|
configuredRepos: testConfiguration,
|
||||||
|
git: &common.GitHandlerImpl{},
|
||||||
}
|
}
|
||||||
|
|
||||||
event := &common.PullRequestWebhookEvent{
|
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) {
|
t.Run("Close routine called for PR closing", func(t *testing.T) {
|
||||||
ctl := gomock.NewController(t)
|
ctl := gomock.NewController(t)
|
||||||
closedMock := mock_main.NewMockPullRequestProcessor(ctl)
|
closedMock := mock_main.NewMockPullRequestProcessor(ctl)
|
||||||
@ -87,7 +123,97 @@ func TestPRProcessor(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
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())
|
t.Error(logBuf.String())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user