package gitread import ( "os/exec" "path/filepath" "testing" ) // newBareWithReleases builds a bare repo named "/proj.git" containing one // commit plus the given release branches (each pointing at its own commit whose // subject is "ship "). Returns the Store rooted at the temp dir. func newBareWithReleases(t *testing.T, branches ...string) *Store { t.Helper() root := t.TempDir() work := filepath.Join(root, "work") run := func(dir string, args ...string) { t.Helper() c := exec.Command("git", args...) c.Dir = dir c.Env = append(c.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@x", "GIT_AUTHOR_DATE=2026-01-01T00:00:00", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@x", "GIT_COMMITTER_DATE=2026-01-01T00:00:00") if out, err := c.CombinedOutput(); err != nil { t.Fatalf("git %v: %v: %s", args, err, out) } } run(root, "init", "-q", "-b", "main", "work") run(work, "commit", "-q", "--allow-empty", "-m", "init") for _, b := range branches { run(work, "checkout", "-q", "-b", b) run(work, "commit", "-q", "--allow-empty", "-m", "ship "+b) run(work, "checkout", "-q", "main") } run(work, "clone", "-q", "--bare", work, filepath.Join(root, "proj.git")) return New(root) } func TestReleaseBookmarks(t *testing.T) { s := newBareWithReleases(t, "release/v1.0.0", "release/v1.2.0", "release/v0.9.0", "feature/x") got, err := s.ReleaseBookmarks("proj", "release/") if err != nil { t.Fatalf("ReleaseBookmarks: %v", err) } want := []string{"v1.2.0", "v1.0.0", "v0.9.0"} // semver-descending, feature/x excluded if len(got) != len(want) { t.Fatalf("got %d refs %v, want %v", len(got), got, want) } for i, w := range want { if got[i].Name != w { t.Errorf("ReleaseBookmarks[%d].Name = %q, want %q", i, got[i].Name, w) } } if got[0].Message != "ship release/v1.2.0" { t.Errorf("Message = %q, want tip subject", got[0].Message) } } func TestLatestReleaseAndGuard(t *testing.T) { s := newBareWithReleases(t, "release/v1.0.0", "release/v1.2.0") latest, ok := s.LatestRelease("proj", "release/") if !ok || latest.Name != "v1.2.0" { t.Fatalf("LatestRelease = %q,%v; want v1.2.0,true", latest.Name, ok) } if !s.IsHighestRelease("proj", "release/", "v1.2.0") { t.Error("v1.2.0 should be highest (tie)") } if !s.IsHighestRelease("proj", "release/", "v2.0.0") { t.Error("v2.0.0 should be highest") } if s.IsHighestRelease("proj", "release/", "v1.1.0") { t.Error("v1.1.0 must NOT be highest (downgrade guard)") } empty := newBareWithReleases(t) if !empty.IsHighestRelease("proj", "release/", "v1.0.0") { t.Error("first release should be highest when none exist") } }