From 2916ec8da5ea137a55266855529680e8da194f634cd2aae7017bafccfb4fb9fb Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Wed, 27 Nov 2024 17:50:55 +0100 Subject: [PATCH] workflow-pr: tests --- workflow-pr/maintainership.go | 26 +++++++++ workflow-pr/maintainership_test.go | 89 ++++++++++++++++++++++++++++++ workflow-pr/repo_check_test.go | 1 - 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 workflow-pr/maintainership.go create mode 100644 workflow-pr/maintainership_test.go diff --git a/workflow-pr/maintainership.go b/workflow-pr/maintainership.go new file mode 100644 index 0000000..ec429a0 --- /dev/null +++ b/workflow-pr/maintainership.go @@ -0,0 +1,26 @@ +package main + +import "src.opensuse.org/autogits/common" + +//go:generate mockgen -source=maintainership.go -destination=mock/maintainership.go -typed + +type GiteaMaintainershipInterface interface { + FetchMaintainershipFile(org, prj, branch string) ([]byte, error) +} + +func MaintainerListForProject(gitea GiteaMaintainershipInterface, org, branch string) ([]string, error) { + _, err := gitea.FetchMaintainershipFile(org, common.DefaultGitPrj, branch) + if err != nil { + return nil, err + } + + return nil, nil +} + +func MaintainerListForPackage(gitea GiteaMaintainershipInterface, org, pkg, branch string) ([]string, error) { + _, err := gitea.FetchMaintainershipFile(org, pkg, branch) + if err != nil { + return nil, err + } + return []string{}, nil +} diff --git a/workflow-pr/maintainership_test.go b/workflow-pr/maintainership_test.go new file mode 100644 index 0000000..6dfd788 --- /dev/null +++ b/workflow-pr/maintainership_test.go @@ -0,0 +1,89 @@ +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) + } + }) +} diff --git a/workflow-pr/repo_check_test.go b/workflow-pr/repo_check_test.go index 1497863..ecfe335 100644 --- a/workflow-pr/repo_check_test.go +++ b/workflow-pr/repo_check_test.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "log" - "os" "testing" "go.uber.org/mock/gomock"