Files
sync-branch/test/sync_branch_test.go
T
bot bae9657059
ci / test (push) Successful in 1m1s
初始版本:從 feather-vault 搬出
2026-11-05 02:54:23 +00:00

104 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// .gitea/actions/sync-branch/sync.sh 的黑箱測試:用暫時的 bare 儲存庫當 origin,檢查合併、重跑、目標分支不存在與衝突。
package test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func gitIn(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v:%v\n%s", args, err, out)
}
return strings.TrimSpace(string(out))
}
func commitFile(t *testing.T, dir, name, content string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
gitIn(t, dir, "add", ".")
gitIn(t, dir, "commit", "-qm", "c "+name)
gitIn(t, dir, "push", "-q", "origin", "HEAD")
}
// syncSetup 建出 origin 與工作目錄:main 有一個提交;withDev 時 dev 從它分出去並多一個提交。
func syncSetup(t *testing.T, withDev bool) (origin, work string) {
t.Helper()
root := t.TempDir()
origin, work = filepath.Join(root, "origin.git"), filepath.Join(root, "work")
gitIn(t, root, "init", "-q", "--bare", origin)
gitIn(t, root, "clone", "-q", origin, work)
gitIn(t, work, "checkout", "-q", "-b", "main")
commitFile(t, work, "a.txt", "1")
if withDev {
gitIn(t, work, "checkout", "-q", "-b", "dev")
commitFile(t, work, "dev.txt", "d")
gitIn(t, work, "checkout", "-q", "main")
}
return origin, work
}
func runSync(t *testing.T, work string) (string, error) {
t.Helper()
script, _ := filepath.Abs("../sync.sh")
cmd := exec.Command("bash", script)
cmd.Dir = work
cmd.Env = append(os.Environ(), "SYNC_SOURCE=main", "SYNC_TARGET=dev", "SYNC_SHA="+gitIn(t, work, "rev-parse", "HEAD"),
"GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t")
out, err := cmd.CombinedOutput()
return string(out), err
}
func TestSyncBranchMergesSourceIntoTargetOnce(t *testing.T) {
origin, work := syncSetup(t, true)
commitFile(t, work, "b.txt", "2") // main 往前一個提交
out, err := runSync(t, work)
if err != nil {
t.Fatalf("同步失敗:%v\n%s", err, out)
}
if got := gitIn(t, origin, "ls-tree", "--name-only", "dev"); !strings.Contains(got, "b.txt") || !strings.Contains(got, "dev.txt") {
t.Fatalf("dev 應該同時有 main 的 b.txt 與自己的 dev.txt:\n%s", got)
}
head := gitIn(t, origin, "rev-parse", "dev")
if out, err := runSync(t, work); err != nil || !strings.Contains(out, "不需要同步") {
t.Fatalf("第二次應該不需要同步:%v\n%s", err, out)
}
if gitIn(t, origin, "rev-parse", "dev") != head {
t.Fatal("已經包含時不該再多一個提交")
}
}
func TestSyncBranchDoesNothingWithoutTarget(t *testing.T) {
_, work := syncSetup(t, false)
out, err := runSync(t, work)
if err != nil || !strings.Contains(out, "不存在") {
t.Fatalf("目標分支不存在應該成功並說明:%v\n%s", err, out)
}
}
func TestSyncBranchFailsOnConflictWithoutPushing(t *testing.T) {
origin, work := syncSetup(t, true)
gitIn(t, work, "checkout", "-q", "dev")
commitFile(t, work, "a.txt", "dev 版")
gitIn(t, work, "checkout", "-q", "main")
commitFile(t, work, "a.txt", "main 版")
before := gitIn(t, origin, "rev-parse", "dev")
out, err := runSync(t, work)
if err == nil || !strings.Contains(out, "衝突") {
t.Fatalf("有衝突應該失敗並說明:%v\n%s", err, out)
}
if gitIn(t, origin, "rev-parse", "dev") != before {
t.Fatal("衝突時不該推送")
}
}