workflow-pr: tests

This commit is contained in:
Adam Majer 2024-11-26 17:21:17 +01:00
parent e56f444960
commit f281986c8f

View File

@ -8,6 +8,7 @@ import (
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
mock_common "src.opensuse.org/autogits/common/mock"
mock_main "src.opensuse.org/workflow-pr/mock"
)
@ -133,5 +134,111 @@ func TestRepoCheck(t *testing.T) {
t.Error(err)
}
})
}
type testGit struct {
git *common.GitHandler
}
func (s *testGit) CreateGitHandler(a, b, c string) (*common.GitHandler, error) {
return s.git, nil
}
func TestVerifyProjectState(t *testing.T) {
var logBuf bytes.Buffer
oldOut := log.Writer()
log.SetOutput(&logBuf)
defer log.SetOutput(oldOut)
t.Run("Project state with no PRs", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
git := &common.GitHandler{
DebugLogger: true,
GitCommiter: "TestCommiter",
GitEmail: "test@testing",
GitPath: t.TempDir(),
}
setupGitForTests(t, git)
org := "repo1_org"
config1 := &common.AutogitConfig{
GitProjectName: "git_repo1",
Organization: "repo1_org",
Branch: "testing",
Reviewers: []string{"reviewer1", "reviewer2"},
Workflows: []string{"pr"},
}
configs := &RequestProcessor{
configuredRepos: map[string][]*common.AutogitConfig{
org: []*common.AutogitConfig{config1},
},
}
gitea.EXPECT().CreateRepositoryIfNotExist(gomock.Any(), gomock.Any(), config1.GitProjectName).Return(&models.Repository{
SSHURL: "./prj",
}, nil)
gitea.EXPECT().GetRecentPullRequests(org, "testRepo")
gitea.EXPECT().GetRecentCommits(org, "testRepo", "testing", gomock.Any())
c := CreateDefaultStateChecker(false, configs, gitea, 0)
c.git = &testGit{
git: git,
}
err := c.VerifyProjectState("repo1_org", configs.configuredRepos[org], 0)
if err != nil {
t.Error(err)
}
})
t.Run("Project state with 1 PRs", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
git := &common.GitHandler{
DebugLogger: true,
GitCommiter: "TestCommiter",
GitEmail: "test@testing",
GitPath: t.TempDir(),
}
setupGitForTests(t, git)
org := "repo1_org"
config1 := &common.AutogitConfig{
GitProjectName: "git_repo1",
Organization: "repo1_org",
Branch: "testing",
Reviewers: []string{"reviewer1", "reviewer2"},
Workflows: []string{"pr"},
}
configs := &RequestProcessor{
configuredRepos: map[string][]*common.AutogitConfig{
org: []*common.AutogitConfig{config1},
},
}
gitea.EXPECT().CreateRepositoryIfNotExist(gomock.Any(), gomock.Any(), config1.GitProjectName).Return(&models.Repository{
SSHURL: "./prj",
}, nil)
gitea.EXPECT().GetRecentPullRequests(org, "testRepo").Return([]*models.PullRequest{
&models.PullRequest{
}}, nil)
gitea.EXPECT().GetRecentCommits(org, "testRepo", "testing", gomock.Any())
c := CreateDefaultStateChecker(false, configs, gitea, 0)
c.git = &testGit{
git: git,
}
err := c.VerifyProjectState("repo1_org", configs.configuredRepos[org], 0)
if err != nil {
t.Error(err)
}
})
}