package thin import ( "context" "os" "os/exec" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // TestEmptyRepoFixRoundPublishes: in a repository with no commits, the // first round pushes the task branch's initial commit, and a fix round // (its base the task branch's tip, the default branch still unborn) // pushes again as a fast-forward of that tip (the remote refuses any // other), with no push_failed; the default branch stays unborn. func TestEmptyRepoFixRoundPublishes(t *testing.T) { old := pushBackoff pushBackoff = time.Millisecond t.Cleanup(func() { pushBackoff = old }) e, f, r, _ := hubRig(t) dir := filepath.Dir(f.dir) // An empty repository: no refs at all, HEAD naming an unborn main, // and only fast-forwards accepted. empty := filepath.Join(dir, "empty.git") gitOut(t, dir, "init", "-q", "--bare", "-b", "main", empty) gitOut(t, dir, "--git-dir="+empty, "config", "receive.denyNonFastForwards", "true") f.dir = empty r.Specialist, r.Branch = wire.SpecialistEngineering, "equipo/tk1-salon" work := filepath.Join(dir, "salon") os.MkdirAll(work, 0700) commit := func(gitDir, file, text string, parents ...string) string { env := []string{"--git-dir=" + gitDir, "--work-tree=" + work} os.WriteFile(filepath.Join(work, file), []byte(text), 0600) gitOut(t, work, append(env, "add", "-A")...) tree := gitOut(t, work, append(env, "write-tree")...) args := append(env, "commit-tree", tree, "-m", file) for _, p := range parents { args = append(args, "-p", p) } return gitOut(t, work, args...) } // Round 1: no base, the repository's initial commit. r1 := r r1.StepID, r1.Base = "s1", "" git1 := e.tree(r1, "").GitDir gitOut(t, dir, "init", "-q", "--bare", git1) r1.Candidate = commit(git1, "index.html", "

Salon

\n") if rep := e.publish(context.Background(), r1, wire.StepReport{}); !rep.Pushed || rep.State != wire.StepDone { t.Fatalf("round 1: %+v", rep) } // Round 2: the base is the task branch's tip; main is still unborn. r2 := r r2.StepID, r2.Base = "s2", r1.Candidate git2 := e.tree(r2, r2.Base).GitDir gitOut(t, dir, "clone", "-q", "--bare", git1, git2) r2.Candidate = commit(git2, "hours.txt", "open late\n", r1.Candidate) rep := e.publish(context.Background(), r2, wire.StepReport{}) if !rep.Pushed || rep.State != wire.StepDone || rep.Reason != "" { t.Fatalf("round 2: %+v", rep) } if tip := gitOut(t, dir, "--git-dir="+empty, "rev-parse", "refs/heads/"+r.Branch); tip != r2.Candidate { t.Fatalf("task branch at %s, want %s", tip, r2.Candidate) } if parent := gitOut(t, dir, "--git-dir="+empty, "rev-parse", r2.Candidate+"^"); parent != r1.Candidate { t.Fatalf("round 2's parent %s, want round 1's %s", parent, r1.Candidate) } if out, _ := exec.Command("git", "--git-dir="+empty, "rev-parse", "--verify", "--quiet", "refs/heads/main").Output(); len(out) > 0 { t.Fatalf("main was created at %s", out) } }