autogits/workflow-pr/maintainership_test.go

281 lines
8.0 KiB
Go
Raw Normal View History

2024-11-27 17:50:55 +01:00
package main
import (
"errors"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
2024-11-29 17:33:01 +01:00
"src.opensuse.org/autogits/common/gitea-generated/models"
2024-11-27 17:50:55 +01:00
mock_main "src.opensuse.org/workflow-pr/mock"
)
func TestMaintainership(t *testing.T) {
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
allocateMaintainershipInterface := func(t *testing.T) *mock_main.MockGiteaMaintainershipInterface {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
return mi
}
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
config := common.AutogitConfig{
Branch: "bar",
Organization: "foo",
GitProjectName: common.DefaultGitPrj,
}
2024-11-27 17:50:55 +01:00
t.Run("No maintainer in empty package", func(t *testing.T) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
2024-11-27 17:50:55 +01:00
2024-11-29 17:33:01 +01:00
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 {
2024-11-27 17:50:55 +01:00
t.Error("Invalid number of maintainers for package", err)
}
})
t.Run("No maintainer for empty projects", func(t *testing.T) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
2024-11-29 17:33:01 +01:00
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
m := MaintainerListForProject(maintainers)
2024-11-27 17:50:55 +01:00
if len(m) != 0 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
2024-11-28 00:15:32 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte("{}"), nil)
2024-11-29 17:33:01 +01:00
maintainers, err = ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
m = MaintainerListForProject(maintainers)
2024-11-28 00:15:32 +01:00
if len(m) != 0 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
2024-11-27 17:50:55 +01:00
})
t.Run("Error in MaintainerListForPackage when remote has an error", func(t *testing.T) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err)
2024-11-27 17:50:55 +01:00
2024-11-29 17:33:01 +01:00
_, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
2024-11-27 17:50:55 +01:00
switch errRet {
case nil:
t.Error("Should have returned an error")
case err:
break
default:
t.Error("Unexpected error received", err)
}
})
2024-11-28 17:10:26 +01:00
2024-11-27 17:50:55 +01:00
t.Run("Error in MaintainerListForProject when remote has an error", func(t *testing.T) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err)
2024-11-29 17:33:01 +01:00
_, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
2024-11-27 17:50:55 +01:00
switch errRet {
case nil:
t.Error("Should have returned an error")
case err:
break
default:
t.Error("Unexpected error received", err)
}
})
2024-11-28 00:15:32 +01:00
t.Run("Multiple project maintainers", func(t *testing.T) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user1", "user2"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-27 17:50:55 +01:00
2024-11-29 17:33:01 +01:00
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 {
2024-11-27 17:50:55 +01:00
t.Error("Invalid number of maintainers for project", err)
}
2024-11-28 00:15:32 +01:00
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) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
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 {
2024-11-28 00:15:32 +01:00
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) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user", 4]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err == nil {
2024-11-28 00:15:32 +01:00
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Invalid list of project maintainers", func(t *testing.T) {
2024-11-28 17:10:26 +01:00
mi := allocateMaintainershipInterface(t)
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": 4
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
2024-11-28 17:10:26 +01:00
if err == nil {
2024-11-28 00:15:32 +01:00
t.Error("Invalid number of maintainers for project", err)
}
2024-11-27 17:50:55 +01:00
})
2024-11-28 17:10:26 +01:00
t.Run("Multiple package maintainers", func(t *testing.T) {
mi := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
2024-11-28 17:10:26 +01:00
{
"pkg": ["user1", "user2"],
"": ["user1", "user3"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-29 17:33:01 +01:00
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 {
2024-11-28 17:10:26 +01:00
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(`
2024-11-28 17:10:26 +01:00
{
"pkg": ["user1", "user2"],
"": ["user1", "user3"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-29 17:33:01 +01:00
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 {
2024-11-28 17:10:26 +01:00
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(`
2024-11-28 17:10:26 +01:00
{
"pkg": 3,
"": ["user", 4]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-29 17:33:01 +01:00
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err == nil {
2024-11-28 17:10:26 +01:00
t.Error("Invalid number of maintainers for project", err)
}
})
2024-11-29 17:33:01 +01:00
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)
}
})
2024-11-28 17:10:26 +01:00
t.Parallel()
2024-11-27 17:50:55 +01:00
}