gunk-dev/the-valley

snapshot_test.go

sourcec1620ef8763amain's published tip history
5.1 KiB
1package rebase
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strconv"
10 "strings"
11 "testing"
12)
13
14// A snapshot whose fetch fails because main moved after the listing reads
15// origin again. Here origin rewrites main, and prunes its old tip, as the
16// snapshot fetches. Once, and the read made again finds main at its new
17// tip. On every fetch, and the read ends with the fetch's error after
18// snapshotTries listings.
19func TestASnapshotReadsAgainWhenMainMovesUnderIt(t *testing.T) {
20 for _, c := range []struct {
21 name string
22 races int
23 }{{"once", 1}, {"every time", snapshotTries}} {
24 t.Run(c.name, func(t *testing.T) {
25 w := newWorld(t)
26 w.moveMain("main.txt", "main\n")
27 listed := w.at("refs/heads/main")
28 reader := filepath.Join(t.TempDir(), "reader")
29 w.run(filepath.Dir(reader), "init", "--quiet", reader)
30 w.run(reader, "remote", "add", "origin", w.origin)
31 calls := filepath.Join(t.TempDir(), "calls")
32 w.run(reader, "config", "remote.origin.uploadpack", racing(t, calls, c.races))
33
34 s, err := ReadSnapshot(context.Background(), Git{Dir: reader, Env: w.env}, Verifier{})
35 data, _ := os.ReadFile(calls)
36 n, _ := strconv.Atoi(strings.TrimSpace(string(data)))
37 if c.races == snapshotTries {
38 if err == nil || n != 2*snapshotTries {
39 t.Errorf("a read that raced main every time ended with %v, after %d calls to origin", err, n)
40 }
41 return
42 }
43 if err != nil {
44 t.Fatalf("the read did not survive main moving under it: %v", err)
45 }
46 if now := w.at("refs/heads/main"); s.Tip != now || now == listed || n != 4 {
47 t.Errorf("the snapshot has main at %s, origin at %s, after %d calls to origin", short(s.Tip), short(now), n)
48 }
49 })
50 }
51}
52
53// racing is an upload-pack that rewrites origin's main, and prunes its old
54// tip, before each of the first races fetches it serves. A snapshot lists
55// origin and then fetches, so every second call is a fetch. It counts its
56// calls in the file given.
57func racing(t *testing.T, calls string, races int) string {
58 t.Helper()
59 path := filepath.Join(t.TempDir(), "racing-upload-pack")
60 script := fmt.Sprintf(`#!/bin/sh
61n=$(( $(cat '%[1]s' 2>/dev/null || echo 0) + 1 ))
62echo "$n" > '%[1]s'
63if [ $((n %% 2)) -eq 0 ] && [ $((n / 2)) -le %[2]d ]; then
64 tree=$(git -C "$1" rev-parse 'refs/heads/main^{tree}')
65 new=$(git -C "$1" commit-tree "$tree" -m "main rewritten, call $n")
66 git -C "$1" update-ref refs/heads/main "$new"
67 git -C "$1" gc --quiet --prune=now
68fi
69exec git upload-pack "$@"
70`, calls, races)
71 if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
72 t.Fatal(err)
73 }
74 return path
75}
76
77// A snapshot answers whether one commit is an ancestor of another as git
78// does, for every pair, from one read of main's history and each head's
79// own commits, without asking git about any pair. Main here holds a
80// merge. One branch forks below it, one from the side it merged, one from
81// main's tip, and one merges main back in, so its history meets main's at
82// two commits.
83func TestAncestryIsAnsweredAsGitAnswersIt(t *testing.T) {
84 w := newWorld(t)
85 base := w.git("rev-parse", "HEAD")
86 m1 := w.commit("m1", "main.txt", "1\n")
87 w.git("checkout", "--quiet", "-b", "side", base)
88 s1 := w.commit("s1", "side.txt", "s\n")
89 w.git("checkout", "--quiet", "main")
90 w.git("merge", "--quiet", "--no-ff", "-m", "merge side", "side")
91 merge := w.git("rev-parse", "HEAD")
92 m2 := w.commit("m2", "main.txt", "2\n")
93 branch := func(name, from string) string {
94 w.git("checkout", "--quiet", "-b", name, from)
95 return w.commit(name, name+".txt", name+"\n")
96 }
97 x1 := branch("x", m1)
98 x2 := w.commit("x2", "x.txt", "x2\n")
99 y := branch("y", m2)
100 s2 := branch("s", s1)
101 w.git("checkout", "--quiet", "-b", "z", x2)
102 w.git("merge", "--quiet", "--no-ff", "-m", "merge main", m2)
103 z := w.git("rev-parse", "HEAD")
104 w.git("push", "--quiet", "origin", "main", "x", "y", "s", "z")
105
106 commits := []string{base, m1, s1, merge, m2, x1, x2, y, s2, z}
107 want := map[[2]string]bool{}
108 for _, a := range commits {
109 for _, b := range commits {
110 want[[2]string{a, b}] = w.ancestor(a, b)
111 }
112 }
113
114 // Every git the snapshot starts goes through a shim that logs it.
115 real, err := exec.LookPath("git")
116 if err != nil {
117 t.Fatal(err)
118 }
119 shim, calls := t.TempDir(), filepath.Join(t.TempDir(), "calls")
120 script := fmt.Sprintf("#!/bin/sh\necho \"$*\" >> '%s'\nexec '%s' \"$@\"\n", calls, real)
121 if err := os.WriteFile(filepath.Join(shim, "git"), []byte(script), 0o755); err != nil {
122 t.Fatal(err)
123 }
124 t.Setenv("PATH", shim+string(os.PathListSeparator)+os.Getenv("PATH"))
125
126 s, err := ReadSnapshot(context.Background(), w.g, Verifier{})
127 if err != nil {
128 t.Fatal(err)
129 }
130 for _, a := range commits {
131 if got := s.Reaches(a); got != want[[2]string{a, m2}] {
132 t.Errorf("Reaches(%s) is %v, and git says %v", short(a), got, !got)
133 }
134 for _, b := range commits {
135 if got := s.StrictAncestor(a, b); got != (a != b && want[[2]string{a, b}]) {
136 t.Errorf("StrictAncestor(%s, %s) is %v, and git says %v", short(a), short(b), got, !got)
137 }
138 }
139 }
140 log, _ := os.ReadFile(calls)
141 if n := strings.Count(string(log), "merge-base --is-ancestor"); n != 0 {
142 t.Errorf("the snapshot asked git about %d pairs:\n%s", n, log)
143 }
144}