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"] } `) SinglePrjMaintainershipFile := []byte(` { "*": "user" } `) BrokenPrjMaintainershipFile := []byte(` { "*": ["user", 4] } `) SingleBrokenPrjMaintainershipFile := []byte(` { "*": 4 } `) 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) } mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte("{}"), 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("Multiple 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) } 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) { ctl := gomock.NewController(t) mi := mock_main.NewMockGiteaMaintainershipInterface(ctl) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(SinglePrjMaintainershipFile, nil) m, err := MaintainerListForProject(mi, "foo", "bar") 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) { ctl := gomock.NewController(t) mi := mock_main.NewMockGiteaMaintainershipInterface(ctl) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(BrokenPrjMaintainershipFile, nil) m, err := MaintainerListForProject(mi, "foo", "bar") if len(m) != 0 || err.Error() != "Invalid type" { t.Error("Invalid number of maintainers for project", err) } }) t.Run("Invalid list of project maintainers", func(t *testing.T) { ctl := gomock.NewController(t) mi := mock_main.NewMockGiteaMaintainershipInterface(ctl) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(SingleBrokenPrjMaintainershipFile, nil) _, err := MaintainerListForProject(mi, "foo", "bar") if err.Error() != "Invalid type" { t.Error("Invalid number of maintainers for project", err) } }) }