autogits/workflow-pr/main_test.go
2024-11-08 16:36:05 +01:00

198 lines
5.7 KiB
Go

package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"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) {
req := common.PullRequestWebhookEvent{
Repository: &common.Repository{
Name: "testingRepo",
},
Pull_Request: &common.PullRequest{
Number: 10,
},
}
branchName := prGitBranchNameForPR(&req)
if branchName != "PR_testingRepo#10" {
t.Error("Unexpected branch name:", branchName)
}
}
func TestProjctGitSync(t *testing.T) {
req := common.PullRequestWebhookEvent{
Action: "pull",
Number: 0,
}
if err := processPrjGitPullRequestSync(&req); err != nil {
t.Error(err)
}
}
const LocalCMD = "---"
func gitExecs(t *testing.T, git *common.GitHandler, cmds [][]string) {
for _, cmd := range cmds {
if cmd[0] == LocalCMD {
command := exec.Command(cmd[2], cmd[3:]...)
command.Dir = filepath.Join(git.GitPath, cmd[1])
command.Stdin = nil
command.Env = []string{"GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_1=protocol.file.allow", "GIT_CONFIG_VALUE_1=always"}
_, err := command.CombinedOutput()
if err != nil {
t.Errorf(" *** error: %v\n", err)
}
} else {
git.GitExecOrPanic(cmd[0], cmd[1:]...)
}
}
}
func commandsForPackages(dir, prefix string, startN, endN int) [][]string {
commands := make([][]string, (endN - startN + 2) * 6)
if dir == "" {
dir = "."
}
cmdIdx := 0
for idx := startN; idx <= endN; idx++ {
pkgDir := fmt.Sprintf("%s%d", prefix, idx)
commands[cmdIdx+0] = []string{"", "init", "-q", "--object-format", "sha256", "-b", "testing", pkgDir}
commands[cmdIdx+1] = []string{LocalCMD, pkgDir, "/usr/bin/touch", "testFile"}
commands[cmdIdx+2] = []string{pkgDir, "add", "testFile"}
commands[cmdIdx+3] = []string{pkgDir, "commit", "-m", "added testFile"}
commands[cmdIdx+4] = []string{pkgDir, "config", "receive.denyCurrentBranch", "ignore"}
commands[cmdIdx+5] = []string{"prj", "submodule", "add", filepath.Join("..", pkgDir), filepath.Join(dir, pkgDir)}
cmdIdx += 6
}
// add all the submodules to the prj
commands[cmdIdx+0] = []string{"prj", "commit", "-a", "-m", "adding subpackages"}
return commands
}
func setupGitForTests(t *testing.T, git *common.GitHandler) {
common.ExtraGitParams = []string{
"GIT_CONFIG_COUNT=1",
"GIT_CONFIG_KEY_0=protocol.file.allow",
"GIT_CONFIG_VALUE_0=always",
}
gitExecs(t, git, [][]string{
{"", "init", "-q", "--object-format", "sha256", "-b", "testing", "prj"},
{"", "init", "-q", "--object-format", "sha256", "-b", "testing", "foo"},
{LocalCMD, "foo", "/usr/bin/touch", "file1"},
{"foo", "add", "file1"},
{"foo", "commit", "-m", "first commit"},
{"prj", "config", "receive.denyCurrentBranch", "ignore"},
{"prj", "submodule", "init"},
{"prj", "submodule", "add", filepath.Join(git.GitPath, "foo"), "testRepo"},
{"prj", "add", ".gitmodules", "testRepo"},
{"prj", "commit", "-m", "First instance"},
{"prj", "submodule", "deinit", "testRepo"},
{"", "clone", "prj", common.DefaultGitPrj},
{LocalCMD, "foo", "/usr/bin/touch", "file2"},
{"foo", "add", "file2"},
{"foo", "commit", "-m", "added file2"},
})
}
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.GitHandler{
DebugLogger: true,
GitCommiter: "TestCommiter",
GitEmail: "test@testing",
GitPath: t.TempDir(),
}
setupGitForTests(t, git)
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])
if err := updateOrCreatePRBranch(req, git, "created commit", "testing"); err != nil {
t.Error(err)
}
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.GitHandler{
DebugLogger: true,
GitCommiter: "TestCommiter",
GitEmail: "test@testing",
GitPath: t.TempDir(),
}
setupGitForTests(t, git)
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])
if err := updateOrCreatePRBranch(req, git, "created commit", "testingCreated"); err != nil {
t.Error(err)
}
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())
}
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())
// os.CopyFS("/tmp/test", os.DirFS(git.GitPath))
}
}