oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package forge
2
3import (
4 "context"
5 "errors"
6 "strings"
7 "testing"
8)
9
10// The first merge into an empty repository, whose default branch is
11// unborn (no tip, no advertised name). Today's hub refuses it:
12// unborn_default, or is_default once a rebuild pointed its HEAD at the
13// task branch. That is ErrMergeUnbornDefault, final for the request:
14// one request, no blind retry. "moved" while the default is still unborn
15// is the same; once someone created the default meanwhile it is an
16// ordinary refusal, and the next merge is an ordinary one.
17func TestMergeBranchUnbornDefaultBeforeTheHubUpdate(t *testing.T) {
18 commit := strings.Repeat("a", 40)
19 for _, c := range []MergeResult{{Status: 409, Reason: "unborn_default"}, {Status: 400, Reason: "is_default"}, {Status: 409, Reason: "moved"}} {
20 h := &fakeHub{head: commit, unborn: true, answer: func(*fakeHub, MergeRequest) (MergeResult, error) { return c, nil }}
21 m := newMerge(t, h)
22 if _, err := MergeBranch(context.Background(), m); !errors.Is(err, ErrMergeUnbornDefault) || len(h.requests) != 1 {
23 t.Fatalf("%+v: %v (%d requests)", c, err, len(h.requests))
24 }
25 if rec, ok, _ := m.load(); !ok || rec.Phase != mergeRefusedPh || rec.Reason != c.Reason {
26 t.Fatalf("%+v: record %+v", c, rec)
27 }
28 }
29 h := &fakeHub{head: commit, unborn: true, answer: func(h *fakeHub, _ MergeRequest) (MergeResult, error) {
30 h.unborn = false // someone created the default branch meanwhile
31 return MergeResult{Status: 409, Reason: "moved"}, nil
32 }}
33 if _, err := MergeBranch(context.Background(), newMerge(t, h)); !errors.Is(err, ErrMergeFailed) || errors.Is(err, ErrMergeUnbornDefault) {
34 t.Fatalf("default created meanwhile: %v", err)
35 }
36}
37
38// After the hub update the first merge creates the default branch at
39// the reviewed tip: 200, merged, the SHA is the tip itself (no merge
40// commit). A second call reads the record, never asks again.
41func TestMergeBranchUnbornDefaultAfterTheHubUpdate(t *testing.T) {
42 commit := strings.Repeat("a", 40)
43 h := &fakeHub{head: commit, unborn: true, answer: func(h *fakeHub, req MergeRequest) (MergeResult, error) {
44 if h.head != req.ExpectSHA {
45 return MergeResult{Status: 409, Reason: "moved"}, nil
46 }
47 h.unborn, h.landed, h.merge = false, true, req.ExpectSHA
48 return MergeResult{Status: 200, Merged: true, SHA: req.ExpectSHA, Into: "main"}, nil
49 }}
50 m := newMerge(t, h)
51 out, err := MergeBranch(context.Background(), m)
52 if err != nil || out.SHA != commit || out.Into != "main" || out.Source != SourceHub || len(h.requests) != 1 {
53 t.Fatalf("first merge %+v %v (%d requests)", out, err, len(h.requests))
54 }
55 if again, err := MergeBranch(context.Background(), m); err != nil || again.SHA != commit || len(h.requests) != 1 {
56 t.Fatalf("again %+v %v", again, err)
57 }
58 // A branch that moved since it was judged is never merged, unborn or not.
59 h2 := &fakeHub{head: strings.Repeat("b", 40), unborn: true, answer: h.answer}
60 if _, err := MergeBranch(context.Background(), newMerge(t, h2)); !errors.Is(err, ErrMergeMoved) || len(h2.requests) != 0 {
61 t.Fatalf("moved: %v", err)
62 }
63}
64
65// The hub since 53d8125 keeps the source branch on the first merge and
66// says so (branch_closed false); an answer without "into" still lands in
67// the unborn default's name, main. The kept branch then reads as merged:
68// the next call is answered by the record, never a second request.
69func TestMergeBranchFirstMergeKeepsTheBranch(t *testing.T) {
70 commit := strings.Repeat("a", 40)
71 h := &fakeHub{head: commit, unborn: true, answer: func(h *fakeHub, req MergeRequest) (MergeResult, error) {
72 h.unborn, h.landed, h.merge = false, true, req.ExpectSHA
73 return MergeResult{Status: 200, Merged: true, SHA: req.ExpectSHA, BranchClosed: false}, nil
74 }}
75 m := newMerge(t, h)
76 out, err := MergeBranch(context.Background(), m)
77 if err != nil || out.SHA != commit || out.Into != "main" || out.Source != SourceHub {
78 t.Fatalf("first merge %+v %v", out, err)
79 }
80 if h.head != commit {
81 t.Fatalf("the branch was not kept: %q", h.head)
82 }
83 if again, err := MergeBranch(context.Background(), m); err != nil || again.SHA != commit || again.Into != "main" || len(h.requests) != 1 {
84 t.Fatalf("again %+v %v (%d requests)", again, err, len(h.requests))
85 }
86}
87