gunk-dev/the-valley

append_test.go

source5f0229dc1066main's published tip history
9.3 KiB
1package rebase
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "the-valley/change"
13)
14
15// moveBranches prepares a family and pushes its branches, appending no
16// version, and returns each layer's prepared head by change ID.
17func (w *world) moveBranches(f *Family) map[string]string {
18 w.t.Helper()
19 ctx := context.Background()
20 p, err := Prepare(ctx, w.g, f)
21 if err != nil {
22 w.t.Fatalf("prepare: %v", err)
23 }
24 defer p.Release(ctx, w.g)
25 pub, err := Publish(ctx, w.g, p)
26 if err != nil || pub.Push != Pushed {
27 w.t.Fatalf("publish: %v %v", err, pub.Push)
28 }
29 heads := map[string]string{}
30 for l, h := range p.Heads {
31 heads[l.ID] = h
32 }
33 return heads
34}
35
36// ownerPushes has the owner add a commit on top of origin's branch.
37func (w *world) ownerPushes(branch string) string {
38 w.t.Helper()
39 w.git("fetch", "--quiet", "origin")
40 w.git("checkout", "--quiet", "-B", branch, "origin/"+branch)
41 file := branch + "-own.txt"
42 before, _ := os.ReadFile(filepath.Join(w.work, file))
43 head := w.commit("the owner's own work on "+branch, file, string(before)+"own\n")
44 w.git("push", "--quiet", "origin", branch)
45 return head
46}
47
48// A restack append writes the record commit and pushes nothing: origin's
49// record stays where it was, and the commit, with the record it holds, is
50// in the repository for the caller to keep before the push. The version
51// keeps the active version's title, though the commit's subject differs,
52// and names its branch. The push then moves the record to that commit.
53func TestRestackAppendKeepsTheActiveTitle(t *testing.T) {
54 w := newWorld(t)
55 w.layer("a", "main")
56 w.readyTitled(idA, "a", "A title unlike its commit's subject")
57 w.moveMain("main.txt", "main\n")
58 f := w.capture(Selection{ID: idA})
59 was := f.Bottom().Head
60 head := w.moveBranches(f)[idA]
61 ref := change.Ref(Target, idA)
62 old := w.at(ref)
63 ctx := context.Background()
64
65 p, err := BuildRestack(ctx, w.g, RestackAppend{ID: idA, Head: head, IfActive: was, Principal: "restacker"})
66 if err != nil {
67 t.Fatal(err)
68 }
69 if p.Old != old || p.Ref != ref || p.Commit == "" || p.Version != 2 || w.at(ref) != old {
70 t.Fatalf("the build pushed, or does not say what it wrote: %+v; origin's record is at %s", p, w.at(ref))
71 }
72 if parents := w.git("rev-list", "--parents", "-n", "1", p.Commit); parents != p.Commit+" "+old+" "+head {
73 t.Errorf("the record commit's parents are %q", parents)
74 }
75 r, err := ReadRecord(ctx, w.g, p.Commit)
76 if err != nil {
77 t.Fatal(err)
78 }
79 v := r.ActiveVersion()
80 if v.N != 2 || HeadOf(v) != head || v.Title != "A title unlike its commit's subject" || v.Branch != "a" || v.Uploader != "restacker" {
81 t.Errorf("the new version is %+v", v)
82 }
83 ap := p.Push(ctx, w.g)
84 if ap.Err != nil || ap.Version != 2 || ap.Write.Push != Pushed || w.at(ref) != p.Commit {
85 t.Fatalf("the push: %+v; origin's record is at %s", ap, w.at(ref))
86 }
87}
88
89// A manual append keeps titling a version by its head's subject, and
90// takes a head anywhere on the branch.
91func TestManualAppendTitlesBySubject(t *testing.T) {
92 w := newWorld(t)
93 w.layer("b", "main")
94 w.readyTitled(idB, "b", "B's own title")
95 was := w.at("refs/heads/b")
96 head := w.ownerPushes("b")
97 w.ownerPushes("b")
98
99 ap := AppendVersion(context.Background(), w.g, Append{ID: idB, Branch: "b", Head: head, IfActive: was, Principal: "author"})
100 if ap.Err != nil || ap.Version != 2 {
101 t.Fatalf("append: %+v", ap)
102 }
103 if v := w.record(idB).ActiveVersion(); v.Title != "the owner's own work on b" || HeadOf(v) != head {
104 t.Errorf("the new version is %+v", v)
105 }
106}
107
108// A restack append records only the head the restack moved the branch to.
109// A branch the owner has pushed past that head is refused when the record
110// is built, and again when it is pushed, with nothing moved either time.
111func TestRestackAppendNeedsTheBranchAtItsHead(t *testing.T) {
112 w := newWorld(t)
113 w.layer("a", "main")
114 w.layer("b", "main")
115 w.ready(idA, "a")
116 w.ready(idB, "b")
117 w.moveMain("main.txt", "main\n")
118 ctx := context.Background()
119
120 fa := w.capture(Selection{ID: idA})
121 wasA := fa.Bottom().Head
122 headA := w.moveBranches(fa)[idA]
123 w.ownerPushes("a")
124 before := w.refs()
125 _, err := BuildRestack(ctx, w.g, RestackAppend{ID: idA, Head: headA, IfActive: wasA, Principal: "restacker"})
126 if err == nil || !strings.Contains(err.Error(), "not "+short(headA)) {
127 t.Fatalf("a restack append to a branch past its head: %v", err)
128 }
129 if after := w.refs(); after != before {
130 t.Errorf("a refused restack append moved something")
131 }
132
133 fb := w.capture(Selection{ID: idB})
134 wasB := fb.Bottom().Head
135 headB := w.moveBranches(fb)[idB]
136 p, err := BuildRestack(ctx, w.g, RestackAppend{ID: idB, Head: headB, IfActive: wasB, Principal: "restacker"})
137 if err != nil {
138 t.Fatal(err)
139 }
140 w.ownerPushes("b")
141 before = w.refs()
142 ap := p.Push(ctx, w.g)
143 if ap.Err == nil || ap.Version != 0 || ap.Write.Push != Unpushed || ap.Write.Commit != p.Commit || ap.Write.Old != p.Old {
144 t.Fatalf("a restack push to a branch moved since the build: %+v", ap)
145 }
146 if after := w.refs(); after != before {
147 t.Errorf("a refused restack push moved something")
148 }
149}
150
151// A report commit is written and then pushed: written, it holds the report
152// and is not on origin; pushed, origin's report ref names it. The next
153// report has the one it replaces as its parent, and a push leased on a
154// report that has moved is refused.
155func TestReportIsWrittenThenPushed(t *testing.T) {
156 w := newWorld(t)
157 w.layer("a", "main")
158 w.ready(idA, "a")
159 ctx := context.Background()
160 tip := change.HeadPrefix + w.at("refs/heads/a")
161 r := &change.RestackReport{
162 Schema: change.RestackSchema, ChangeID: idA, Target: Target,
163 TargetTip: change.HeadPrefix + w.at("refs/heads/main"), Operation: "test-1", Result: change.RestackBlocked,
164 Layers: []change.RestackLayer{{
165 ChangeID: idA, Record: change.HeadPrefix + w.at(change.Ref(Target, idA)), Version: 1,
166 Head: tip, Branch: "a", BranchTip: &tip,
167 }},
168 }
169 ref := RestackPrefix + idA
170
171 first, err := WriteReport(ctx, w.g, r, ReportMessage("kkkk", r), "")
172 if err != nil {
173 t.Fatal(err)
174 }
175 if w.at(ref) != "" {
176 t.Fatal("writing a report pushed it")
177 }
178 if parents := w.git("rev-list", "--parents", "-n", "1", first); parents != first {
179 t.Errorf("a change's first report has parents: %q", parents)
180 }
181 if got, err := ReadReport(ctx, w.g, first); err != nil || got.Operation != "test-1" || got.Result != change.RestackBlocked {
182 t.Fatalf("the report commit reads back as %+v, %v", got, err)
183 }
184 if wr := PushReport(ctx, w.g, idA, "", first); wr.Push != Pushed || w.at(ref) != first {
185 t.Fatalf("the first push: %+v", wr)
186 }
187
188 r.Operation = "test-2"
189 second, err := WriteReport(ctx, w.g, r, ReportMessage("kkkk", r), first)
190 if err != nil {
191 t.Fatal(err)
192 }
193 if parents := w.git("rev-list", "--parents", "-n", "1", second); parents != second+" "+first {
194 t.Errorf("a report's parents are %q", parents)
195 }
196 if wr := PushReport(ctx, w.g, idA, "", second); wr.Push != Refused || w.at(ref) != first {
197 t.Errorf("a push leased on no report, where there is one: %+v", wr)
198 }
199 if wr := PushReport(ctx, w.g, idA, first, second); wr.Push != Pushed || w.at(ref) != second {
200 t.Errorf("the second push: %+v", wr)
201 }
202}
203
204// Release runs under a context that is already done, and for no longer
205// than its bound: a hook that never returns while the kept refs are
206// deleted is killed with everything it started.
207func TestReleaseEndsWithinItsBound(t *testing.T) {
208 w := newWorld(t)
209 w.layer("a", "main")
210 w.ready(idA, "a")
211 w.moveMain("main.txt", "main\n")
212 p, err := Prepare(context.Background(), w.g, w.capture(Selection{ID: idA}))
213 if err != nil {
214 t.Fatal(err)
215 }
216 guard := "[ \"$1\" = prepared ] || exit 0\ncase $(cat) in *' " + keepPrefix + "'*) ;; *) exit 0 ;; esac"
217 b := newBlocker(t, filepath.Join(w.work, ".git", "hooks", "reference-transaction"), guard)
218 defer func(was time.Duration) { cleanupBound = was }(cleanupBound)
219 cleanupBound = 2 * time.Second
220
221 ctx, cancel := context.WithCancel(context.Background())
222 cancel()
223 start := time.Now()
224 done := make(chan error, 1)
225 go func() { done <- p.Release(ctx, w.g) }()
226 select {
227 case err := <-done:
228 if err == nil {
229 t.Error("a release whose refs were never deleted says it released them")
230 }
231 case <-time.After(30 * time.Second):
232 t.Fatal("release did not return")
233 }
234 if took := time.Since(start); took > cleanupBound+waitDelay+5*time.Second {
235 t.Errorf("release took %v, with a bound of %v", took, cleanupBound)
236 }
237 b.wait(t)
238 if b.beating() {
239 t.Error("the hook outlived the bound")
240 }
241}
242
243// An unattended command that reaches origin is killed whole when its
244// context's deadline passes: git and the upload-pack it started for a
245// fetch that never answers.
246func TestUnattendedFetchEndsAtTheDeadline(t *testing.T) {
247 w := newWorld(t)
248 b := newBlocker(t, filepath.Join(t.TempDir(), "upload-pack"), "")
249 w.git("config", "remote.origin.uploadpack", b.script)
250 g := w.g
251 g.Unattended = true
252 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
253 defer cancel()
254
255 start := time.Now()
256 _, err := ListOrigin(ctx, g, "refs/heads/*")
257 if err == nil {
258 t.Fatal("a fetch that never answered read origin")
259 }
260 if took := time.Since(start); took > 2*time.Second+waitDelay+5*time.Second {
261 t.Errorf("the read took %v, with a deadline of 2s", took)
262 }
263 if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
264 t.Errorf("the read ended before its deadline: %v", err)
265 }
266 b.wait(t)
267 if b.beating() {
268 t.Error("the upload-pack git started outlived the deadline")
269 }
270}