gunk-dev/the-valley

append_test.go

sourcefe48b06af1c9main's published tip history
14.7 KiB
1package rebase
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "os"
8 "path/filepath"
9 "reflect"
10 "strings"
11 "testing"
12 "time"
13
14 "the-valley/change"
15)
16
17// moveBranches prepares a family and pushes its branches, appending no
18// version, and returns each layer's prepared head by change ID.
19func (w *world) moveBranches(f *Family) map[string]string {
20 w.t.Helper()
21 ctx := context.Background()
22 p, err := Prepare(ctx, w.g, f)
23 if err != nil {
24 w.t.Fatalf("prepare: %v", err)
25 }
26 defer p.Release(ctx, w.g)
27 pub, err := Publish(ctx, w.g, p)
28 if err != nil || pub.Push != Pushed {
29 w.t.Fatalf("publish: %v %v", err, pub.Push)
30 }
31 heads := map[string]string{}
32 for l, h := range p.Heads {
33 heads[l.ID] = h
34 }
35 return heads
36}
37
38// ownerPushes has the owner add a commit on top of origin's branch.
39func (w *world) ownerPushes(branch string) string {
40 w.t.Helper()
41 w.git("fetch", "--quiet", "origin")
42 w.git("checkout", "--quiet", "-B", branch, "origin/"+branch)
43 file := branch + "-own.txt"
44 before, _ := os.ReadFile(filepath.Join(w.work, file))
45 head := w.commit("the owner's own work on "+branch, file, string(before)+"own\n")
46 w.git("push", "--quiet", "origin", branch)
47 return head
48}
49
50// A restack append writes the record commit and pushes nothing: origin's
51// record stays where it was, and the commit, with the record it holds, is
52// in the repository for the caller to keep before the push. The version
53// keeps the active version's title, though the commit's subject differs,
54// and names its branch. The push then moves the record to that commit.
55func TestRestackAppendKeepsTheActiveTitle(t *testing.T) {
56 w := newWorld(t)
57 w.layer("a", "main")
58 w.readyTitled(idA, "a", "A title unlike its commit's subject")
59 w.moveMain("main.txt", "main\n")
60 f := w.capture(Selection{ID: idA})
61 was := f.Bottom().Head
62 head := w.moveBranches(f)[idA]
63 ref := change.Ref(Target, idA)
64 old := w.at(ref)
65 ctx := context.Background()
66
67 p, err := BuildRestack(ctx, w.g, RestackAppend{ID: idA, Head: head, IfActive: was, Principal: "restacker"})
68 if err != nil {
69 t.Fatal(err)
70 }
71 if p.Old != old || p.Ref != ref || p.Commit == "" || p.Version != 2 || w.at(ref) != old {
72 t.Fatalf("the build pushed, or does not say what it wrote: %+v; origin's record is at %s", p, w.at(ref))
73 }
74 if parents := w.git("rev-list", "--parents", "-n", "1", p.Commit); parents != p.Commit+" "+old+" "+head {
75 t.Errorf("the record commit's parents are %q", parents)
76 }
77 r, err := ReadRecord(ctx, w.g, p.Commit)
78 if err != nil {
79 t.Fatal(err)
80 }
81 v := r.ActiveVersion()
82 if v.N != 2 || HeadOf(v) != head || v.Title != "A title unlike its commit's subject" || v.Branch != "a" || v.Uploader != "restacker" {
83 t.Errorf("the new version is %+v", v)
84 }
85 ap := p.Push(ctx, w.g)
86 if ap.Err != nil || ap.Version != 2 || ap.Write.Push != Pushed || w.at(ref) != p.Commit {
87 t.Fatalf("the push: %+v; origin's record is at %s", ap, w.at(ref))
88 }
89}
90
91// A manual append keeps titling a version by its head's subject, and
92// takes a head anywhere on the branch.
93func TestManualAppendTitlesBySubject(t *testing.T) {
94 w := newWorld(t)
95 w.layer("b", "main")
96 w.readyTitled(idB, "b", "B's own title")
97 was := w.at("refs/heads/b")
98 head := w.ownerPushes("b")
99 w.ownerPushes("b")
100
101 ap := AppendVersion(context.Background(), w.g, Append{ID: idB, Branch: "b", Head: head, IfActive: was, Principal: "author"})
102 if ap.Err != nil || ap.Version != 2 {
103 t.Fatalf("append: %+v", ap)
104 }
105 if v := w.record(idB).ActiveVersion(); v.Title != "the owner's own work on b" || HeadOf(v) != head {
106 t.Errorf("the new version is %+v", v)
107 }
108}
109
110// A restack append records only the head the restack moved the branch to.
111// A branch the owner has pushed past that head is refused when the record
112// is built, and again when it is pushed, with nothing moved either time.
113func TestRestackAppendNeedsTheBranchAtItsHead(t *testing.T) {
114 w := newWorld(t)
115 w.layer("a", "main")
116 w.layer("b", "main")
117 w.ready(idA, "a")
118 w.ready(idB, "b")
119 w.moveMain("main.txt", "main\n")
120 ctx := context.Background()
121
122 fa := w.capture(Selection{ID: idA})
123 wasA := fa.Bottom().Head
124 headA := w.moveBranches(fa)[idA]
125 w.ownerPushes("a")
126 before := w.refs()
127 _, err := BuildRestack(ctx, w.g, RestackAppend{ID: idA, Head: headA, IfActive: wasA, Principal: "restacker"})
128 if err == nil || !strings.Contains(err.Error(), "not "+short(headA)) {
129 t.Fatalf("a restack append to a branch past its head: %v", err)
130 }
131 if after := w.refs(); after != before {
132 t.Errorf("a refused restack append moved something")
133 }
134
135 fb := w.capture(Selection{ID: idB})
136 wasB := fb.Bottom().Head
137 headB := w.moveBranches(fb)[idB]
138 p, err := BuildRestack(ctx, w.g, RestackAppend{ID: idB, Head: headB, IfActive: wasB, Principal: "restacker"})
139 if err != nil {
140 t.Fatal(err)
141 }
142 w.ownerPushes("b")
143 before = w.refs()
144 ap := p.Push(ctx, w.g)
145 if ap.Err == nil || ap.Version != 0 || ap.Write.Push != Unpushed || ap.Write.Commit != p.Commit || ap.Write.Old != p.Old {
146 t.Fatalf("a restack push to a branch moved since the build: %+v", ap)
147 }
148 if after := w.refs(); after != before {
149 t.Errorf("a refused restack push moved something")
150 }
151}
152
153// A restack append is kept and restored as recovery will keep and restore
154// it, and the restored write is the one kept, in the same mode. Once the
155// owner pushes past the head it records, the restored write's push is
156// refused, as the kept one's would be, and nothing moves. Restored while
157// the branch is still at that head, it pushes.
158func TestRestoredRestackStillNeedsTheBranchAtItsHead(t *testing.T) {
159 w := newWorld(t)
160 w.layer("a", "main")
161 w.layer("b", "main")
162 w.ready(idA, "a")
163 w.ready(idB, "b")
164 w.moveMain("main.txt", "main\n")
165 ctx := context.Background()
166 keep := func(id string) []byte {
167 t.Helper()
168 f := w.capture(Selection{ID: id})
169 p, err := BuildRestack(ctx, w.g, RestackAppend{ID: id, Head: w.moveBranches(f)[id], IfActive: f.Bottom().Head, Principal: "restacker"})
170 if err != nil {
171 t.Fatal(err)
172 }
173 data, err := json.Marshal(p)
174 if err != nil {
175 t.Fatal(err)
176 }
177 var q PendingVersion
178 if err := json.Unmarshal(data, &q); err != nil || !reflect.DeepEqual(&q, p) {
179 t.Fatalf("%s restores as %+v, %v; it was kept as %+v", data, q, err, *p)
180 }
181 return data
182 }
183 restored := func(data []byte) *PendingVersion {
184 t.Helper()
185 var p PendingVersion
186 if err := json.Unmarshal(data, &p); err != nil {
187 t.Fatal(err)
188 }
189 return &p
190 }
191
192 keptA, keptB := keep(idA), keep(idB)
193 w.ownerPushes("a")
194 before := w.refs()
195 ap := restored(keptA).Push(ctx, w.g)
196 if ap.Err == nil || ap.Version != 0 || ap.Write.Push != Unpushed {
197 t.Fatalf("a restored restack push to a branch the owner moved: %+v", ap)
198 }
199 if after := w.refs(); after != before {
200 t.Errorf("a refused restored restack push moved something")
201 }
202 p := restored(keptB)
203 if ap := p.Push(ctx, w.g); ap.Err != nil || ap.Version != 2 || w.at(change.Ref(Target, idB)) != p.Commit {
204 t.Fatalf("a restored restack push to a branch still at its head: %+v", ap)
205 }
206}
207
208// Only the form a write is kept in restores. A form missing anything the
209// push checks, holding something else, or not agreeing with itself is
210// refused, and so is a field named twice, which would otherwise let a
211// second mode overrule the first.
212func TestRestoreRefusesAMalformedWrite(t *testing.T) {
213 w := newWorld(t)
214 w.layer("a", "main")
215 w.ready(idA, "a")
216 w.moveMain("main.txt", "main\n")
217 ctx := context.Background()
218 f := w.capture(Selection{ID: idA})
219 p, err := BuildRestack(ctx, w.g, RestackAppend{ID: idA, Head: w.moveBranches(f)[idA], IfActive: f.Bottom().Head, Principal: "restacker"})
220 if err != nil {
221 t.Fatal(err)
222 }
223 data, err := json.Marshal(p)
224 if err != nil {
225 t.Fatal(err)
226 }
227 kept := string(data)
228 field := func(name, value string) string { return `"` + name + `":"` + value + `"` }
229 for what, form := range map[string]string{
230 "no mode": strings.Replace(kept, field("mode", "restack")+",", "", 1),
231 "an unknown mode": strings.Replace(kept, field("mode", "restack"), field("mode", "rebase"), 1),
232 "the mode twice": strings.Replace(kept, field("mode", "restack"), field("mode", "restack")+","+field("mode", "manual"), 1),
233 "an unknown field": strings.Replace(kept, `{"schema"`, `{"lease":"none","schema"`, 1),
234 "no record read": strings.Replace(kept, field("old", p.Old), field("old", ""), 1),
235 "no branch": strings.Replace(kept, field("branch", "a"), field("branch", ""), 1),
236 "a head it is not at": strings.Replace(kept, field("head", p.Head), field("head", p.Old), 1),
237 "another change's ref": strings.Replace(kept, field("ref", p.Ref), field("ref", change.Ref(Target, idB)), 1),
238 "a report's schema": strings.Replace(kept, pendingVersionSchema, pendingReportSchema, 1),
239 "a second form": kept + " " + kept,
240 } {
241 if form == kept {
242 t.Fatalf("%s: the edit changed nothing", what)
243 }
244 var q PendingVersion
245 if err := json.Unmarshal([]byte(form), &q); err == nil {
246 t.Errorf("%s restored: %+v", what, q)
247 }
248 }
249 var r PendingReport
250 if err := json.Unmarshal(data, &r); err == nil {
251 t.Errorf("a pending version restored as a report: %+v", r)
252 }
253}
254
255// A report kept and restored is the report written, and its push moves
256// the report ref to it. A kept report about another change than the one
257// whose ref it names is refused.
258func TestRestoredReportPushes(t *testing.T) {
259 w := newWorld(t)
260 w.layer("a", "main")
261 w.ready(idA, "a")
262 ctx := context.Background()
263 tip := change.HeadPrefix + w.at("refs/heads/a")
264 r := &change.RestackReport{
265 Schema: change.RestackSchema, ChangeID: idA, Target: Target,
266 TargetTip: change.HeadPrefix + w.at("refs/heads/main"), Operation: "test-1", Result: change.RestackBlocked,
267 Layers: []change.RestackLayer{{
268 ChangeID: idA, Record: change.HeadPrefix + w.at(change.Ref(Target, idA)), Version: 1,
269 Head: tip, Branch: "a", BranchTip: &tip,
270 }},
271 }
272 p, err := BuildReport(ctx, w.g, r, ReportMessage("kkkk", r), "")
273 if err != nil {
274 t.Fatal(err)
275 }
276 data, err := json.Marshal(p)
277 if err != nil {
278 t.Fatal(err)
279 }
280 var q PendingReport
281 if err := json.Unmarshal(data, &q); err != nil || !reflect.DeepEqual(&q, p) {
282 t.Fatalf("%s restores as %+v, %v", data, q, err)
283 }
284 if wr := q.Push(ctx, w.g); wr.Push != Pushed || w.at(RestackPrefix+idA) != p.Commit {
285 t.Fatalf("the restored report's push: %+v", wr)
286 }
287 other := strings.Replace(string(data), `"change_id":"`+idA+`","ref":"`+RestackPrefix+idA+`"`,
288 `"change_id":"`+idB+`","ref":"`+RestackPrefix+idB+`"`, 1)
289 if err := json.Unmarshal([]byte(other), &q); err == nil || !strings.Contains(err.Error(), "is about "+idA) {
290 t.Errorf("a report kept under another change's ref: %v", err)
291 }
292}
293
294// A report commit is written and then pushed: written, it holds the report
295// and is not on origin; pushed, origin's report ref names it. The next
296// report has the one it replaces as its parent, and a push leased on a
297// report that has moved is refused.
298func TestReportIsWrittenThenPushed(t *testing.T) {
299 w := newWorld(t)
300 w.layer("a", "main")
301 w.ready(idA, "a")
302 ctx := context.Background()
303 tip := change.HeadPrefix + w.at("refs/heads/a")
304 r := &change.RestackReport{
305 Schema: change.RestackSchema, ChangeID: idA, Target: Target,
306 TargetTip: change.HeadPrefix + w.at("refs/heads/main"), Operation: "test-1", Result: change.RestackBlocked,
307 Layers: []change.RestackLayer{{
308 ChangeID: idA, Record: change.HeadPrefix + w.at(change.Ref(Target, idA)), Version: 1,
309 Head: tip, Branch: "a", BranchTip: &tip,
310 }},
311 }
312 ref := RestackPrefix + idA
313
314 first, err := WriteReport(ctx, w.g, r, ReportMessage("kkkk", r), "")
315 if err != nil {
316 t.Fatal(err)
317 }
318 if w.at(ref) != "" {
319 t.Fatal("writing a report pushed it")
320 }
321 if parents := w.git("rev-list", "--parents", "-n", "1", first); parents != first {
322 t.Errorf("a change's first report has parents: %q", parents)
323 }
324 if got, err := ReadReport(ctx, w.g, first); err != nil || got.Operation != "test-1" || got.Result != change.RestackBlocked {
325 t.Fatalf("the report commit reads back as %+v, %v", got, err)
326 }
327 if wr := PushReport(ctx, w.g, idA, "", first); wr.Push != Pushed || w.at(ref) != first {
328 t.Fatalf("the first push: %+v", wr)
329 }
330
331 r.Operation = "test-2"
332 second, err := WriteReport(ctx, w.g, r, ReportMessage("kkkk", r), first)
333 if err != nil {
334 t.Fatal(err)
335 }
336 if parents := w.git("rev-list", "--parents", "-n", "1", second); parents != second+" "+first {
337 t.Errorf("a report's parents are %q", parents)
338 }
339 if wr := PushReport(ctx, w.g, idA, "", second); wr.Push != Refused || w.at(ref) != first {
340 t.Errorf("a push leased on no report, where there is one: %+v", wr)
341 }
342 if wr := PushReport(ctx, w.g, idA, first, second); wr.Push != Pushed || w.at(ref) != second {
343 t.Errorf("the second push: %+v", wr)
344 }
345}
346
347// Release runs under a context that is already done, and for no longer
348// than its bound: a hook that never returns while the kept refs are
349// deleted is killed with everything it started.
350func TestReleaseEndsWithinItsBound(t *testing.T) {
351 w := newWorld(t)
352 w.layer("a", "main")
353 w.ready(idA, "a")
354 w.moveMain("main.txt", "main\n")
355 p, err := Prepare(context.Background(), w.g, w.capture(Selection{ID: idA}))
356 if err != nil {
357 t.Fatal(err)
358 }
359 guard := "[ \"$1\" = prepared ] || exit 0\ncase $(cat) in *' " + keepPrefix + "'*) ;; *) exit 0 ;; esac"
360 b := newBlocker(t, filepath.Join(w.work, ".git", "hooks", "reference-transaction"), guard)
361 defer func(was time.Duration) { cleanupBound = was }(cleanupBound)
362 cleanupBound = 2 * time.Second
363
364 ctx, cancel := context.WithCancel(context.Background())
365 cancel()
366 start := time.Now()
367 done := make(chan error, 1)
368 go func() { done <- p.Release(ctx, w.g) }()
369 select {
370 case err := <-done:
371 if err == nil {
372 t.Error("a release whose refs were never deleted says it released them")
373 }
374 case <-time.After(30 * time.Second):
375 t.Fatal("release did not return")
376 }
377 if took := time.Since(start); took > cleanupBound+waitDelay+5*time.Second {
378 t.Errorf("release took %v, with a bound of %v", took, cleanupBound)
379 }
380 b.wait(t)
381 if b.beating() {
382 t.Error("the hook outlived the bound")
383 }
384}
385
386// An unattended command that reaches origin is killed whole when its
387// context's deadline passes: git and the upload-pack it started for a
388// fetch that never answers.
389func TestUnattendedFetchEndsAtTheDeadline(t *testing.T) {
390 w := newWorld(t)
391 b := newBlocker(t, filepath.Join(t.TempDir(), "upload-pack"), "")
392 w.git("config", "remote.origin.uploadpack", b.script)
393 g := w.g
394 g.Unattended = true
395 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
396 defer cancel()
397
398 start := time.Now()
399 _, err := ListOrigin(ctx, g, "refs/heads/*")
400 if err == nil {
401 t.Fatal("a fetch that never answered read origin")
402 }
403 if took := time.Since(start); took > 2*time.Second+waitDelay+5*time.Second {
404 t.Errorf("the read took %v, with a deadline of 2s", took)
405 }
406 if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
407 t.Errorf("the read ended before its deadline: %v", err)
408 }
409 b.wait(t)
410 if b.beating() {
411 t.Error("the upload-pack git started outlived the deadline")
412 }
413}