Files
simplegit/gitcmd/branch_test.go
T
2026-07-15 11:06:48 -04:00

80 lines
1.8 KiB
Go

package gitcmd
import (
"context"
"testing"
)
func TestListBranches(t *testing.T) {
f := newFixture(t)
bs, err := f.repo.ListBranches()
if err != nil {
t.Fatal(err)
}
if len(bs) != 2 {
t.Fatalf("len = %d, want 2", len(bs))
}
byName := map[string]Branch{}
for _, b := range bs {
byName[b.Name] = b
}
main, ok := byName["main"]
if !ok {
t.Fatal("missing branch main")
}
if !main.IsDefault {
t.Error("main.IsDefault = false, want true")
}
if main.SHA != f.c5 {
t.Errorf("main.SHA = %q, want %q", main.SHA, f.c5)
}
if len(main.ShortSHA) < 7 || main.SHA[:7] != main.ShortSHA {
t.Errorf("main.ShortSHA = %q, want 7-char prefix of %q", main.ShortSHA, main.SHA)
}
if main.Author != bobName {
t.Errorf("main.Author = %q, want Bob", main.Author)
}
if main.Message != "remove temp" {
t.Errorf("main.Message = %q, want 'remove temp'", main.Message)
}
if !main.When.Equal(mustParseTime(t, d5)) {
t.Errorf("main.When = %v, want %v", main.When, mustParseTime(t, d5))
}
feat, ok := byName["feature"]
if !ok {
t.Fatal("missing branch feature")
}
if feat.IsDefault {
t.Error("feature.IsDefault = true, want false")
}
if feat.SHA != f.c3 {
t.Errorf("feature.SHA = %q, want %q", feat.SHA, f.c3)
}
if feat.Author != aliceName {
t.Errorf("feature.Author = %q, want Alice", feat.Author)
}
if !feat.When.Equal(mustParseTime(t, d3)) {
t.Errorf("feature.When = %v, want %v", feat.When, mustParseTime(t, d3))
}
}
func TestListBranchesEmptyRepo(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)
}
bs, err := r.ListBranches()
if err != nil {
t.Fatalf("ListBranches on empty repo: %v", err)
}
if len(bs) != 0 {
t.Errorf("empty repo branches = %d, want 0", len(bs))
}
}