internal/control/release_test.go
205 lines · 7804 bytes
5 symbols in this file
1package control
2
3import (
4 "bytes"
5 "encoding/json"
6 "strconv"
7 "strings"
8 "testing"
9
10 "gitbay.org/gitbay/internal/protocol"
11 "gitbay.org/gitbay/internal/store"
12)
13
14func TestReleaseListPagesAndHidesTagTitle(t *testing.T) {
15 st, repo, uid := newQueueTestRepo(t)
16 for _, tag := range []string{"v1", "v2", "v3"} {
17 title := tag
18 if tag == "v2" {
19 title = "Second"
20 }
21 if _, err := st.CreateRelease(repo.ID, tag, title, "", uid, "md"); err != nil {
22 t.Fatal(err)
23 }
24 }
25 c, errOut := pruneCtx(st, t.TempDir(), store.User{ID: uid})
26 if code := Dispatch(c, []string{"release", "list", repo.Path(), "--limit", "2"}); code != protocol.ExitOK {
27 t.Fatalf("exit %d: %s", code, errOut)
28 }
29 lines := strings.Split(strings.TrimSpace(c.Stdout.(*bytes.Buffer).String()), "\n")
30 if len(lines) != 3 || lines[0] != "v3\t\t0 asset(s)" || lines[1] != "v2\tSecond\t0 asset(s)" || !strings.HasPrefix(lines[2], "next\t") {
31 t.Fatalf("page 1:\n%s", strings.Join(lines, "\n"))
32 }
33 cursor := strings.TrimPrefix(lines[2], "next\t")
34
35 c, errOut = pruneCtx(st, t.TempDir(), store.User{ID: uid})
36 if code := Dispatch(c, []string{"release", "list", repo.Path(), "--cursor", cursor}); code != protocol.ExitOK {
37 t.Fatalf("exit %d: %s", code, errOut)
38 }
39 if got := c.Stdout.(*bytes.Buffer).String(); got != "v1\t\t0 asset(s)\n" {
40 t.Fatalf("page 2: %q", got)
41 }
42}
43
44// The cursor names the pointed-to release by (created_at, id), not by a
45// live lookup of that id: deleting it between page requests must not
46// blank the next page.
47func TestReleaseListPageSurvivesTheCursorReleaseBeingDeleted(t *testing.T) {
48 st, repo, uid := newQueueTestRepo(t)
49 for _, tag := range []string{"v1", "v2", "v3"} {
50 if _, err := st.CreateRelease(repo.ID, tag, tag, "", uid, "md"); err != nil {
51 t.Fatal(err)
52 }
53 }
54 c, errOut := pruneCtx(st, t.TempDir(), store.User{ID: uid})
55 if code := Dispatch(c, []string{"release", "list", repo.Path(), "--limit", "2"}); code != protocol.ExitOK {
56 t.Fatalf("exit %d: %s", code, errOut)
57 }
58 lines := strings.Split(strings.TrimSpace(c.Stdout.(*bytes.Buffer).String()), "\n")
59 if len(lines) != 3 || !strings.HasPrefix(lines[2], "next\t") {
60 t.Fatalf("page 1:\n%s", strings.Join(lines, "\n"))
61 }
62 cursor := strings.TrimPrefix(lines[2], "next\t")
63
64 rels, err := st.ListReleases(repo.ID)
65 if err != nil {
66 t.Fatal(err)
67 }
68 var v2ID int64
69 for _, r := range rels {
70 if r.Tag == "v2" {
71 v2ID = r.ID
72 }
73 }
74 if v2ID == 0 {
75 t.Fatal("v2 not found")
76 }
77 if err := st.DeleteRelease(v2ID); err != nil {
78 t.Fatal(err)
79 }
80
81 c, errOut = pruneCtx(st, t.TempDir(), store.User{ID: uid})
82 if code := Dispatch(c, []string{"release", "list", repo.Path(), "--cursor", cursor}); code != protocol.ExitOK {
83 t.Fatalf("exit %d: %s", code, errOut)
84 }
85 if got := c.Stdout.(*bytes.Buffer).String(); got != "v1\t\t0 asset(s)\n" {
86 t.Fatalf("page 2 after deleting the cursor release: %q", got)
87 }
88}
89
90// A merge records the commit it landed as: the head for a fast-forward,
91// the new commit for a squash (#331).
92func TestMergeRecordsTheLandedCommit(t *testing.T) {
93 f := newQueueFixture(t, nil)
94 f.verified(f.alice)
95 f.mustRun(f.alice, "mr", "merge", f.repo.Path(), "1", "--strategy", "squash")
96 tip := strings.TrimSpace(f.git(f.dir, "rev-parse", "main"))
97 if mr := f.mr(); mr.MergedSHA != tip || tip == f.headSHA {
98 t.Fatalf("merged_sha %q, main %q, head %q", mr.MergedSHA, tip, f.headSHA)
99 }
100}
101
102// land opens a merge request from a new branch holding one commit on top
103// of the repository's main and merges it with strategy, returning its
104// number.
105func (f *queueFixture) land(branch, title, strategy string) int64 {
106 f.t.Helper()
107 f.git(f.src, "fetch", "-q", f.dir, "main:refs/remotes/bare/main")
108 f.git(f.src, "checkout", "-q", "-b", branch, "bare/main")
109 f.write(branch+".txt", title+"\n")
110 f.git(f.src, "add", ".")
111 f.git(f.src, "commit", "-q", "-m", title)
112 sha := strings.TrimSpace(f.git(f.src, "rev-parse", "HEAD"))
113 f.git(f.dir, "fetch", "-q", f.src, "refs/heads/"+branch+":refs/heads/"+branch)
114 id, err := f.st.CreateMR(f.repo.ID, f.alice.ID, f.repo.ID, branch, "main", title, "", sha, "md", false)
115 if err != nil {
116 f.t.Fatal(err)
117 }
118 mr, err := f.st.MRByID(id)
119 if err != nil {
120 f.t.Fatal(err)
121 }
122 f.git(f.dir, "update-ref", mrHeadRef(mr.Number), sha)
123 f.mustRun(f.alice, "mr", "merge", f.repo.Path(), strconv.FormatInt(mr.Number, 10), "--strategy", strategy)
124 return mr.Number
125}
126
127// release notes lists the merge requests landed between the previous
128// release's tag and a ref, oldest first, one line each; --since names
129// the range's start instead (#331).
130func TestReleaseNotesListsMergedMRsSinceThePreviousRelease(t *testing.T) {
131 f := newQueueFixture(t, nil)
132 f.verified(f.alice)
133 f.git(f.dir, "tag", "v0", f.targetSH)
134 f.mustRun(f.alice, "mr", "merge", f.repo.Path(), "1", "--strategy", "ff")
135
136 // No release yet: the whole history of the ref.
137 if out := f.mustRun(f.alice, "release", "notes", f.repo.Path(), "main"); out != "- t (!1) by @alice\n" {
138 t.Fatalf("no previous release:\n%s", out)
139 }
140
141 f.git(f.dir, "tag", "v1", "main")
142 f.mustRun(f.alice, "release", "create", f.repo.Path(), "v1")
143 f.land("two", "two", "squash")
144 f.land("three", "three", "ff")
145 // An open merge request and one into another branch are not changes.
146 f.branch("dev", f.targetSH)
147 f.git(f.src, "checkout", "-q", "-b", "side", f.targetSH)
148 f.write("side.txt", "s\n")
149 f.git(f.src, "add", ".")
150 f.git(f.src, "commit", "-q", "-m", "side")
151 side := strings.TrimSpace(f.git(f.src, "rev-parse", "HEAD"))
152 f.git(f.dir, "fetch", "-q", f.src, "refs/heads/side:refs/heads/side")
153 if _, err := f.st.CreateMR(f.repo.ID, f.alice.ID, f.repo.ID, "side", "dev", "side", "", side, "md", false); err != nil {
154 t.Fatal(err)
155 }
156 f.git(f.dir, "update-ref", mrHeadRef(4), side)
157 f.mustRun(f.alice, "mr", "merge", f.repo.Path(), "4", "--strategy", "ff")
158
159 if out := f.mustRun(f.alice, "release", "notes", f.repo.Path(), "main"); out != "- two (!2) by @alice\n- three (!3) by @alice\n" {
160 t.Fatalf("since v1:\n%s", out)
161 }
162 // --since overrides the previous release; a row merged before 0079
163 // has no merged_sha and is matched on its head.
164 if _, err := f.st.DB.Exec("UPDATE merge_requests SET merged_sha = '' WHERE number = 1"); err != nil {
165 t.Fatal(err)
166 }
167 if out := f.mustRun(f.alice, "release", "notes", f.repo.Path(), "main", "--since", "v0"); out != "- t (!1) by @alice\n- two (!2) by @alice\n- three (!3) by @alice\n" {
168 t.Fatalf("--since v0:\n%s", out)
169 }
170 // Regenerating for a tag that already has a release starts at the one before it.
171 f.git(f.dir, "tag", "v2", "main")
172 f.mustRun(f.alice, "release", "create", f.repo.Path(), "v2")
173 if out := f.mustRun(f.alice, "release", "notes", f.repo.Path(), "v2"); out != "- two (!2) by @alice\n- three (!3) by @alice\n" {
174 t.Fatalf("v2 with its own release:\n%s", out)
175 }
176
177 out := f.mustRun(f.alice, "release", "notes", f.repo.Path(), "v2", "--json")
178 var env struct {
179 Data struct {
180 Since string `json:"since"`
181 Items []struct {
182 Number int64 `json:"number"`
183 Title string `json:"title"`
184 Author string `json:"author"`
185 MergedAt string `json:"merged_at"`
186 } `json:"items"`
187 } `json:"data"`
188 }
189 if err := json.Unmarshal([]byte(out), &env); err != nil {
190 t.Fatal(err)
191 }
192 d := env.Data
193 if d.Since != "v1" || len(d.Items) != 2 || d.Items[0].Number != 2 || d.Items[1].Title != "three" ||
194 d.Items[0].Author != "alice" || d.Items[0].MergedAt == "" {
195 t.Fatalf("json: %s", out)
196 }
197
198 // An unknown ref or --since is not found.
199 if code, _, _ := f.run(f.alice, "release", "notes", f.repo.Path(), "nope"); code != protocol.ExitNotFound {
200 t.Fatalf("unknown ref: exit %d", code)
201 }
202 if code, _, _ := f.run(f.alice, "release", "notes", f.repo.Path(), "main", "--since", "nope"); code != protocol.ExitNotFound {
203 t.Fatalf("unknown --since: exit %d", code)
204 }
205}