autogits/workflow-pr/maintainership_test.go

217 lines
5.6 KiB
Go

package main
import (
"errors"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
mock_main "src.opensuse.org/workflow-pr/mock"
)
func TestMaintainership(t *testing.T) {
allocateMaintainershipInterface := func(t *testing.T) *mock_main.MockGiteaMaintainershipInterface {
t.Parallel()
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)
m, err := MaintainerListForPackage(mi, config, "bar")
if len(m) != 0 || err != nil {
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)
m, err := MaintainerListForProject(mi, config)
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)
m, err = MaintainerListForProject(mi, config)
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 := MaintainerListForPackage(mi, config, "bar")
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 := MaintainerListForProject(mi, config)
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)
m, err := MaintainerListForProject(mi, config)
if len(m) != 2 || err != nil {
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)
m, err := MaintainerListForProject(mi, config)
if len(m) != 1 || err != nil {
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)
m, err := MaintainerListForProject(mi, config)
if len(m) != 0 || 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 := MaintainerListForProject(mi, config)
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)
m, err := MaintainerListForPackage(mi, config, "pkg")
if len(m) != 3 || err != nil {
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)
m, err := MaintainerListForPackage(mi, config, "bar")
if len(m) != 2 || err != nil {
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)
m, err := MaintainerListForPackage(mi, config, "pkg")
if len(m) != 0 || err == nil {
t.Error("Invalid number of maintainers for project", err)
}
})
t.Parallel()
}