autogits/workflow-pr/maintainership_test.go

281 lines
8.0 KiB
Go

package main
import (
"errors"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
mock_main "src.opensuse.org/workflow-pr/mock"
)
func TestMaintainership(t *testing.T) {
allocateMaintainershipInterface := func(t *testing.T) *mock_main.MockGiteaMaintainershipInterface {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
return mi
}
config := common.AutogitConfig{
Branch: "bar",
Organization: "foo",
GitProjectName: common.DefaultGitPrj,
}
t.Run("No maintainer in empty package", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for package", err)
}
m := MaintainerListForPackage(maintainers, "bar")
if len(m) != 0 {
t.Error("Invalid number of maintainers for package", err)
}
})
t.Run("No maintainer for empty projects", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
m := MaintainerListForProject(maintainers)
if len(m) != 0 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte("{}"), nil)
maintainers, err = ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
m = MaintainerListForProject(maintainers)
if len(m) != 0 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Error in MaintainerListForPackage when remote has an error", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err)
_, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
switch errRet {
case nil:
t.Error("Should have returned an error")
case err:
break
default:
t.Error("Unexpected error received", err)
}
})
t.Run("Error in MaintainerListForProject when remote has an error", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err)
_, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
switch errRet {
case nil:
t.Error("Should have returned an error")
case err:
break
default:
t.Error("Unexpected error received", err)
}
})
t.Run("Multiple project maintainers", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user1", "user2"]
}
`), nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for project", err)
}
m := MaintainerListForProject(maintainers)
if len(m) != 2 {
t.Error("Invalid number of maintainers for project", err)
}
if m[0] != "user1" || m[1] != "user2" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Single project maintainer", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user"]
}
`), nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for project", err)
}
m := MaintainerListForProject(maintainers)
if len(m) != 1 {
t.Error("Invalid number of maintainers for project", err)
}
if m[0] != "user" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Invalid list of project maintainers", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user", 4]
}
`), nil)
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err == nil {
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Invalid list of project maintainers", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": 4
}
`), nil)
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err == nil {
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Multiple package maintainers", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"pkg": ["user1", "user2"],
"": ["user1", "user3"]
}
`), nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for package")
}
m := MaintainerListForPackage(maintainers, "pkg")
if len(m) != 3 {
t.Error("Invalid number of maintainers for package", m)
}
if m[0] != "user1" || m[1] != "user2" || m[2] != "user3" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("No package maintainers and only project maintainer", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"pkg": ["user1", "user2"],
"": ["user1", "user3"]
}
`), nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for package", maintainers)
}
m := MaintainerListForPackage(maintainers, "bar")
if len(m) != 2 {
t.Error("Invalid number of maintainers for package", m)
}
if m[0] != "user1" || m[1] != "user3" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Invalid list of package maintainers", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"pkg": 3,
"": ["user", 4]
}
`), nil)
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err == nil {
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Maintainers not appoved", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).Return(
&models.PullRequest{
Index: 10,
RequestedReviewers: []*models.User{},
},
[]*models.PullReview{},
nil,
)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`{}`), nil)
approved, err := CheckIfMaintainersApproved(mi, config, 10)
if approved || err != nil {
t.Error("Unexpected approved or err:", err)
}
})
t.Run("Maintainers approved", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).Return(
&models.PullRequest{
Index: 10,
RequestedReviewers: []*models.User{},
},
[]*models.PullReview{},
nil,
)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
"": ["test"]
`), nil)
approved, err := CheckIfMaintainersApproved(mi, config, 10)
if !approved || err != nil {
t.Error("Unexpected disapproval or err:", err)
}
})
t.Parallel()
}