104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
// "go.uber.org/mock/gomock"
|
|
"src.opensuse.org/autogits/common"
|
|
// "src.opensuse.org/autogits/common/mock"
|
|
)
|
|
|
|
func TestProjectBranchName(t *testing.T) {
|
|
branchName := prGitBranchNameForPR("testingRepo", 10)
|
|
if branchName != "PR_testingRepo#10" {
|
|
t.Error("Unexpected branch name:", branchName)
|
|
}
|
|
}
|
|
|
|
func TestUpdatePrBranch(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
origLogger := log.Writer()
|
|
log.SetOutput(&buf)
|
|
defer log.SetOutput(origLogger)
|
|
|
|
req := &common.PullRequestWebhookEvent{
|
|
Repository: &common.Repository{
|
|
Name: "testRepo",
|
|
},
|
|
Pull_Request: &common.PullRequest{},
|
|
}
|
|
|
|
git := &common.GitHandlerImpl{
|
|
GitCommiter: "TestCommiter",
|
|
GitEmail: "test@testing",
|
|
GitPath: t.TempDir(),
|
|
}
|
|
|
|
setupGitForTests(t, git)
|
|
gitExecs(t, git, [][]string{{"", "clone", "prj", common.DefaultGitPrj}})
|
|
|
|
revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n")
|
|
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
|
|
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
|
|
|
|
updateSubmoduleInPR("testRepo", revs[0], git)
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "created commit"))
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:+testing"))
|
|
git.GitExecOrPanic("prj", "reset", "--hard", "testing")
|
|
rev := strings.TrimSpace(git.GitExecWithOutputOrPanic(filepath.Join(common.DefaultGitPrj, "testRepo"), "rev-list", "-1", "HEAD"))
|
|
if rev != req.Pull_Request.Head.Sha {
|
|
t.Error("prj/testRepo not updated to", req.Pull_Request.Head.Sha, "but is at", rev)
|
|
t.Error(buf.String())
|
|
}
|
|
}
|
|
|
|
func TestCreatePrBranch(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
origLogger := log.Writer()
|
|
log.SetOutput(&buf)
|
|
defer log.SetOutput(origLogger)
|
|
|
|
req := &common.PullRequestWebhookEvent{
|
|
Repository: &common.Repository{
|
|
Name: "testRepo",
|
|
},
|
|
Pull_Request: &common.PullRequest{},
|
|
}
|
|
|
|
git := &common.GitHandlerImpl{
|
|
GitCommiter: "TestCommiter",
|
|
GitEmail: "test@testing",
|
|
GitPath: t.TempDir(),
|
|
}
|
|
|
|
setupGitForTests(t, git)
|
|
gitExecs(t, git, [][]string{{"", "clone", "prj", common.DefaultGitPrj}})
|
|
|
|
revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n")
|
|
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
|
|
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
|
|
|
|
updateSubmoduleInPR("testRepo", revs[0], git)
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "created commit"))
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:testingCreated"))
|
|
|
|
rev := strings.TrimSpace(git.GitExecWithOutputOrPanic(filepath.Join(common.DefaultGitPrj, "testRepo"), "rev-list", "-1", "HEAD"))
|
|
if rev != req.Pull_Request.Head.Sha {
|
|
t.Error("prj/testRepo not updated to", req.Pull_Request.Head.Sha, "but is at", rev)
|
|
t.Error(buf.String())
|
|
}
|
|
|
|
os.CopyFS("/tmp/test", os.DirFS(git.GitPath))
|
|
git.GitExecOrPanic("prj", "reset", "--hard", "testingCreated")
|
|
rev = strings.TrimSpace(git.GitExecWithOutputOrPanic("prj", "submodule", "status", "testRepo"))[1 : len(req.Pull_Request.Head.Sha)+1]
|
|
if rev != req.Pull_Request.Head.Sha {
|
|
t.Error("prj/testRepo not updated to", req.Pull_Request.Head.Sha, "but is at", rev)
|
|
t.Error(buf.String())
|
|
}
|
|
}
|