package zitcli
import (
"context"
"os"
"os/exec"
"path/filepath"
"regexp"
"testing"
)
// zit reports a conflicting merge region by region, and the report is
// the answer: exit 5 is a conflict, not a failure. A clean pair reports
// no files.
func TestMergeRegionsReportsAConflict(t *testing.T) {
if _, err := exec.LookPath("zit"); err != nil {
t.Skip("zit not on PATH")
}
dir := t.TempDir()
run := func(args ...string) string {
cmd := exec.Command("zit", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("zit %v: %v\n%s", args, err, out)
}
return string(out)
}
write := func(body string) {
if err := os.WriteFile(filepath.Join(dir, "f.txt"), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
tip := func(branch string) string {
h := regexp.MustCompile(`[0-9a-f]{64}`).FindString(run("branch", "info", branch))
if h == "" {
t.Fatalf("no tip for %s", branch)
}
return h
}
run("init", ".")
write("a\nb\nc\nd\ne\nf\ng\n")
run("add", "f.txt")
run("commit", "-m", "base", "--user", "u")
base := tip("trunk")
run("branch", "new", "feat")
write("a\nB-feat\nc\nd\ne\nf\ng\nadded-by-feat\n")
run("commit", "-m", "feat", "--user", "u")
run("update", "trunk")
write("A-trunk\nb-trunk\nc\nd\ne\nF-trunk\ng\n")
run("commit", "-m", "trunk", "--user", "u")
c := New(dir)
rep, err := c.MergeRegions(context.Background(), tip("feat"), tip("trunk"))
if err != nil {
t.Fatal(err)
}
if rep.Verdict != "conflict" || len(rep.Files) != 1 || rep.Files[0].Path != "f.txt" {
t.Fatalf("FAIL: report %+v", rep)
}
kinds := ""
for _, r := range rep.Files[0].Regions {
kinds += r.Kind + " "
}
if kinds != "conflict same current same other " {
t.Errorf("FAIL: regions %q", kinds)
}
if f := rep.Files[0]; f.Base == "" || f.Current == "" || f.Other == "" {
t.Errorf("FAIL: a side has no blob: %+v", f)
}
clean, err := c.MergeRegions(context.Background(), base, tip("trunk"))
if err != nil || len(clean.Files) != 0 {
t.Errorf("FAIL: a clean pair reported %+v (%v)", clean, err)
}
}