231 lines
6.9 KiB
Go
231 lines
6.9 KiB
Go
package common_test
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
|
|
"go.uber.org/mock/gomock"
|
|
"src.opensuse.org/autogits/common"
|
|
"src.opensuse.org/autogits/common/gitea-generated/client/repository"
|
|
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
|
|
maintainers []string
|
|
otherError bool
|
|
packageName string
|
|
|
|
maintainersFile []byte
|
|
maintainersFileErr error
|
|
|
|
maintainersDir map[string][]byte
|
|
}{
|
|
/* 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"), // repository.NewRepoGetRawFileNotFound(),
|
|
packageName: "foo",
|
|
},
|
|
{
|
|
name: "Multiple package maintainers",
|
|
maintainersFile: []byte(`{"pkg": ["user1", "user2"], "": ["user1", "user3"]}`),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{"": ["user1", "user3"]}`),
|
|
"pkg": []byte(`{"pkg": ["user1", "user2"]}`),
|
|
},
|
|
maintainers: []string{"user1", "user2", "user3"},
|
|
packageName: "pkg",
|
|
},
|
|
{
|
|
name: "No package maintainers and only project maintainer",
|
|
maintainersFile: []byte(`{"pkg2": ["user1", "user2"], "": ["user1", "user3"]}`),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{"": ["user1", "user3"]}`),
|
|
},
|
|
maintainers: []string{"user1", "user3"},
|
|
packageName: "pkg",
|
|
},
|
|
{
|
|
name: "Invalid list of package maintainers",
|
|
maintainersFile: []byte(`{"pkg": 3,"": ["user", 4]}`),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{"": ["user1", 4]}`),
|
|
"pkg": []byte(`"pkg": 3`),
|
|
},
|
|
otherError: true,
|
|
packageName: "pkg",
|
|
},
|
|
|
|
/* PROJECT MAINTAINERS */
|
|
{
|
|
name: "No maintainer for empty project",
|
|
},
|
|
{
|
|
name: "No maintainer for empty project maintainer file",
|
|
maintainersFile: []byte("{}"),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{}`),
|
|
},
|
|
},
|
|
{
|
|
name: "Error in MaintainerListForProject when remote has an error",
|
|
maintainersFileErr: errors.New("some error"), //repository.NewRepoGetRawFileNotFound(),
|
|
},
|
|
{
|
|
name: "Multiple project maintainers",
|
|
maintainersFile: []byte(`{"": ["user1", "user2"]}`),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{"": ["user1", "user2"]}`),
|
|
},
|
|
maintainers: []string{"user1", "user2"},
|
|
},
|
|
{
|
|
name: "Single project maintainer",
|
|
maintainersFile: []byte(`{"": ["user"]}`),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{"": ["user"]}`),
|
|
},
|
|
maintainers: []string{"user"},
|
|
},
|
|
{
|
|
name: "Invalid list of project maintainers",
|
|
maintainersFile: []byte(`{"": ["user", 4]}`),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{"": ["user", 4]}`),
|
|
},
|
|
otherError: true,
|
|
},
|
|
{
|
|
name: "Invalid list of project maintainers",
|
|
maintainersFile: []byte(`{"": 4}`),
|
|
maintainersDir: map[string][]byte{
|
|
"_project": []byte(`{"": 4}`),
|
|
},
|
|
otherError: true,
|
|
},
|
|
}
|
|
|
|
notFoundError := repository.NewRepoGetRawFileNotFound()
|
|
for _, test := range packageTests {
|
|
runTests := func(t *testing.T, mi common.GiteaMaintainershipReader) {
|
|
maintainers, err := common.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", test.packageName, len(m), "vs", len(test.maintainers))
|
|
}
|
|
for i := range m {
|
|
if !slices.Contains(test.maintainers, m[i]) {
|
|
t.Fatal("Can't find expected users. Found:", m)
|
|
}
|
|
}
|
|
}
|
|
|
|
t.Run(test.name+"_File", func(t *testing.T) {
|
|
ctl := gomock.NewController(t)
|
|
mi := mock_common.NewMockGiteaMaintainershipReader(ctl)
|
|
|
|
// tests with maintainership file
|
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").
|
|
Return(test.maintainersFile, test.maintainersFileErr)
|
|
mi.EXPECT().FetchMaintainershipDirFile("foo", common.DefaultGitPrj, "bar", common.ProjectFileKey).
|
|
Return(nil, notFoundError)
|
|
|
|
runTests(t, mi)
|
|
})
|
|
|
|
t.Run(test.name+"_Dir", func(t *testing.T) {
|
|
ctl := gomock.NewController(t)
|
|
mi := mock_common.NewMockGiteaMaintainershipReader(ctl)
|
|
|
|
// run same tests with directory maintainership data
|
|
for filename, data := range test.maintainersDir {
|
|
mi.EXPECT().FetchMaintainershipDirFile("foo", common.DefaultGitPrj, "bar", filename).Return(data, test.maintainersFileErr).AnyTimes()
|
|
}
|
|
if _, found := test.maintainersDir[common.ProjectFileKey]; !found {
|
|
mi.EXPECT().FetchMaintainershipDirFile("foo", common.DefaultGitPrj, "bar", common.ProjectFileKey).Return(nil, test.maintainersFileErr).AnyTimes()
|
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, test.maintainersFileErr).AnyTimes()
|
|
}
|
|
mi.EXPECT().FetchMaintainershipDirFile("foo", common.DefaultGitPrj, "bar", gomock.Any()).Return(nil, notFoundError).AnyTimes()
|
|
|
|
runTests(t, mi)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMaintainershipFileWrite(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
is_dir bool
|
|
maintainers map[string][]string
|
|
expected_output string
|
|
expected_error error
|
|
}{
|
|
{
|
|
name: "empty dataset",
|
|
expected_output: "{\n}\n",
|
|
},
|
|
{
|
|
name: "2 project maintainers and 2 single package maintainers",
|
|
maintainers: map[string][]string{
|
|
"": {"two", "one"},
|
|
"pkg1": {"three"},
|
|
"foo": {"four", "byte"},
|
|
},
|
|
expected_output: "{\n \"\": [\"one\",\"two\"]\n \"foo\": [\"byte\",\"four\"]\n \"pkg1\": [\"three\"]\n}\n",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
b := bytes.Buffer{}
|
|
data := common.MaintainershipMap{
|
|
Data: test.maintainers,
|
|
IsDir: test.is_dir,
|
|
}
|
|
|
|
if err := data.WriteMaintainershipFile(&b); err != test.expected_error {
|
|
t.Fatal("unexpected error:", err, "Expecting:", test.expected_error)
|
|
}
|
|
|
|
output := b.String()
|
|
|
|
if test.expected_output != output {
|
|
t.Fatal("unexpected output:", output, "Expecting:", test.expected_output)
|
|
}
|
|
})
|
|
}
|
|
}
|