autogits/workflow-pr/maintainership_test.go
2024-12-11 14:41:51 +01:00

133 lines
3.6 KiB
Go

package main
import (
"errors"
"slices"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
mock_common "src.opensuse.org/autogits/common/mock"
)
func TestMaintainership(t *testing.T) {
config := common.AutogitConfig{
Branch: "bar",
Organization: "foo",
GitProjectName: common.DefaultGitPrj,
}
packageTests := []struct {
name string
maintainersFile []byte
maintainersFileErr error
maintainers []string
otherError bool
packageName string
}{
/* PACKAGE MAINTAINERS */
// package tests have packageName, projects do not
{
name: "No maintainer in empty package",
packageName: "foo",
},
{
name: "Error in MaintainerListForPackage when remote has an error",
maintainersFileErr: errors.New("some error here"),
packageName: "foo",
},
{
name: "Multiple package maintainers",
maintainersFile: []byte(`{"pkg": ["user1", "user2"], "": ["user1", "user3"]}`),
maintainers: []string{"user1", "user2", "user3"},
packageName: "pkg",
},
{
name: "No package maintainers and only project maintainer",
maintainersFile: []byte(`{"pkg2": ["user1", "user2"], "": ["user1", "user3"]}`),
maintainers: []string{"user1", "user3"},
packageName: "pkg",
},
{
name: "Invalid list of package maintainers",
maintainersFile: []byte(`{"pkg": 3,"": ["user", 4]}`),
otherError: true,
packageName: "pkg",
},
/* PROJECT MAINTAINERS */
{
name: "No maintainer for empty project",
},
{
name: "No maintainer for empty project maintainer file",
maintainersFile: []byte("{}"),
},
{
name: "Error in MaintainerListForProject when remote has an error",
maintainersFileErr: errors.New("some error here"),
},
{
name: "Multiple project maintainers",
maintainersFile: []byte(`{"": ["user1", "user2"]}`),
maintainers: []string{"user1", "user2"},
},
{
name: "Single project maintainer",
maintainersFile: []byte(`{"": ["user"]}`),
maintainers: []string{"user"},
},
{
name: "Invalid list of project maintainers",
maintainersFile: []byte(`{"": ["user", 4]}`),
otherError: true,
},
{
name: "Invalid list of project maintainers",
maintainersFile: []byte(`{"": 4}`),
otherError: true,
},
}
for _, test := range packageTests {
t.Run(test.name, func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").
Return(test.maintainersFile, test.maintainersFileErr)
maintainers, err := FetchProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil && !test.otherError {
if test.maintainersFileErr == nil {
t.Fatal("Unexpected error recieved", err)
} else if err != test.maintainersFileErr {
t.Error("Wrong error recieved", err)
}
} else if test.maintainersFileErr != nil {
t.Fatal("Expected an error...")
} else if test.otherError && err == nil {
t.Fatal("Expected an error...")
}
var m []string
if len(test.packageName) > 0 {
m = maintainers.ListPackageMaintainers(test.packageName)
} else {
m = maintainers.ListProjectMaintainers()
}
if len(m) != len(test.maintainers) {
t.Error("Invalid number of maintainers for package", err)
}
for i := range m {
if !slices.Contains(test.maintainers, m[i]) {
t.Fatal("Can't find expected users. Found:", m)
}
}
})
}
}