autogits/workflow-pr/pr_test.go
2024-12-18 17:30:00 +01:00

162 lines
4.6 KiB
Go

package main
import (
"errors"
"os"
"os/exec"
"path"
"testing"
"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"
)
func TestPR(t *testing.T) {
cwd, _ := os.Getwd()
cmd := exec.Command("/usr/bin/bash", path.Join(cwd, "test_repo_setup.sh"))
cmd.Dir = t.TempDir()
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatal(string(out))
}
baseConfig := common.AutogitConfig{
Reviewers: []string{"super1", "super2"},
Branch: "branch",
Organization: "foo",
GitProjectName: "barPrj",
}
type prdata struct {
pr *models.PullRequest
err error
}
tests := []struct {
name string
data []prdata
api_error string
resLen int
reviewed bool
prjGitPRIndex int
}{
{
name: "Error fetching PullRequest",
data: []prdata{
{pr: &models.PullRequest{Body: "", Index: 42, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo", Owner: &models.User{UserName: "test"}}}}, err: errors.New("Missing PR")},
},
},
{
name: "Error fetching is missing links, fixable",
data: []prdata{
{pr: &models.PullRequest{Body: "", Index: 42, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo", Owner: &models.User{UserName: "test"}}}}},
},
resLen: 1,
prjGitPRIndex: -1,
},
{
name: "Review set is not consistent, but fixable",
data: []prdata{
{pr: &models.PullRequest{Body: "PR: foo/barPrj#22", Index: 42, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo", Owner: &models.User{UserName: "test"}}}}},
{pr: &models.PullRequest{Body: "", Index: 22, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "barPrj", Owner: &models.User{UserName: "foo"}}}}},
},
resLen: 2,
prjGitPRIndex: 1,
},
{
name: "Review set is consistent: 1pkg",
data: []prdata{
{pr: &models.PullRequest{Body: "PR: foo/barPrj#22", Index: 42, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo", Owner: &models.User{UserName: "test"}}}}},
{pr: &models.PullRequest{Body: "PR: test/repo#42", Index: 22, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "barPrj", Owner: &models.User{UserName: "foo"}}}}},
},
resLen: 2,
prjGitPRIndex: 1,
},
{
name: "Review set is consistent: 2pkg",
data: []prdata{
{pr: &models.PullRequest{Body: "PR: foo/barPrj#22", Index: 42, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo", Owner: &models.User{UserName: "test"}}}}},
{pr: &models.PullRequest{Body: "PR: test/repo#42\nPR: test/repo2#41", Index: 22, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "barPrj", Owner: &models.User{UserName: "foo"}}}}},
{pr: &models.PullRequest{Body: "PR: foo/barPrj#22", Index: 41, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo2", Owner: &models.User{UserName: "test"}}}}},
},
resLen: 3,
prjGitPRIndex: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctl := gomock.NewController(t)
mock := mock_common.NewMockGiteaPRFetcher(ctl)
var test_err error
for _, data := range test.data {
mock.EXPECT().GetPullRequest(data.pr.Base.Repo.Owner.UserName, data.pr.Base.Repo.Name, data.pr.Index).Return(data.pr, data.err)
if data.err != nil {
test_err = data.err
}
}
res, err := FetchReviewSet(mock, "test", "repo", 42, &baseConfig)
if err == nil {
if test_err != nil {
t.Fatal("Expected", test_err, "but got", err)
}
} else {
if res != nil {
t.Fatal("error but got ReviewSet?")
}
if test.api_error != "" {
if err.Error() != test.api_error {
t.Fatal("expected", test.api_error, "but got", err)
}
} else if test_err != err {
t.Fatal("expected", test_err, "but got", err)
}
return
}
t.Log(res)
if test.resLen != len(res.prs) {
t.Error("expected result len", test.resLen, "but got", len(res.prs))
}
pr, err := res.GetPrjGitPR()
if test.prjGitPRIndex < 0 {
if err == nil {
t.Error("expected error, but nothing")
}
}
pr_found := false
if test.prjGitPRIndex >= 0 {
for i := range test.data {
if pr == test.data[i].pr && i == test.prjGitPRIndex {
t.Log("found at index", i)
pr_found = true
}
}
if !pr_found {
t.Error("Cannot find expected PrjGit location in PrjGit set", pr)
}
} else {
if pr != nil {
t.Log("Expected prjgit not found, but found?", pr)
}
}
/*
if res.IsReviewed() != test.reviewed {
t.Error("expected reviewed to be NOT", res.IsReviewed())
}
*/
})
}
}