| 1 | package main |
| 2 | |
| 3 | import ( |
| 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. |
| 14 | type compareRepo struct { |
| 15 | t *testing.T |
| 16 | origin, work string |
| 17 | } |
| 18 | |
| 19 | func 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 | |
| 40 | func (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 | |
| 53 | func (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. |
| 59 | func (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. |
| 68 | func (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. |
| 79 | func (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. |
| 89 | func (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 "". |
| 98 | func (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. |
| 108 | func (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 | |
| 119 | func (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. |
| 131 | func 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 | // A branch named c/<ID> or c-<ID> maps to c-<ID>, which is reserved for |
| 212 | // the change with that ID. A request or an outcome under it is that |
| 213 | // change's, so it is no evidence of approval for a change whose versions |
| 214 | // name the branch, even when it names one of their heads. |
| 215 | func TestAnotherChangesReservedNameIsNoEvidence(t *testing.T) { |
| 216 | id, other := strings.Repeat("x", 32), strings.Repeat("y", 32) |
| 217 | for _, prefix := range []string{"c/", "c-"} { |
| 218 | t.Run(prefix, func(t *testing.T) { |
| 219 | r := newCompareRepo(t) |
| 220 | branch := prefix + other |
| 221 | r.git("checkout", "--quiet", "-b", branch) |
| 222 | r.file("feature.txt", "feature\n") |
| 223 | r.git("commit", "--quiet", "-m", "add the feature") |
| 224 | r.git("push", "--quiet", "origin", branch) |
| 225 | r.valley("ready", "--change", id, branch) |
| 226 | v1 := r.git("rev-parse", branch) |
| 227 | r.moveMain("other") |
| 228 | v2 := r.rebaseOnto(branch) |
| 229 | r.valley("ready", "--change", id, branch) |
| 230 | r.git("fetch", "--quiet", "origin") |
| 231 | |
| 232 | reserved := "c-" + other |
| 233 | r.setRef(requestPrefix+reserved, v1) |
| 234 | r.setRef(outcomePrefix+reserved, r.outcomeRecord(v1, "land")) |
| 235 | if kind, text := r.compare(id, 2, v2); kind != compareNone || text != "" { |
| 236 | t.Fatalf("a request and an outcome under %s counted for %s: %s\n%s", reserved, id[:4], kind, text) |
| 237 | } |
| 238 | r.setRef(requestPrefix+"c-"+id, v1) |
| 239 | if kind, text := r.compare(id, 2, v2); kind != compareClean || !strings.Contains(text, "clean restack of approved v1 at "+short(v1)) { |
| 240 | t.Fatalf("a request under the change's own c-<change ID>: %s\n%s", kind, text) |
| 241 | } |
| 242 | }) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // clean names the landings between the two bases, oldest first; changed |
| 247 | // lists the files whose change differs, the range-diff, and the shown |
| 248 | // version's diff of those files in git's ordinary form, whatever the |
| 249 | // person's diff configuration; and a message or an author that changed is |
| 250 | // shown whatever the files say. |
| 251 | func TestCompareSaysWhatChanged(t *testing.T) { |
| 252 | r := newCompareRepo(t) |
| 253 | id := strings.Repeat("z", 32) |
| 254 | r.git("checkout", "--quiet", "-b", "topic") |
| 255 | r.file("tool.sh", "#!/bin/sh\n") |
| 256 | r.file("image.bin", "\x00\x01") |
| 257 | r.git("commit", "--quiet", "-m", "add the tool") |
| 258 | r.git("push", "--quiet", "origin", "topic") |
| 259 | r.valley("ready", "--change", id, "topic") |
| 260 | v1 := r.git("rev-parse", "topic") |
| 261 | b0 := r.git("rev-parse", "main") |
| 262 | r.setRef(requestPrefix+"c-"+id, v1) |
| 263 | one, two := r.moveMain("one"), r.moveMain("two") |
| 264 | v2 := r.rebaseOnto("topic") |
| 265 | r.valley("ready", "--change", id, "topic") |
| 266 | r.git("fetch", "--quiet", "origin") |
| 267 | |
| 268 | kind, text := r.compare(id, 2, v2) |
| 269 | want := "zzzz v2 at " + short(v2) + ": clean restack of approved v1 at " + short(v1) + ".\n" + |
| 270 | "File delta unchanged. Base " + short(b0) + " -> " + short(two) + ".\n" + |
| 271 | "Landings between bases:\n" + |
| 272 | " " + short(one) + " land one\n" + |
| 273 | " " + short(two) + " land two\n" |
| 274 | if kind != compareClean || text != want { |
| 275 | t.Fatalf("clean:\n%s\n%s\nwant:\n%s", kind, text, want) |
| 276 | } |
| 277 | |
| 278 | // v3: the binary file's content and the tool's mode change. git |
| 279 | // range-diff calls the two commits equal, since a binary file's patch |
| 280 | // says only that it differs, so the listing of files is what shows it. |
| 281 | r.setRef(requestPrefix+"c-"+id, v2) |
| 282 | r.git("checkout", "--quiet", "topic") |
| 283 | r.file("image.bin", "\x00\x02") |
| 284 | if err := os.Chmod(filepath.Join(r.work, "tool.sh"), 0o755); err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | r.git("add", "tool.sh") |
| 288 | r.git("commit", "--quiet", "--amend", "--no-edit") |
| 289 | r.git("push", "--quiet", "--force", "origin", "topic") |
| 290 | r.valley("ready", "--change", id, "topic") |
| 291 | r.git("checkout", "--quiet", "main") |
| 292 | v3 := r.git("rev-parse", "topic") |
| 293 | r.git("config", "diff.noprefix", "true") |
| 294 | kind, text = r.compare(id, 3, v3) |
| 295 | r.git("config", "--unset", "diff.noprefix") |
| 296 | for _, line := range []string{ |
| 297 | "zzzz v3 at " + short(v3) + ": changed since approved v2 at " + short(v2) + ".\nReview the range-diff before approving.\n", |
| 298 | "Files whose change differs from v2's, as mode and SHA-256 of the content:\n image.bin\n v2: absent -> 100644 ", |
| 299 | " tool.sh\n v2: absent -> 100644 ", |
| 300 | " v3: absent -> 100755 ", |
| 301 | "git range-diff " + short(two) + ".." + short(v2) + " " + short(two) + ".." + short(v3) + ":\n", |
| 302 | "1: " + short(v2) + " = 1: " + short(v3) + " add the tool", |
| 303 | "\nv3's diff of the files whose change differs, from its base " + short(two) + |
| 304 | ". A note written beside a line of it keeps its file and line:\ndiff --git a/image.bin b/image.bin\n", |
| 305 | "Binary files /dev/null and b/image.bin differ\ndiff --git a/tool.sh b/tool.sh\nnew file mode 100755\n", |
| 306 | "@@ -0,0 +1 @@\n+#!/bin/sh\n", |
| 307 | } { |
| 308 | if !strings.Contains(text, line) { |
| 309 | t.Fatalf("changed does not say %q:\n%s", line, text) |
| 310 | } |
| 311 | } |
| 312 | if kind != compareChanged || strings.Contains(text, "Commit messages") || strings.Contains(text, "Commit authors") { |
| 313 | t.Fatalf("changed: %s\n%s", kind, text) |
| 314 | } |
| 315 | |
| 316 | // v4: the same files, with another message and another author. |
| 317 | r.setRef(requestPrefix+"c-"+id, v3) |
| 318 | r.git("checkout", "--quiet", "topic") |
| 319 | r.git("commit", "--quiet", "--amend", "-m", "add the tool, renamed", "--author", "Someone Else <else@localhost>") |
| 320 | r.git("push", "--quiet", "--force", "origin", "topic") |
| 321 | r.valley("ready", "--change", id, "topic") |
| 322 | r.git("checkout", "--quiet", "main") |
| 323 | v4 := r.git("rev-parse", "topic") |
| 324 | kind, text = r.compare(id, 4, v4) |
| 325 | for _, line := range []string{ |
| 326 | ": clean restack of approved v3 at " + short(v3) + ".\nFile delta unchanged. Base " + short(two) + " -> " + short(two) + ".\n", |
| 327 | "Commit messages changed since approved v3. Read them before approving:\n" + |
| 328 | " v3, commit 1 of 1, " + short(v3) + ":\n add the tool\n" + |
| 329 | " v4, commit 1 of 1, " + short(v4) + ":\n add the tool, renamed\n", |
| 330 | "Commit authors changed since approved v3:\n v3, commit 1 of 1, " + short(v3) + ": compare-test <compare-test@localhost> ", |
| 331 | " v4, commit 1 of 1, " + short(v4) + ": Someone Else <else@localhost> ", |
| 332 | "Landings between bases: none.\n", |
| 333 | } { |
| 334 | if !strings.Contains(text, line) { |
| 335 | t.Fatalf("a changed message and author do not say %q:\n%s", line, text) |
| 336 | } |
| 337 | } |
| 338 | if kind != compareClean { |
| 339 | t.Fatalf("a changed message alone is still a clean file delta: %s", kind) |
| 340 | } |
| 341 | |
| 342 | // The version given must have the head given. |
| 343 | var stdout, stderr bytes.Buffer |
| 344 | 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") { |
| 345 | t.Fatalf("compare took a head its version does not record: %d %s", code, stderr.String()) |
| 346 | } |
| 347 | } |