▍ humdrum codex / custard v0.3.0
license AGPL-3.0
2.6 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gitread

import (
	"os/exec"
	"path/filepath"
	"testing"
)

// newBareWithReleases builds a bare repo named "<root>/proj.git" containing one
// commit plus the given release branches (each pointing at its own commit whose
// subject is "ship <branch>"). 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")
	}
}