package gitcmd import ( "context" "testing" ) func TestGetContributors(t *testing.T) { f := newFixture(t) cs, err := f.repo.GetContributors("HEAD") if err != nil { t.Fatal(err) } if len(cs) != 2 { t.Fatalf("len = %d, want 2", len(cs)) } alice := cs[0] if alice.Author != aliceName { t.Errorf("cs[0].Author = %q, want Alice", alice.Author) } if alice.Commits != 3 || alice.Additions != 3 || alice.Deletions != 1 || alice.TotalLines != 4 { t.Errorf("Alice = %+v, want {Commits:3 Additions:3 Deletions:1 TotalLines:4}", alice) } bob := cs[1] if bob.Author != bobName { t.Errorf("cs[1].Author = %q, want Bob", bob.Author) } if bob.Commits != 2 || bob.Additions != 2 || bob.Deletions != 1 || bob.TotalLines != 3 { t.Errorf("Bob = %+v, want {Commits:2 Additions:2 Deletions:1 TotalLines:3}", bob) } if alice.Email != aliceEmail || bob.Email != bobEmail { t.Errorf("emails = %q / %q", alice.Email, bob.Email) } } func TestGetContributorsEmptyRefMeansHEAD(t *testing.T) { f := newFixture(t) empty, err := f.repo.GetContributors("") if err != nil { t.Fatal(err) } head, err := f.repo.GetContributors("HEAD") if err != nil { t.Fatal(err) } if len(empty) != len(head) { t.Errorf("contributor count differs: '' -> %d, HEAD -> %d", len(empty), len(head)) } } func TestGetContributorsEmptyRepo(t *testing.T) { dir := t.TempDir() bare := dir + "/empty.git" gitRun(t, "", nil, "init", "-q", "--bare", bare) r, err := OpenRepository(context.Background(), bare) if err != nil { t.Fatal(err) } cs, err := r.GetContributors("HEAD") if err != nil { t.Fatalf("GetContributors on empty repo: %v", err) } if len(cs) != 0 { t.Errorf("empty repo contributors = %d, want 0", len(cs)) } } func TestGetStats(t *testing.T) { f := newFixture(t) s, err := f.repo.GetStats() if err != nil { t.Fatal(err) } if s.TotalCommits != 5 { t.Errorf("TotalCommits = %d, want 5", s.TotalCommits) } if s.TotalBranches != 2 { t.Errorf("TotalBranches = %d, want 2", s.TotalBranches) } if s.TotalTags != 2 { t.Errorf("TotalTags = %d, want 2", s.TotalTags) } if s.TotalSize <= 0 { t.Errorf("TotalSize = %d, want > 0", s.TotalSize) } if len(s.Contributors) != 2 { t.Errorf("Contributors = %d, want 2", len(s.Contributors)) } if !s.FirstCommit.Equal(mustParseTime(t, d1)) { t.Errorf("FirstCommit = %v, want %v", s.FirstCommit, mustParseTime(t, d1)) } if !s.LastCommit.Equal(mustParseTime(t, d5)) { t.Errorf("LastCommit = %v, want %v", s.LastCommit, mustParseTime(t, d5)) } if !s.FirstCommit.Before(s.LastCommit) { t.Error("FirstCommit not before LastCommit") } } func TestCountNonEmptyLines(t *testing.T) { cases := []struct { in string want int }{ {"", 0}, {"a", 1}, {"a\nb", 2}, {"a\n\nb", 2}, {"a\nb\n", 2}, {"\n\n", 0}, {" \n \n", 0}, {"x\ny\nz", 3}, } for _, tc := range cases { if got := countNonEmptyLines(tc.in); got != tc.want { t.Errorf("countNonEmptyLines(%q) = %d, want %d", tc.in, got, tc.want) } } }