2024-12-02 10:26:51 +01:00
|
|
|
package main
|
|
|
|
|
2024-12-04 08:55:40 +01:00
|
|
|
import (
|
2024-12-17 23:33:43 +01:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2024-12-04 08:55:40 +01:00
|
|
|
"testing"
|
2024-12-17 23:33:43 +01:00
|
|
|
|
2024-12-04 08:55:40 +01:00
|
|
|
"go.uber.org/mock/gomock"
|
2024-12-17 23:33:43 +01:00
|
|
|
"src.opensuse.org/autogits/common"
|
2024-12-04 08:55:40 +01:00
|
|
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
2024-12-17 23:33:43 +01:00
|
|
|
mock_common "src.opensuse.org/autogits/common/mock"
|
2024-12-04 08:55:40 +01:00
|
|
|
)
|
2024-12-02 10:26:51 +01:00
|
|
|
|
|
|
|
func TestPR(t *testing.T) {
|
2024-12-17 23:33:43 +01:00
|
|
|
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",
|
2024-12-18 17:30:00 +01:00
|
|
|
Organization: "foo",
|
2024-12-17 23:33:43 +01:00
|
|
|
GitProjectName: "barPrj",
|
|
|
|
}
|
|
|
|
|
|
|
|
type prdata struct {
|
|
|
|
pr *models.PullRequest
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
t.Run("TestPR", func(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
data []prdata
|
|
|
|
api_error string
|
|
|
|
|
|
|
|
resLen int
|
|
|
|
reviewed bool
|
|
|
|
consistentSet bool
|
|
|
|
prjGitPRIndex int
|
|
|
|
|
|
|
|
customMockSetup func(*mock_common.MockGiteaPRFetcher) error
|
|
|
|
reviewSetFetcher func(*mock_common.MockGiteaPRFetcher) (*ReviewSet, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
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")},
|
|
|
|
},
|
2024-12-17 23:33:43 +01:00
|
|
|
},
|
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
{
|
|
|
|
name: "Error fetching PullRequest in PrjGit",
|
|
|
|
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"}}}}, err: errors.New("Missing PR")},
|
|
|
|
},
|
2024-12-18 17:30:00 +01:00
|
|
|
},
|
2025-01-02 13:46:59 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
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,
|
2024-12-17 23:33:43 +01:00
|
|
|
},
|
2025-01-02 13:46:59 +01:00
|
|
|
{
|
|
|
|
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,
|
2024-12-17 23:33:43 +01:00
|
|
|
},
|
2025-01-02 13:46:59 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
consistentSet: true,
|
2024-12-17 23:33:43 +01:00
|
|
|
},
|
2025-01-02 13:46:59 +01:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
consistentSet: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Review set of prjgit PR is consistent",
|
|
|
|
data: []prdata{
|
|
|
|
{pr: &models.PullRequest{Body: "", Index: 42, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "barPrj", Owner: &models.User{UserName: "foo"}}}}},
|
|
|
|
},
|
|
|
|
resLen: 1,
|
|
|
|
prjGitPRIndex: 0,
|
|
|
|
consistentSet: true,
|
|
|
|
reviewSetFetcher: func(mock *mock_common.MockGiteaPRFetcher) (*ReviewSet, error) {
|
|
|
|
return FetchReviewSet(mock, "foo", "barPrj", 42, &baseConfig)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Review set is inconsistent: 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/repo2#41", Index: 20, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "barPrj", Owner: &models.User{UserName: "foo"}}}}},
|
|
|
|
{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#20", Index: 41, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo2", Owner: &models.User{UserName: "test"}}}}},
|
|
|
|
},
|
|
|
|
resLen: 4,
|
|
|
|
prjGitPRIndex: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Review set is inconsistent: not pointing at PrjGit",
|
|
|
|
data: []prdata{
|
|
|
|
{pr: &models.PullRequest{Body: "PR: test/repo2#41", 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: test/repo#42", Index: 41, Base: &models.PRBranchInfo{Repo: &models.Repository{Name: "repo2", Owner: &models.User{UserName: "test"}}}}},
|
|
|
|
},
|
|
|
|
resLen: 2,
|
|
|
|
prjGitPRIndex: -1,
|
|
|
|
},
|
|
|
|
}
|
2024-12-17 23:33:43 +01:00
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
ctl := gomock.NewController(t)
|
|
|
|
mock := mock_common.NewMockGiteaPRFetcher(ctl)
|
2024-12-17 23:33:43 +01:00
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
var test_err error
|
|
|
|
if test.customMockSetup != nil {
|
|
|
|
if err := test.customMockSetup(mock); err != nil {
|
|
|
|
test_err = err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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).AnyTimes()
|
|
|
|
if data.err != nil {
|
|
|
|
test_err = data.err
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 23:33:43 +01:00
|
|
|
}
|
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
var res *ReviewSet
|
|
|
|
var err error
|
|
|
|
if test.reviewSetFetcher != nil {
|
|
|
|
res, err = test.reviewSetFetcher(mock)
|
|
|
|
} else {
|
|
|
|
res, err = FetchReviewSet(mock, "test", "repo", 42, &baseConfig)
|
2024-12-18 17:30:00 +01:00
|
|
|
}
|
2025-01-02 13:46:59 +01:00
|
|
|
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?")
|
|
|
|
}
|
2024-12-18 17:30:00 +01:00
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
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)
|
2024-12-17 23:33:43 +01:00
|
|
|
}
|
2025-01-02 13:46:59 +01:00
|
|
|
return
|
2024-12-17 23:33:43 +01:00
|
|
|
}
|
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
t.Log(res)
|
2024-12-18 17:30:00 +01:00
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
if test.resLen != len(res.prs) {
|
|
|
|
t.Error("expected result len", test.resLen, "but got", len(res.prs))
|
2024-12-18 17:30:00 +01:00
|
|
|
}
|
2025-01-02 13:46:59 +01:00
|
|
|
|
|
|
|
PrjGitPR, err := res.GetPrjGitPR()
|
|
|
|
if test.prjGitPRIndex < 0 {
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error, but nothing")
|
2024-12-18 17:30:00 +01:00
|
|
|
}
|
|
|
|
}
|
2025-01-02 13:46:59 +01:00
|
|
|
pr_found := false
|
|
|
|
if test.prjGitPRIndex >= 0 {
|
|
|
|
for i := range test.data {
|
|
|
|
if PrjGitPR == 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 PR set", PrjGitPR)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if PrjGitPR != nil {
|
|
|
|
t.Log("Expected prjgit not found, but found?", PrjGitPR)
|
|
|
|
}
|
2024-12-18 17:30:00 +01:00
|
|
|
}
|
|
|
|
|
2025-01-02 13:46:59 +01:00
|
|
|
if res.IsConsistent() != test.consistentSet {
|
|
|
|
t.Error("IsConsistent() returned unexpected:", test.consistentSet)
|
2024-12-18 17:30:00 +01:00
|
|
|
}
|
2025-01-02 13:46:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
if res.IsReviewed() != test.reviewed {
|
|
|
|
t.Error("expected reviewed to be NOT", res.IsReviewed())
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2024-12-17 23:33:43 +01:00
|
|
|
}
|