| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // TestEmptyRepoFixRoundPublishes: in a repository with no commits, the |
| 15 | // first round pushes the task branch's initial commit, and a fix round |
| 16 | // (its base the task branch's tip, the default branch still unborn) |
| 17 | // pushes again as a fast-forward of that tip (the remote refuses any |
| 18 | // other), with no push_failed; the default branch stays unborn. |
| 19 | func TestEmptyRepoFixRoundPublishes(t *testing.T) { |
| 20 | old := pushBackoff |
| 21 | pushBackoff = time.Millisecond |
| 22 | t.Cleanup(func() { pushBackoff = old }) |
| 23 | e, f, r, _ := hubRig(t) |
| 24 | dir := filepath.Dir(f.dir) |
| 25 | // An empty repository: no refs at all, HEAD naming an unborn main, |
| 26 | // and only fast-forwards accepted. |
| 27 | empty := filepath.Join(dir, "empty.git") |
| 28 | gitOut(t, dir, "init", "-q", "--bare", "-b", "main", empty) |
| 29 | gitOut(t, dir, "--git-dir="+empty, "config", "receive.denyNonFastForwards", "true") |
| 30 | f.dir = empty |
| 31 | r.Specialist, r.Branch = wire.SpecialistEngineering, "equipo/tk1-salon" |
| 32 | work := filepath.Join(dir, "salon") |
| 33 | os.MkdirAll(work, 0700) |
| 34 | commit := func(gitDir, file, text string, parents ...string) string { |
| 35 | env := []string{"--git-dir=" + gitDir, "--work-tree=" + work} |
| 36 | os.WriteFile(filepath.Join(work, file), []byte(text), 0600) |
| 37 | gitOut(t, work, append(env, "add", "-A")...) |
| 38 | tree := gitOut(t, work, append(env, "write-tree")...) |
| 39 | args := append(env, "commit-tree", tree, "-m", file) |
| 40 | for _, p := range parents { |
| 41 | args = append(args, "-p", p) |
| 42 | } |
| 43 | return gitOut(t, work, args...) |
| 44 | } |
| 45 | |
| 46 | // Round 1: no base, the repository's initial commit. |
| 47 | r1 := r |
| 48 | r1.StepID, r1.Base = "s1", "" |
| 49 | git1 := e.tree(r1, "").GitDir |
| 50 | gitOut(t, dir, "init", "-q", "--bare", git1) |
| 51 | r1.Candidate = commit(git1, "index.html", "<h1>Salon</h1>\n") |
| 52 | if rep := e.publish(context.Background(), r1, wire.StepReport{}); !rep.Pushed || rep.State != wire.StepDone { |
| 53 | t.Fatalf("round 1: %+v", rep) |
| 54 | } |
| 55 | |
| 56 | // Round 2: the base is the task branch's tip; main is still unborn. |
| 57 | r2 := r |
| 58 | r2.StepID, r2.Base = "s2", r1.Candidate |
| 59 | git2 := e.tree(r2, r2.Base).GitDir |
| 60 | gitOut(t, dir, "clone", "-q", "--bare", git1, git2) |
| 61 | r2.Candidate = commit(git2, "hours.txt", "open late\n", r1.Candidate) |
| 62 | rep := e.publish(context.Background(), r2, wire.StepReport{}) |
| 63 | if !rep.Pushed || rep.State != wire.StepDone || rep.Reason != "" { |
| 64 | t.Fatalf("round 2: %+v", rep) |
| 65 | } |
| 66 | if tip := gitOut(t, dir, "--git-dir="+empty, "rev-parse", "refs/heads/"+r.Branch); tip != r2.Candidate { |
| 67 | t.Fatalf("task branch at %s, want %s", tip, r2.Candidate) |
| 68 | } |
| 69 | if parent := gitOut(t, dir, "--git-dir="+empty, "rev-parse", r2.Candidate+"^"); parent != r1.Candidate { |
| 70 | t.Fatalf("round 2's parent %s, want round 1's %s", parent, r1.Candidate) |
| 71 | } |
| 72 | if out, _ := exec.Command("git", "--git-dir="+empty, "rev-parse", "--verify", "--quiet", "refs/heads/main").Output(); len(out) > 0 { |
| 73 | t.Fatalf("main was created at %s", out) |
| 74 | } |
| 75 | } |
| 76 | |