more tests

This commit is contained in:
Adam Majer 2024-11-12 14:26:36 +01:00
parent dc96392b40
commit e8e51e21ca
4 changed files with 274 additions and 13 deletions

View File

@ -41,17 +41,17 @@ type GitHandler struct {
}
type GitHandlerGenerator interface {
CreateGitHandler(git_author, email, name string) (*GitHandler, error);
CreateGitHandler(git_author, email, prj_name string) (*GitHandler, error);
}
type GitHandlerImpl struct {}
func (*GitHandlerImpl) CreateGitHandler(git_author, email, name string) (*GitHandler, error) {
func (*GitHandlerImpl) CreateGitHandler(git_author, email, prj_name string) (*GitHandler, error) {
var err error
git := new(GitHandler)
git.GitCommiter = git_author
git.GitPath, err = os.MkdirTemp("", name)
git.GitPath, err = os.MkdirTemp("", prj_name)
if err != nil {
return nil, fmt.Errorf("Cannot create temp dir: %w", err)
}

View File

@ -105,7 +105,6 @@ func setupGitForTests(t *testing.T, git *common.GitHandler) {
{"prj", "add", ".gitmodules", "testRepo"},
{"prj", "commit", "-m", "First instance"},
{"prj", "submodule", "deinit", "testRepo"},
{"", "clone", "prj", common.DefaultGitPrj},
{LocalCMD, "foo", "/usr/bin/touch", "file2"},
{"foo", "add", "file2"},
{"foo", "commit", "-m", "added file2"},
@ -122,8 +121,7 @@ func TestUpdatePrBranch(t *testing.T) {
Repository: &common.Repository{
Name: "testRepo",
},
Pull_Request: &common.PullRequest {
},
Pull_Request: &common.PullRequest{},
}
git := &common.GitHandler{
@ -134,6 +132,8 @@ func TestUpdatePrBranch(t *testing.T) {
}
setupGitForTests(t, git)
gitExecs(t, git, [][]string{{"", "clone", "prj", common.DefaultGitPrj}})
revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n")
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
@ -160,8 +160,7 @@ func TestCreatePrBranch(t *testing.T) {
Repository: &common.Repository{
Name: "testRepo",
},
Pull_Request: &common.PullRequest {
},
Pull_Request: &common.PullRequest{},
}
git := &common.GitHandler{
@ -172,6 +171,8 @@ func TestCreatePrBranch(t *testing.T) {
}
setupGitForTests(t, git)
gitExecs(t, git, [][]string{{"", "clone", "prj", common.DefaultGitPrj}})
revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n")
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
@ -194,4 +195,3 @@ func TestCreatePrBranch(t *testing.T) {
// os.CopyFS("/tmp/test", os.DirFS(git.GitPath))
}
}

View File

@ -0,0 +1,84 @@
package main
import (
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
mock_common "src.opensuse.org/autogits/common/mock"
)
func TestClosePR(t *testing.T) {
pr := PullRequestClosed{}
config := &common.AutogitConfig{
Reviewers: []string{"reviewer1", "reviewer2"},
Branch: "branch",
Organization: "test",
GitProjectName: "prj",
}
event := &common.PullRequestWebhookEvent{
Action: "closed",
Number: 1,
Pull_Request: &common.PullRequest{
Id: 1,
Base: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
Head: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
},
Repository: &common.Repository{
Owner: &common.Organization{
Username: "test",
},
},
}
git := &common.GitHandler{
GitCommiter: "tester",
GitEmail: "test@suse.com",
}
t.Run("PR git closed request against PrjGit == no action", func(t *testing.T) {
ctl := gomock.NewController(t)
pr.gitea = mock_common.NewMockGitea(ctl)
git.GitPath = t.TempDir()
config.GitProjectName = "testRepo"
event.Repository.Name = "testRepo"
if err := pr.Process(event, git, config); err != nil {
t.Error("Error PrjGit closed request. Should be no error.", err)
}
})
t.Run("PR git closed", func(t *testing.T) {
ctl := gomock.NewController(t)
pr.gitea = mock_common.NewMockGitea(ctl)
git.GitPath = t.TempDir()
config.GitProjectName = "prjGit"
event.Repository.Name = "tester"
if err := pr.Process(event, git, config); err != nil {
t.Error("Error PrjGit closed request. Should be no error.", err)
}
})
}

View File

@ -0,0 +1,177 @@
package main
import (
"errors"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
mock_common "src.opensuse.org/autogits/common/mock"
)
func TestOpenPR(t *testing.T) {
pr := PullRequestOpened{}
config := &common.AutogitConfig{
Reviewers: []string{"reviewer1", "reviewer2"},
Branch: "branch",
Organization: "test",
GitProjectName: "prj",
}
event := &common.PullRequestWebhookEvent{
Action: "opened",
Number: 1,
Pull_Request: &common.PullRequest{
Id: 1,
Base: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
Head: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
},
Repository: &common.Repository{
Owner: &common.Organization{
Username: "test",
},
},
}
git := &common.GitHandler{
GitCommiter: "tester",
GitEmail: "test@suse.com",
}
t.Run("PR git opened request against PrjGit == no action", func(t *testing.T) {
ctl := gomock.NewController(t)
pr.gitea = mock_common.NewMockGitea(ctl)
git.GitPath = t.TempDir()
config.GitProjectName = "testRepo"
event.Repository.Name = "testRepo"
if err := pr.Process(event, git, config); err != nil {
t.Error("Error PrjGit opened request. Should be no error.", err)
}
})
t.Run("Open PrjGit PR", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
prjgit := &models.Repository{
SSHURL: "./prj",
DefaultBranch: "testing",
}
giteaPR := &models.PullRequest{}
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(prjgit, nil)
gitea.EXPECT().CreatePullRequestIfNotExist(prjgit, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(giteaPR, nil)
gitea.EXPECT().RequestReviews(giteaPR, "reviewer1").Return(nil, nil)
gitea.EXPECT().RequestReviews(giteaPR, "reviewer2").Return(nil, nil)
err := pr.Process(event, git, config)
if err != nil {
t.Error("error:", err)
}
})
t.Run("Cannot create prjgit repository", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
failedErr := errors.New("Returned error here")
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(nil, failedErr)
err := pr.Process(event, git, config)
if err != failedErr {
t.Error("error:", err)
}
})
t.Run("Cannot create PR", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
prjgit := &models.Repository{
SSHURL: "./prj",
DefaultBranch: "testing",
}
failedErr := errors.New("Returned error here")
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(prjgit, nil)
gitea.EXPECT().CreatePullRequestIfNotExist(prjgit, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, failedErr)
err := pr.Process(event, git, config)
if err != failedErr {
t.Error("error:", err)
}
})
t.Run("Open PrjGit PR", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
prjgit := &models.Repository{
Name: "SomeRepo",
Owner: &models.User {
UserName: "org",
},
SSHURL: "./prj",
DefaultBranch: "testing",
}
giteaPR := &models.PullRequest{
Base: &models.PRBranchInfo {
Repo: prjgit,
},
Index: 13,
}
failedErr := errors.New("Returned error here")
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(prjgit, nil)
gitea.EXPECT().CreatePullRequestIfNotExist(prjgit, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(giteaPR, nil)
gitea.EXPECT().RequestReviews(giteaPR, "reviewer1").Return(nil, failedErr)
// gitea.EXPECT().RequestReviews(giteaPR, "reviewer2").Return(nil, nil)
err := pr.Process(event, git, config)
if errors.Unwrap(err) != failedErr {
t.Error("error:", err)
}
})
}