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" "go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
mock_common "src.opensuse.org/autogits/common/mock" mock_common "src.opensuse.org/autogits/common/mock"
mock_main "src.opensuse.org/workflow-pr/mock" mock_main "src.opensuse.org/workflow-pr/mock"
) )
@ -126,12 +127,118 @@ func TestRepoCheck(t *testing.T) {
err := errors.New("test error") err := errors.New("test error")
state.EXPECT().VerifyProjectState("repo1_org", gomock.Any(), 0).Return(err) state.EXPECT().VerifyProjectState("repo1_org", gomock.Any(), 0).Return(err)
r := c.CheckRepos() r := c.CheckRepos()
if !errors.Is(r, err) { if !errors.Is(r, err) {
t.Error(err) 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)
}
})
} }