gunk-dev/the-valley

compare_test.go

source885985671589main's published tip history
11.7 KiB
1package main
2
3import (
4 "bytes"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strconv"
9 "strings"
10 "testing"
11)
12
13// compareRepo is an origin and a clone of it, for compare's tests.
14type compareRepo struct {
15 t *testing.T
16 origin, work string
17}
18
19func newCompareRepo(t *testing.T) *compareRepo {
20 t.Helper()
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 r := &compareRepo{t: t, origin: filepath.Join(dir, "origin.git"), work: filepath.Join(dir, "work")}
27 r.in(dir, "init", "--quiet", "--bare", "--initial-branch=main", r.origin)
28 r.in(dir, "clone", "--quiet", r.origin, r.work)
29 r.git("config", "user.name", "compare-test")
30 r.git("config", "user.email", "compare-test@localhost")
31 r.git("config", "valley.principal", "klaus-env")
32 r.git("checkout", "--quiet", "-b", "main")
33 r.file("README", "base\n")
34 r.git("commit", "--quiet", "-m", "base")
35 r.git("push", "--quiet", "origin", "main")
36 t.Chdir(r.work)
37 return r
38}
39
40func (r *compareRepo) in(dir string, args ...string) string {
41 r.t.Helper()
42 cmd := exec.Command("git", args...)
43 cmd.Dir = dir
44 var stderr bytes.Buffer
45 cmd.Stderr = &stderr
46 out, err := cmd.Output()
47 if err != nil {
48 r.t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, stderr.String())
49 }
50 return strings.TrimSpace(string(out))
51}
52
53func (r *compareRepo) git(args ...string) string {
54 r.t.Helper()
55 return r.in(r.work, args...)
56}
57
58// file writes a file in the work tree and stages it.
59func (r *compareRepo) file(name, content string) {
60 r.t.Helper()
61 if err := os.WriteFile(filepath.Join(r.work, name), []byte(content), 0o644); err != nil {
62 r.t.Fatal(err)
63 }
64 r.git("add", name)
65}
66
67// valley runs a verb of valley-go and returns what it printed.
68func (r *compareRepo) valley(args ...string) string {
69 r.t.Helper()
70 var stdout, stderr bytes.Buffer
71 if code := run(args, &stdout, &stderr); code != 0 {
72 r.t.Fatalf("valley-go %s: %s", strings.Join(args, " "), stderr.String())
73 }
74 return stdout.String()
75}
76
77// moveMain adds a commit to main on origin, as a landing would, and
78// fetches it. It returns main's new tip.
79func (r *compareRepo) moveMain(name string) string {
80 r.git("checkout", "--quiet", "main")
81 r.file(name, name+"\n")
82 r.git("commit", "--quiet", "-m", "land "+name)
83 r.git("push", "--quiet", "origin", "main")
84 return r.git("rev-parse", "HEAD")
85}
86
87// rebaseOnto rebases a branch onto main and pushes it, and returns its
88// new head.
89func (r *compareRepo) rebaseOnto(branch string) string {
90 r.git("checkout", "--quiet", branch)
91 r.git("rebase", "--quiet", "main")
92 r.git("push", "--quiet", "--force", "origin", branch)
93 r.git("checkout", "--quiet", "main")
94 return r.git("rev-parse", branch)
95}
96
97// setRef points a ref on origin at an object, or deletes it for "".
98func (r *compareRepo) setRef(ref, oid string) {
99 if oid == "" {
100 r.in(r.origin, "update-ref", "-d", ref)
101 return
102 }
103 r.in(r.origin, "update-ref", ref, oid)
104}
105
106// outcomeRecord writes an outcome record on origin, as the integrator
107// writes one, and returns its blob.
108func (r *compareRepo) outcomeRecord(head, decision string) string {
109 cmd := exec.Command("git", "hash-object", "-w", "--stdin")
110 cmd.Dir = r.origin
111 cmd.Stdin = strings.NewReader(head + " " + r.git("rev-parse", "main") + " 0123456789abcdef " + decision + "\n")
112 out, err := cmd.Output()
113 if err != nil {
114 r.t.Fatal(err)
115 }
116 return strings.TrimSpace(string(out))
117}
118
119func (r *compareRepo) compare(id string, version int, head string) (kind, text string) {
120 r.t.Helper()
121 out := r.valley("compare", "--tip", r.git("rev-parse", "origin/main"), id, strconv.Itoa(version), head)
122 kind, text, _ = strings.Cut(out, "\n")
123 return kind, text
124}
125
126// A request or an outcome under a branch's request name is evidence that
127// a head was approved only while nothing else maps to that name: no
128// version of another change, and no branch on origin that is not the
129// change's own. Under c-<change ID> it always is. An ambiguous name stops
130// the search: an older version is not compared instead.
131func TestTheApprovedVersionIsNamedUnambiguously(t *testing.T) {
132 r := newCompareRepo(t)
133 id := strings.Repeat("x", 32)
134 r.git("checkout", "--quiet", "-b", "a/b")
135 r.file("feature.txt", "feature\n")
136 r.git("commit", "--quiet", "-m", "add the feature")
137 r.git("push", "--quiet", "origin", "a/b")
138 r.valley("ready", "--change", id, "a/b")
139 v1 := r.git("rev-parse", "a/b")
140 byBranch := requestPrefix + "a-b"
141 r.setRef(byBranch, v1)
142 r.moveMain("other")
143 v2 := r.rebaseOnto("a/b")
144 r.valley("ready", "--change", id, "a/b")
145 r.git("fetch", "--quiet", "origin")
146
147 clean := func(why string) {
148 t.Helper()
149 if kind, text := r.compare(id, 2, v2); kind != compareClean || !strings.Contains(text, "clean restack of approved v1 at "+short(v1)) {
150 t.Fatalf("%s: %s\n%s", why, kind, text)
151 }
152 }
153 unknown := func(why, ref string) {
154 t.Helper()
155 kind, text := r.compare(id, 2, v2)
156 if kind != compareNone || !strings.Contains(text, "is named by "+ref+", and another change or branch maps to that name too") {
157 t.Fatalf("%s: %s\n%s", why, kind, text)
158 }
159 }
160 clean("a request under the branch's name")
161
162 // A plain branch on origin whose name maps to a-b as well.
163 r.git("push", "--quiet", "origin", "main:refs/heads/a-b")
164 unknown("a request under a name a plain branch maps to", byBranch)
165 r.setRef("refs/heads/a-b", "")
166 clean("a request once the plain branch is gone")
167
168 // Another change, whose version names a branch that maps to a-b.
169 r.git("checkout", "--quiet", "-b", "a-b", "main")
170 r.file("y.txt", "y\n")
171 r.git("commit", "--quiet", "-m", "another change")
172 r.git("push", "--quiet", "origin", "a-b")
173 r.valley("ready", "--change", strings.Repeat("y", 32), "a-b")
174 r.git("checkout", "--quiet", "main")
175 r.setRef("refs/heads/a-b", "")
176 unknown("a request under a name another change's version maps to", byBranch)
177
178 // The outcome of the same request, after the integrator consumed it:
179 // ambiguous under a-b, and evidence under c-<change ID>.
180 r.setRef(byBranch, "")
181 r.setRef(outcomePrefix+"a-b", r.outcomeRecord(v1, "stale"))
182 unknown("an outcome under a name another change's version maps to", outcomePrefix+"a-b")
183 r.setRef(outcomePrefix+"a-b", "")
184 r.setRef(outcomePrefix+"c-"+id, r.outcomeRecord(v1, "refused-not-ready"))
185 if kind, text := r.compare(id, 2, v2); kind != compareNone || text != "" {
186 t.Fatalf("an outcome that judged no approval counted: %s\n%s", kind, text)
187 }
188 r.setRef(outcomePrefix+"c-"+id, r.outcomeRecord(v1, "land"))
189 clean("an outcome under c-<change ID>")
190
191 // A version shown is never its own reference, and a greater version
192 // named only ambiguously stops the search before v1.
193 r.git("checkout", "--quiet", "a/b")
194 r.file("feature.txt", "feature, edited\n")
195 r.git("commit", "--quiet", "-m", "edit the feature")
196 r.git("push", "--quiet", "origin", "a/b")
197 r.valley("ready", "--change", id, "a/b")
198 r.git("checkout", "--quiet", "main")
199 v3 := r.git("rev-parse", "a/b")
200 r.setRef(requestPrefix+"c-"+id, v3)
201 if kind, text := r.compare(id, 3, v3); kind != compareChanged || !strings.Contains(text, "changed since approved v1 at "+short(v1)) {
202 t.Fatalf("v3 is not set against v1, the greatest version below it with an approval: %s\n%s", kind, text)
203 }
204 r.setRef(requestPrefix+"c-"+id, "")
205 r.setRef(byBranch, v2)
206 if kind, text := r.compare(id, 3, v3); kind != compareNone || !strings.Contains(text, "v2 at "+short(v2)+" is named by "+byBranch) {
207 t.Fatalf("v2 named ambiguously did not stop the search before v1: %s\n%s", kind, text)
208 }
209}
210
211// clean names the landings between the two bases, oldest first; changed
212// lists the files whose change differs and the range-diff; and a message
213// or an author that changed is shown whatever the files say.
214func TestCompareSaysWhatChanged(t *testing.T) {
215 r := newCompareRepo(t)
216 id := strings.Repeat("z", 32)
217 r.git("checkout", "--quiet", "-b", "topic")
218 r.file("tool.sh", "#!/bin/sh\n")
219 r.file("image.bin", "\x00\x01")
220 r.git("commit", "--quiet", "-m", "add the tool")
221 r.git("push", "--quiet", "origin", "topic")
222 r.valley("ready", "--change", id, "topic")
223 v1 := r.git("rev-parse", "topic")
224 b0 := r.git("rev-parse", "main")
225 r.setRef(requestPrefix+"c-"+id, v1)
226 one, two := r.moveMain("one"), r.moveMain("two")
227 v2 := r.rebaseOnto("topic")
228 r.valley("ready", "--change", id, "topic")
229 r.git("fetch", "--quiet", "origin")
230
231 kind, text := r.compare(id, 2, v2)
232 want := "zzzz v2 at " + short(v2) + ": clean restack of approved v1 at " + short(v1) + ".\n" +
233 "File delta unchanged. Base " + short(b0) + " -> " + short(two) + ".\n" +
234 "Landings between bases:\n" +
235 " " + short(one) + " land one\n" +
236 " " + short(two) + " land two\n"
237 if kind != compareClean || text != want {
238 t.Fatalf("clean:\n%s\n%s\nwant:\n%s", kind, text, want)
239 }
240
241 // v3: the binary file's content and the tool's mode change. git
242 // range-diff calls the two commits equal, since a binary file's patch
243 // says only that it differs, so the listing of files is what shows it.
244 r.setRef(requestPrefix+"c-"+id, v2)
245 r.git("checkout", "--quiet", "topic")
246 r.file("image.bin", "\x00\x02")
247 if err := os.Chmod(filepath.Join(r.work, "tool.sh"), 0o755); err != nil {
248 t.Fatal(err)
249 }
250 r.git("add", "tool.sh")
251 r.git("commit", "--quiet", "--amend", "--no-edit")
252 r.git("push", "--quiet", "--force", "origin", "topic")
253 r.valley("ready", "--change", id, "topic")
254 r.git("checkout", "--quiet", "main")
255 v3 := r.git("rev-parse", "topic")
256 kind, text = r.compare(id, 3, v3)
257 for _, line := range []string{
258 "zzzz v3 at " + short(v3) + ": changed since approved v2 at " + short(v2) + ".\nReview the range-diff before approving.\n",
259 "Files whose change differs from v2's, as mode and SHA-256 of the content:\n image.bin\n v2: absent -> 100644 ",
260 " tool.sh\n v2: absent -> 100644 ",
261 " v3: absent -> 100755 ",
262 "git range-diff " + short(two) + ".." + short(v2) + " " + short(two) + ".." + short(v3) + ":\n",
263 "1: " + short(v2) + " = 1: " + short(v3) + " add the tool",
264 } {
265 if !strings.Contains(text, line) {
266 t.Fatalf("changed does not say %q:\n%s", line, text)
267 }
268 }
269 if kind != compareChanged || strings.Contains(text, "Commit messages") || strings.Contains(text, "Commit authors") {
270 t.Fatalf("changed: %s\n%s", kind, text)
271 }
272
273 // v4: the same files, with another message and another author.
274 r.setRef(requestPrefix+"c-"+id, v3)
275 r.git("checkout", "--quiet", "topic")
276 r.git("commit", "--quiet", "--amend", "-m", "add the tool, renamed", "--author", "Someone Else <else@localhost>")
277 r.git("push", "--quiet", "--force", "origin", "topic")
278 r.valley("ready", "--change", id, "topic")
279 r.git("checkout", "--quiet", "main")
280 v4 := r.git("rev-parse", "topic")
281 kind, text = r.compare(id, 4, v4)
282 for _, line := range []string{
283 ": clean restack of approved v3 at " + short(v3) + ".\nFile delta unchanged. Base " + short(two) + " -> " + short(two) + ".\n",
284 "Commit messages changed since approved v3. Read them before approving:\n" +
285 " v3, commit 1 of 1, " + short(v3) + ":\n add the tool\n" +
286 " v4, commit 1 of 1, " + short(v4) + ":\n add the tool, renamed\n",
287 "Commit authors changed since approved v3:\n v3, commit 1 of 1, " + short(v3) + ": compare-test <compare-test@localhost> ",
288 " v4, commit 1 of 1, " + short(v4) + ": Someone Else <else@localhost> ",
289 "Landings between bases: none.\n",
290 } {
291 if !strings.Contains(text, line) {
292 t.Fatalf("a changed message and author do not say %q:\n%s", line, text)
293 }
294 }
295 if kind != compareClean {
296 t.Fatalf("a changed message alone is still a clean file delta: %s", kind)
297 }
298
299 // The version given must have the head given.
300 var stdout, stderr bytes.Buffer
301 if code := run([]string{"compare", "--tip", two, id, "3", v4}, &stdout, &stderr); code == 0 || !strings.Contains(stderr.String(), "v3 of change zzzz records the head") {
302 t.Fatalf("compare took a head its version does not record: %d %s", code, stderr.String())
303 }
304}