package zitcli
import (
"context"
"strconv"
"testing"
)
// Each line is credited to the check-in that brought it, with a line
// that later commits left alone keeping its first author's check-in.
// A path that is not tracked answers not-ok, not an error.
func TestAnnotateCreditsEachLine(t *testing.T) {
repo := testRepo(t, 0)
commitFileZ(t, repo, "f.txt", "one\ntwo\n", "first")
commitFileZ(t, repo, "f.txt", "one\ntwo\nthree\n", "second")
z := New(repo)
lines, cut, ok, err := z.Annotate(context.Background(), "trunk", "f.txt", 0)
if err != nil || !ok || cut {
t.Fatalf("annotate: ok %v cut %v err %v", ok, cut, err)
}
if len(lines) != 3 {
t.Fatalf("FAIL: %d lines, want 3: %+v", len(lines), lines)
}
if lines[0].Hash != lines[1].Hash || lines[2].Hash == lines[0].Hash {
t.Errorf("FAIL: lines one and two share the first check-in and three the second: %+v", lines)
}
if lines[2].N != 3 || lines[2].Text != "three" || len(lines[0].Hash) < 40 {
t.Errorf("FAIL: line three reads %+v", lines[2])
}
if _, _, ok, err := z.Annotate(context.Background(), "trunk", "missing.txt", 0); ok || err != nil {
t.Errorf("FAIL: an untracked path answered ok %v, err %v", ok, err)
}
}
// Under a version limit, a line older than the versions walked comes
// back unattributed (zit 0.13.0 prints blank columns for it), and every
// line is still there; AnnotateAll walks the whole history.
func TestAnnotateUnderAVersionLimit(t *testing.T) {
repo := testRepo(t, 0)
body := "base\n"
commitFileZ(t, repo, "f.txt", body, "v1")
for i := 2; i <= 6; i++ {
// Distinct lines: which of several identical lines a version
// "added" is ambiguous, and not what this test is about.
body += "line " + strconv.Itoa(i) + "\n"
commitFileZ(t, repo, "f.txt", body, "v")
}
z := New(repo)
limited, _, ok, err := z.Annotate(context.Background(), "trunk", "f.txt", 3)
if err != nil || !ok || len(limited) != 6 {
t.Fatalf("FAIL: limited annotate: %d lines, ok %v, err %v", len(limited), ok, err)
}
if limited[0].Hash != "" || limited[0].Text != "base" || limited[0].N != 1 {
t.Errorf("FAIL: the oldest line under -n 3 reads %+v, want it unattributed", limited[0])
}
if limited[5].Hash == "" {
t.Error("FAIL: the newest line lost its check-in")
}
all, _, _, err := z.Annotate(context.Background(), "trunk", "f.txt", AnnotateAll)
if err != nil || len(all) != 6 || all[0].Hash == "" {
t.Errorf("FAIL: the whole history left a line unattributed: %+v (%v)", all, err)
}
t.Log("ok")
}