autogits/workflow-pr/maintainership_test.go
2024-11-27 17:50:55 +01:00

90 lines
2.5 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) {
MaintainershipFile := []byte(`
{
"*": ["user1", "user2"]
}
`)
t.Run("No maintainer in empty package", func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
mi.EXPECT().FetchMaintainershipFile("foo", "goo", "bar").Return(nil, nil)
m, err := MaintainerListForPackage(mi, "foo", "goo", "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) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
m, err := MaintainerListForProject(mi, "foo", "bar")
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) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", "goo", "bar").Return(nil, err)
_, errRet := MaintainerListForPackage(mi, "foo", "goo", "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) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err)
_, errRet := MaintainerListForProject(mi, "foo", "bar")
switch errRet {
case nil:
t.Error("Should have returned an error")
case err:
break
default:
t.Error("Unexpected error received", err)
}
})
t.Run("Project maintainers", func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(MaintainershipFile, nil)
m, err := MaintainerListForProject(mi, "foo", "bar")
if len(m) != 2 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
})
}