▍ humdrum codex / custard v0.3.0
license AGPL-3.0
4.1 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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package release

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

	"custard/internal/gitread"
)

func TestReleaseSourceAndPrefix(t *testing.T) {
	var c RepoConfig
	if got := c.ReleaseSource(); got != SourceTag {
		t.Errorf("default ReleaseSource = %q, want %q", got, SourceTag)
	}
	if got := c.Prefix(); got != "release/" {
		t.Errorf("default Prefix = %q, want release/", got)
	}
	c.Brew.Source = "bookmark"
	c.Brew.BookmarkPrefix = "rel/"
	if got := c.ReleaseSource(); got != SourceBookmark {
		t.Errorf("ReleaseSource = %q, want %q", got, SourceBookmark)
	}
	if got := c.Prefix(); got != "rel/" {
		t.Errorf("Prefix = %q, want rel/", got)
	}
}

func TestBookmarkVersion(t *testing.T) {
	cases := []struct {
		short, prefix, want string
		ok                  bool
	}{
		{"release/v1.2.3", "release/", "v1.2.3", true},
		{"rel/v0.1.0", "rel/", "v0.1.0", true},
		{"release/v1.2.3", "rel/", "", false},     // wrong prefix
		{"release/main", "release/", "", false},   // not semver
		{"main", "release/", "", false},           // no prefix
		{"release/v1.2", "release/", "", false},   // not 3-part
	}
	for _, c := range cases {
		got, ok := BookmarkVersion(c.short, c.prefix)
		if ok != c.ok || got != c.want {
			t.Errorf("BookmarkVersion(%q,%q) = %q,%v; want %q,%v", c.short, c.prefix, got, ok, c.want, c.ok)
		}
	}
}

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)
	}
}

// TestPublishArchivesBranchRef verifies bookmark mode: archive the release
// BRANCH while naming the tarball/formula by the VERSION.
func TestPublishArchivesBranchRef(t *testing.T) {
	root := t.TempDir()
	work := filepath.Join(root, "work")
	gitRun(t, root, "init", "-q", "-b", "main", "work")
	if err := os.WriteFile(filepath.Join(work, "go.mod"), []byte("module proj\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"))

	// A bare tap repo to receive the formula.
	tap := filepath.Join(root, "homebrew-tap.git")
	gitRun(t, root, "init", "-q", "--bare", "homebrew-tap.git")
	// Seed the tap with one commit so HEAD exists for cloning.
	tapWork := filepath.Join(root, "tapwork")
	gitRun(t, root, "clone", "-q", "homebrew-tap.git", "tapwork")
	if err := os.WriteFile(filepath.Join(tapWork, "README"), []byte("tap\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	gitRun(t, tapWork, "add", ".")
	gitRun(t, tapWork, "commit", "-q", "-m", "seed")
	gitRun(t, tapWork, "push", "-q", "origin", "HEAD:master")

	dl := filepath.Join(root, "dl")
	ver, err := Publish(Options{
		Store:     gitread.New(root),
		ReposPath: root,
		DLPath:    dl,
		TapRepo:   "homebrew-tap",
		BaseURL:   "https://example.test",
		Repo:      "proj",
		Ref:       "release/v1.2.3", // archive the branch
		Tag:       "v1.2.3",         // version
		Desc:      "a thing",
		Package:   ".",
	})
	if err != nil {
		t.Fatalf("Publish: %v", err)
	}
	if ver != "1.2.3" {
		t.Errorf("ver = %q, want 1.2.3", ver)
	}
	if _, err := os.Stat(filepath.Join(dl, "proj-1.2.3.tar.gz")); err != nil {
		t.Errorf("tarball not written: %v", err)
	}
	_ = tap
}

func TestPublishDefaultsRefToTag(t *testing.T) {
	// With Ref empty, Publish must archive Tag (tag-mode back-compat). A bogus
	// repo path makes git archive fail; assert the error names the tag, proving
	// Tag was used as the archive ref.
	_, err := Publish(Options{
		Store: gitread.New(t.TempDir()), ReposPath: t.TempDir(), DLPath: t.TempDir(),
		Repo: "nope", Tag: "v9.9.9",
	})
	if err == nil || !strings.Contains(err.Error(), "v9.9.9") {
		t.Fatalf("expected archive error naming v9.9.9, got %v", err)
	}
}