package zitcli
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// The merge base of two check-ins cannot change, so asking again for
// the same pair does not start zit again. A name can move, and no
// common ancestor may only mean a side has not arrived: neither is kept.
func TestMergeBaseIsAskedOncePerPairOfCheckins(t *testing.T) {
count := filepath.Join(t.TempDir(), "count")
fakeZit(t, `echo x >> `+count+`
case "$*" in *dead*) exit 1;; esac
echo `+strings.Repeat("c", 64))
spawned := func() int {
b, _ := os.ReadFile(count)
return strings.Count(string(b), "x")
}
c := New("r")
a, b := strings.Repeat("a", 64), strings.Repeat("b", 64)
for i := 0; i < 3; i++ {
got, err := c.MergeBase(context.Background(), a, b)
if err != nil || got != strings.Repeat("c", 64) {
t.Fatalf("FAIL: round %d: %q %v", i, got, err)
}
}
if n := spawned(); n != 1 {
t.Errorf("FAIL: zit started %d times for one pair of check-ins", n)
}
c.MergeBase(context.Background(), "trunk", b)
c.MergeBase(context.Background(), "trunk", b)
if n := spawned(); n != 3 {
t.Errorf("FAIL: a branch name was kept: zit started %d times in all, want 3", n)
}
missing := strings.Repeat("f", 60) + "dead"
for i := 0; i < 2; i++ {
if got, _ := c.MergeBase(context.Background(), a, missing); got != "" {
t.Errorf("FAIL: %q", got)
}
}
if n := spawned(); n != 5 {
t.Errorf("FAIL: an empty answer was kept: zit started %d times in all, want 5", n)
}
if !t.Failed() {
t.Log("ok")
}
}