package server import ( "os" "os/exec" "path/filepath" "testing" "custard/internal/config" "custard/internal/gitread" ) func gitRun(t *testing.T, 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) } } // bookmarkRepo builds a bare "proj.git" in bookmark mode with one release branch. func bookmarkRepo(t *testing.T) *Server { t.Helper() root := t.TempDir() work := filepath.Join(root, "work") gitRun(t, root, "init", "-q", "-b", "main", "work") if err := os.WriteFile(filepath.Join(work, ".custard.yaml"), []byte("brew:\n enabled: true\n source: bookmark\n"), 0o644); err != nil { t.Fatal(err) } gitRun(t, work, "add", ".") gitRun(t, work, "commit", "-q", "-m", "init") gitRun(t, work, "checkout", "-q", "-b", "release/v1.2.3") gitRun(t, work, "commit", "-q", "--allow-empty", "-m", "ship v1.2.3") gitRun(t, work, "checkout", "-q", "main") gitRun(t, work, "clone", "-q", "--bare", work, filepath.Join(root, "proj.git")) return &Server{store: gitread.New(root), cfg: config.Config{ReposPath: root}} } func TestLatestReleaseBookmarkMode(t *testing.T) { s := bookmarkRepo(t) ref, ok := s.latestRelease("proj") if !ok || ref.Name != "v1.2.3" { t.Fatalf("latestRelease = %q,%v; want v1.2.3,true", ref.Name, ok) } if ref.Message != "ship v1.2.3" { t.Errorf("label = %q, want tip subject", ref.Message) } } // tagRepo builds a bare "proj.git" in tag mode (no .custard.yaml) with a // lightweight semver tag v1.0.0 on a commit. func tagRepo(t *testing.T) *Server { t.Helper() root := t.TempDir() work := filepath.Join(root, "work") gitRun(t, root, "init", "-q", "-b", "main", "work") if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("hello\n"), 0o644); err != nil { t.Fatal(err) } gitRun(t, work, "add", ".") gitRun(t, work, "commit", "-q", "-m", "init") gitRun(t, work, "tag", "v1.0.0") gitRun(t, work, "clone", "-q", "--bare", work, filepath.Join(root, "proj.git")) return &Server{store: gitread.New(root), cfg: config.Config{ReposPath: root}} } func TestLatestReleaseTagMode(t *testing.T) { s := tagRepo(t) ref, ok := s.latestRelease("proj") if !ok || ref.Name != "v1.0.0" { t.Fatalf("latestRelease = %q,%v; want v1.0.0,true", ref.Name, ok) } }