gunk-dev/the-valley

rebase_test.go

source17cde4c788e5main's published tip history
6.8 KiB
1package main
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "syscall"
11 "testing"
12 "time"
13)
14
15// A SIGTERM while a hook blocks the rebase ends it: rebase exits 130 at
16// once, though the post-checkout hook worktree add runs holds the stderr
17// the verb was given. The hook is killed with the git that ran it, and the
18// rebase leaves no worktree, no ref and no temporary directory, and moves
19// nothing on origin.
20func TestRebaseSignalledWhileAHookBlocks(t *testing.T) {
21 dir := t.TempDir()
22 t.Setenv("HOME", dir)
23 t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
24 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
25 t.Setenv("VALLEY_KNOWN_SIGNERS", "")
26 origin, work := filepath.Join(dir, "origin.git"), filepath.Join(dir, "work")
27 git := func(dir string, args ...string) string {
28 t.Helper()
29 cmd := exec.Command("git", args...)
30 cmd.Dir = dir
31 out, err := cmd.CombinedOutput()
32 if err != nil {
33 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
34 }
35 return strings.TrimSpace(string(out))
36 }
37 git(dir, "init", "--quiet", "--bare", "--initial-branch=main", origin)
38 git(dir, "clone", "--quiet", origin, work)
39 git(work, "config", "user.name", "rebase-test")
40 git(work, "config", "user.email", "rebase-test@localhost")
41 git(work, "config", "valley.principal", "rebaser")
42 git(work, "checkout", "--quiet", "-b", "main")
43 git(work, "commit", "--quiet", "--allow-empty", "-m", "base")
44 git(work, "push", "--quiet", "origin", "main")
45 git(work, "checkout", "--quiet", "-b", "one")
46 git(work, "commit", "--quiet", "--allow-empty", "-m", "layer one")
47 git(work, "push", "--quiet", "origin", "one")
48 t.Chdir(work)
49 id := strings.Repeat("q", 32)
50 var stdout, stderr bytes.Buffer
51 if code := run([]string{"ready", "--change", id, "one"}, &stdout, &stderr); code != 0 {
52 t.Fatalf("ready: %s", stderr.String())
53 }
54 git(work, "checkout", "--quiet", "main")
55 git(work, "commit", "--quiet", "--allow-empty", "-m", "main moves on")
56 git(work, "push", "--quiet", "origin", "main")
57 before := git(origin, "for-each-ref")
58
59 // The hook says it has started, and then beats for a minute: it adds
60 // a line to a file every tenth of a second.
61 started, beats := filepath.Join(dir, "started"), filepath.Join(dir, "beats")
62 hook := fmt.Sprintf("#!/bin/sh\n[ -e '%s' ] && exit 0\n: > '%s'\n"+
63 "i=0\nwhile [ $i -lt 600 ]; do echo >> '%s'; sleep 0.1; i=$((i+1)); done\n", started, started, beats)
64 if err := os.WriteFile(filepath.Join(work, ".git", "hooks", "post-checkout"), []byte(hook), 0o755); err != nil {
65 t.Fatal(err)
66 }
67 tmp := filepath.Join(dir, "tmp")
68 if err := os.Mkdir(tmp, 0o755); err != nil {
69 t.Fatal(err)
70 }
71 t.Setenv("TMPDIR", tmp)
72
73 stdout.Reset()
74 stderr.Reset()
75 exited := make(chan int, 1)
76 go func() { exited <- run([]string{"rebase", id}, &stdout, &stderr) }()
77 for deadline := time.Now().Add(30 * time.Second); ; time.Sleep(20 * time.Millisecond) {
78 if _, err := os.Stat(started); err == nil {
79 break
80 }
81 if time.Now().After(deadline) {
82 t.Fatal("the hook never started")
83 }
84 }
85 self, _ := os.FindProcess(os.Getpid())
86 if err := self.Signal(syscall.SIGTERM); err != nil {
87 t.Fatal(err)
88 }
89 select {
90 case code := <-exited:
91 if code != 130 {
92 t.Fatalf("rebase exited %d, not 130:\n%s%s", code, stdout.String(), stderr.String())
93 }
94 case <-time.After(30 * time.Second):
95 t.Fatal("rebase did not end on SIGTERM")
96 }
97
98 size := func() int64 {
99 fi, err := os.Stat(beats)
100 if err != nil {
101 return 0
102 }
103 return fi.Size()
104 }
105 time.Sleep(200 * time.Millisecond)
106 was := size()
107 time.Sleep(time.Second)
108 if size() != was {
109 t.Error("the hook outlived the rebase")
110 }
111 if kept := git(work, "for-each-ref", "refs/valley-rebase/"); kept != "" {
112 t.Errorf("the rebase left a ref behind: %q", kept)
113 }
114 if out := git(work, "worktree", "list", "--porcelain"); strings.Count(out, "worktree ") != 1 {
115 t.Errorf("the rebase left a worktree registered:\n%s", out)
116 }
117 if left, _ := os.ReadDir(tmp); len(left) != 0 {
118 t.Errorf("the rebase left its worktree's directory: %s", left[0].Name())
119 }
120 if after := git(origin, "for-each-ref"); after != before {
121 t.Errorf("an interrupted rebase moved something:\n%s\nnow:\n%s", before, after)
122 }
123}
124
125// The manifest lines up the layers' names, says for each change whether a
126// version was added, and gives the command that finishes each change whose
127// branch may hold its rebased head with no version recording it: one
128// pushed and left without a version, or one whose push may have reached
129// origin. A plain branch is never unfinished, and neither is a change
130// whose branch did not move.
131func TestManifest(t *testing.T) {
132 const (
133 o1, n1 = "1111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "1111111bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
134 o2, n2 = "2222222aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "2222222bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
135 o3, n3 = "3333333aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "3333333bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
136 id2 = "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
137 id3 = "pppppppppppppppppppppppppppppppp"
138 )
139 stack := func(pushed string, added2, added3 int) []*layer {
140 return []*layer{
141 {name: "topic/one", branch: "topic/one", head: o1, rebased: n1, pushed: pushed},
142 {name: "qqqq", id: id2, branch: "two", head: o2, rebased: n2, pushed: pushed, added: added2},
143 {name: "pppp", id: id3, branch: "my branch", head: o3, rebased: n3, pushed: pushed, added: added3},
144 }
145 }
146 for _, c := range []struct {
147 name string
148 layers []*layer
149 want string
150 }{
151 {"finished", stack(pushDone, 2, 4), `the stack under pppp, bottom first:
152 topic/one 1111111 -> 1111111 origin/topic/one pushed
153 qqqq 2222222 -> 2222222 origin/two pushed, v2 added
154 pppp 3333333 -> 3333333 origin/my branch pushed, v4 added
155`},
156 {"one change left without a version", stack(pushDone, 0, 4), `the stack under pppp, bottom first:
157 topic/one 1111111 -> 1111111 origin/topic/one pushed
158 qqqq 2222222 -> 2222222 origin/two pushed, no version added
159 pppp 3333333 -> 3333333 origin/my branch pushed, v4 added
160to finish qqqq: valley ready --head ` + n2 + ` --change ` + id2 + ` two
161`},
162 {"refused", stack(pushNot, 0, 0), `the stack under pppp, bottom first:
163 topic/one 1111111 -> 1111111 origin/topic/one not pushed
164 qqqq 2222222 -> 2222222 origin/two not pushed, no version added
165 pppp 3333333 -> 3333333 origin/my branch not pushed, no version added
166`},
167 {"not known", stack(pushUnknown, 0, 0), `the stack under pppp, bottom first:
168 topic/one 1111111 -> 1111111 origin/topic/one may have moved
169 qqqq 2222222 -> 2222222 origin/two may have moved, no version added
170 pppp 3333333 -> 3333333 origin/my branch may have moved, no version added
171to finish qqqq: valley ready --head ` + n2 + ` --change ` + id2 + ` two
172to finish pppp: valley ready --head ` + n3 + ` --change ` + id3 + ` my\ branch
173`},
174 } {
175 if got := manifest("pppp", c.layers); got != c.want {
176 t.Errorf("%s:\n%s\nwant:\n%s", c.name, got, c.want)
177 }
178 }
179}