| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "the-valley/change" |
| 11 | "the-valley/delta" |
| 12 | ) |
| 13 | |
| 14 | // A binding names the record, the version and its head, the tip judged |
| 15 | // against, the one merge base of the tip and the head, and the digest of |
| 16 | // the delta between them, and a review bound with it verifies. |
| 17 | func TestABindingReproduces(t *testing.T) { |
| 18 | w := newWorld(t) |
| 19 | s := w.story() |
| 20 | cr, b, err := Bind(w.repo(), s.rec2, 2, s.tip, budget()) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | d, err := delta.Between(delta.Repo{Dir: w.dir, Env: w.env}, s.base, s.head2) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | want := Binding{ |
| 29 | Record: change.HeadPrefix + s.rec2, Version: 2, Head: change.HeadPrefix + s.head2, |
| 30 | TargetTip: change.HeadPrefix + s.tip, Base: change.HeadPrefix + s.base, Delta: d.Digest(), |
| 31 | } |
| 32 | if b != want || cr.ChangeID != changeID || cr.Target != "main" { |
| 33 | t.Fatalf("Bind = %+v for %s on %s, want %+v", b, cr.ChangeID, cr.Target, want) |
| 34 | } |
| 35 | if len(d) != 2 { |
| 36 | t.Fatalf("the delta has %d paths, where the version changes 2", len(d)) |
| 37 | } |
| 38 | rec := w.verdict(s, "reviewer", Approve) |
| 39 | if err := Verify(w.repo(), rec, w.against()); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if _, _, err := Bind(w.repo(), s.rec2, 3, s.tip, budget()); err == nil || !strings.Contains(err.Error(), "no version 3") { |
| 43 | t.Fatalf("a version the record does not hold: %v", err) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // A head and a tip with no history in common, or with two merge bases, |
| 48 | // give no base to compare from, so no binding: Bind refuses, and Verify |
| 49 | // refuses a binding that names one. |
| 50 | func TestNoBaseOrAnAmbiguousOneMakesNoBinding(t *testing.T) { |
| 51 | w := newWorld(t) |
| 52 | s := w.story() |
| 53 | unrelated := w.commit(map[string]string{"elsewhere": "x\n"}) |
| 54 | if _, _, err := Bind(w.repo(), s.rec2, 2, unrelated, budget()); !errors.Is(err, ErrNoBase) { |
| 55 | t.Fatalf("a tip with no history in common: %v", err) |
| 56 | } |
| 57 | |
| 58 | // A criss-cross: two merges of a and b, each the other way round, have |
| 59 | // both a and b as merge bases. |
| 60 | a := w.commit(map[string]string{"README.md": "a\n"}, s.base) |
| 61 | b := w.commit(map[string]string{"README.md": "b\n"}, s.base) |
| 62 | ab := w.commit(map[string]string{"README.md": "ab\n"}, a, b) |
| 63 | ba := w.commit(map[string]string{"README.md": "ba\n"}, b, a) |
| 64 | cr := &change.Record{ |
| 65 | Schema: change.Schema, ChangeID: otherID, Target: "main", Owner: "author", State: change.Ready, Seq: 1, |
| 66 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + ba, Title: "Cross", Uploader: "author"}}, |
| 67 | Active: 1, |
| 68 | } |
| 69 | record := w.record(cr, ba) |
| 70 | w.setRef(change.Ref("main", otherID), record) |
| 71 | w.setRef("refs/heads/main", ab) |
| 72 | _, _, err := Bind(w.repo(), record, 1, ab, budget()) |
| 73 | if !errors.Is(err, ErrAmbiguousBase) || !strings.Contains(err.Error(), a) || !strings.Contains(err.Error(), b) { |
| 74 | t.Fatalf("a criss-cross: %v", err) |
| 75 | } |
| 76 | d, err := delta.Between(delta.Repo{Dir: w.dir, Env: w.env}, a, ba) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | rec := &Record{ |
| 81 | Schema: Schema, ReviewID: id1, ChangeID: otherID, Target: "main", Review: "operator", Reviewer: "reviewer", |
| 82 | Verdict: Approve, Summary: "Fine.", |
| 83 | Binding: &Binding{Record: change.HeadPrefix + record, Version: 1, Head: change.HeadPrefix + ba, |
| 84 | TargetTip: change.HeadPrefix + ab, Base: change.HeadPrefix + a, Delta: d.Digest()}, |
| 85 | } |
| 86 | against := Against{RecordTip: record, TargetTip: ab, Budget: budget()} |
| 87 | if err := Verify(w.repo(), rec, against); !errors.Is(err, ErrAmbiguousBase) { |
| 88 | t.Fatalf("a binding that picks one of two bases: %v", err) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Each case changes one part of a good review, and Verify refuses it, |
| 93 | // saying which part does not reproduce. |
| 94 | func TestVerifyRefusesWhatDoesNotReproduce(t *testing.T) { |
| 95 | w := newWorld(t) |
| 96 | s := w.story() |
| 97 | other := &change.Record{ |
| 98 | Schema: change.Schema, ChangeID: otherID, Target: "main", Owner: "author", State: change.Ready, Seq: 2, |
| 99 | Versions: s.record.Versions, Active: 2, |
| 100 | } |
| 101 | otherRecord := w.record(other, s.rec1, s.head2) |
| 102 | // A record commit that reads exactly as the change's record of seq 2 |
| 103 | // does, written beside its history rather than on it. |
| 104 | forged := w.record(s.record, s.head2) |
| 105 | offMain := w.commit(map[string]string{"README.md": "elsewhere\n", "docs/guide.md": "one\ntwo\nthree\n"}, s.base) |
| 106 | blob := w.blob("x\n") |
| 107 | cases := []struct { |
| 108 | name string |
| 109 | change func(r *Record, a *Against) |
| 110 | says string |
| 111 | budgets bool |
| 112 | }{ |
| 113 | {"another delta", func(r *Record, a *Against) { r.Binding.Delta = digest }, "binding.delta is " + digest, false}, |
| 114 | {"another base", func(r *Record, a *Against) { r.Binding.Base = change.HeadPrefix + s.tip }, "binding.base is " + s.tip, false}, |
| 115 | {"another head", func(r *Record, a *Against) { r.Binding.Head = change.HeadPrefix + s.head1 }, "as version 2", false}, |
| 116 | {"a version the record does not hold", func(r *Record, a *Against) { r.Binding.Version = 3 }, "no version 3", false}, |
| 117 | {"a record that holds one version", func(r *Record, a *Against) { r.Binding.Record = change.HeadPrefix + s.rec1 }, "no version 2", false}, |
| 118 | {"another change's record", func(r *Record, a *Against) { r.Binding.Record = change.HeadPrefix + otherRecord }, "a record of the change " + otherID, false}, |
| 119 | {"a record that is no commit", func(r *Record, a *Against) { r.Binding.Record = change.HeadPrefix + blob }, "is a blob", false}, |
| 120 | {"a record off the change's history", func(r *Record, a *Against) { r.Binding.Record = change.HeadPrefix + forged }, "is not the change's record of seq 2", false}, |
| 121 | {"a record past the change's", func(r *Record, a *Against) { a.RecordTip = s.rec1 }, "the change's record is at 1", false}, |
| 122 | {"a tip that is no commit", func(r *Record, a *Against) { r.Binding.TargetTip = change.HeadPrefix + blob }, "is a blob, where a commit is expected", false}, |
| 123 | {"a tip off the target's history", func(r *Record, a *Against) { r.Binding.TargetTip = change.HeadPrefix + offMain }, "is not on the history of the target's tip", false}, |
| 124 | {"a note on another blob", func(r *Record, a *Against) { r.Notes[0].Blob = change.HeadPrefix + blob }, "notes[0] names \"docs/guide.md\" on the after side as", false}, |
| 125 | {"a note on the other side", func(r *Record, a *Against) { r.Notes[2].Side = Before }, "notes[2] names \"docs/new file.md\" on the before side, where there is no such file", false}, |
| 126 | {"a note on a directory", func(r *Record, a *Against) { r.Notes[0].Path = "docs" }, "where it is a tree", false}, |
| 127 | {"a note past the last line", func(r *Record, a *Against) { r.Notes[1].Lines = &Lines{Start: 3, End: 4} }, "names lines 3 to 4 of \"docs/guide.md\" on the before side, which has 3", false}, |
| 128 | {"no time left", func(r *Record, a *Against) { a.Budget.Deadline = time.Now().Add(-time.Second) }, "deadline", true}, |
| 129 | {"no deadline at all", func(r *Record, a *Against) { a.Budget.Deadline = time.Time{} }, "sets none", false}, |
| 130 | } |
| 131 | for _, c := range cases { |
| 132 | t.Run(c.name, func(t *testing.T) { |
| 133 | rec, a := w.verdict(s, "reviewer", RequestChanges), w.against() |
| 134 | if err := Verify(w.repo(), rec, a); err != nil { |
| 135 | t.Fatalf("the review before the change: %v", err) |
| 136 | } |
| 137 | c.change(rec, &a) |
| 138 | err := Verify(w.repo(), rec, a) |
| 139 | if err == nil { |
| 140 | t.Fatal("it verified") |
| 141 | } |
| 142 | if !strings.Contains(err.Error(), c.says) || errors.Is(err, delta.ErrBudget) != c.budgets { |
| 143 | t.Fatalf("the refusal does not say %q, or is wrongly a budget's: %v", c.says, err) |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | if err := Verify(w.repo(), withdrawal(w.verdict(s, "reviewer", Approve), "reviewer"), w.against()); err == nil { |
| 148 | t.Fatal("a withdrawal verified") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // A version whose tree is hostile, a few small trees each naming the next |
| 153 | // 64 times, or whose files are larger than the budget, cannot be bound or |
| 154 | // verified within the budget. The refusal says so, as a budget's, and not |
| 155 | // that the binding is wrong. |
| 156 | func TestHostileTreesHitTheBudget(t *testing.T) { |
| 157 | w := newWorld(t) |
| 158 | s := w.story() |
| 159 | tree := w.tree(map[string]string{"leaf": "x\n"}) |
| 160 | for level := 0; level < 4; level++ { |
| 161 | var entries strings.Builder |
| 162 | for i := 0; i < 64; i++ { |
| 163 | fmt.Fprintf(&entries, "040000 tree %s\td%02d\n", tree, i) |
| 164 | } |
| 165 | tree = w.git([]byte(entries.String()), "mktree") |
| 166 | } |
| 167 | hostile := w.commitTree(tree, s.base) |
| 168 | large := w.commit(map[string]string{"README.md": strings.Repeat("0123456789abcdef", 1<<18)}, s.base) |
| 169 | for _, c := range []struct { |
| 170 | name, head string |
| 171 | budget func(*delta.Budget) |
| 172 | says string |
| 173 | }{ |
| 174 | {"64 to the 4th paths", hostile, func(b *delta.Budget) { b.Entries = 10000 }, "10000 entries"}, |
| 175 | {"a file of 4 MiB", large, func(b *delta.Budget) { b.Bytes = 1 << 20 }, "1048576 bytes"}, |
| 176 | } { |
| 177 | t.Run(c.name, func(t *testing.T) { |
| 178 | cr := &change.Record{ |
| 179 | Schema: change.Schema, ChangeID: otherID, Target: "main", Owner: "author", State: change.Ready, Seq: 1, |
| 180 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + c.head, Title: "Hostile", Uploader: "author"}}, |
| 181 | Active: 1, |
| 182 | } |
| 183 | record := w.record(cr, c.head) |
| 184 | b := budget() |
| 185 | c.budget(&b) |
| 186 | start := time.Now() |
| 187 | _, _, err := Bind(w.repo(), record, 1, s.tip, b) |
| 188 | if !errors.Is(err, delta.ErrBudget) || !strings.Contains(err.Error(), c.says) { |
| 189 | t.Fatalf("Bind: %v", err) |
| 190 | } |
| 191 | rec := &Record{ |
| 192 | Schema: Schema, ReviewID: id1, ChangeID: otherID, Target: "main", Review: "operator", Reviewer: "reviewer", |
| 193 | Verdict: RequestChanges, Summary: "Too large to read.", |
| 194 | Binding: &Binding{Record: change.HeadPrefix + record, Version: 1, Head: change.HeadPrefix + c.head, |
| 195 | TargetTip: change.HeadPrefix + s.tip, Base: change.HeadPrefix + s.base, Delta: digest}, |
| 196 | } |
| 197 | err = Verify(w.repo(), rec, Against{RecordTip: record, TargetTip: s.tip, Budget: b}) |
| 198 | if !errors.Is(err, delta.ErrBudget) || !strings.Contains(err.Error(), c.says) { |
| 199 | t.Fatalf("Verify: %v", err) |
| 200 | } |
| 201 | if elapsed := time.Since(start); elapsed > 20*time.Second { |
| 202 | t.Fatalf("stopping at the budget took %s", elapsed) |
| 203 | } |
| 204 | }) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // A deadline that passes after the last read from git, while the delta is |
| 209 | // hashed, fails Bind and Verify as a budget's refusal. Neither returns a |
| 210 | // result computed late, and a note looked up after the deadline is not |
| 211 | // taken for a note on a file that is not there. |
| 212 | func TestTheDeadlineHoldsAfterTheLastRead(t *testing.T) { |
| 213 | w := newWorld(t) |
| 214 | s := w.story() |
| 215 | var deadline time.Time |
| 216 | reached := false |
| 217 | digested = func() { |
| 218 | reached = true |
| 219 | time.Sleep(time.Until(deadline) + 10*time.Millisecond) |
| 220 | } |
| 221 | defer func() { digested = func() {} }() |
| 222 | tight := func() delta.Budget { |
| 223 | b := budget() |
| 224 | deadline = time.Now().Add(2 * time.Second) |
| 225 | b.Deadline = deadline |
| 226 | reached = false |
| 227 | return b |
| 228 | } |
| 229 | withNotes := w.verdict(s, "reviewer", Approve) |
| 230 | noNotes := w.verdict(s, "reviewer", Approve) |
| 231 | noNotes.Notes = nil |
| 232 | for _, c := range []struct { |
| 233 | name string |
| 234 | run func(delta.Budget) error |
| 235 | }{ |
| 236 | {"Bind", func(b delta.Budget) error { |
| 237 | _, _, err := Bind(w.repo(), s.rec2, 2, s.tip, b) |
| 238 | return err |
| 239 | }}, |
| 240 | {"Verify, with notes to look up", func(b delta.Budget) error { |
| 241 | a := w.against() |
| 242 | a.Budget = b |
| 243 | return Verify(w.repo(), withNotes, a) |
| 244 | }}, |
| 245 | {"Verify, with no notes", func(b delta.Budget) error { |
| 246 | a := w.against() |
| 247 | a.Budget = b |
| 248 | return Verify(w.repo(), noNotes, a) |
| 249 | }}, |
| 250 | } { |
| 251 | t.Run(c.name, func(t *testing.T) { |
| 252 | err := c.run(tight()) |
| 253 | if !reached { |
| 254 | t.Fatalf("it did not reach the digest within two seconds: %v", err) |
| 255 | } |
| 256 | if !errors.Is(err, delta.ErrBudget) || !strings.Contains(err.Error(), "deadline") { |
| 257 | t.Fatalf("a deadline passed while hashing: %v", err) |
| 258 | } |
| 259 | }) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // A file a note names has no bound of its own, only the budget's bytes. A |
| 264 | // note on an unchanged file too large for them, which the delta never |
| 265 | // reads, is refused as the budget's, so the caller can tell a review too |
| 266 | // large to check from one that is wrong. Within a larger budget it |
| 267 | // verifies. |
| 268 | func TestANoteOnAFileTooLargeForTheBudget(t *testing.T) { |
| 269 | w := newWorld(t) |
| 270 | large := strings.Repeat("0123456789abcdef\n", 1<<16) |
| 271 | base := w.commit(map[string]string{"large.txt": large, "README.md": "hello\n"}) |
| 272 | head := w.commit(map[string]string{"large.txt": large, "README.md": "hello, world\n"}, base) |
| 273 | cr := &change.Record{ |
| 274 | Schema: change.Schema, ChangeID: otherID, Target: "main", Owner: "author", State: change.Ready, Seq: 1, |
| 275 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "Greet the world", Uploader: "author"}}, |
| 276 | Active: 1, |
| 277 | } |
| 278 | record := w.record(cr, head) |
| 279 | _, b, err := Bind(w.repo(), record, 1, base, budget()) |
| 280 | if err != nil { |
| 281 | t.Fatal(err) |
| 282 | } |
| 283 | rec := &Record{ |
| 284 | Schema: Schema, ReviewID: id1, ChangeID: otherID, Target: "main", Review: "operator", Reviewer: "reviewer", |
| 285 | Verdict: Approve, Binding: &b, Summary: "Fine.", |
| 286 | Notes: []Note{{Side: After, Path: "large.txt", Blob: change.HeadPrefix + w.git(nil, "rev-parse", head+":large.txt"), |
| 287 | Lines: &Lines{Start: 1, End: 2}, Body: "These lines are fine."}}, |
| 288 | } |
| 289 | against := Against{RecordTip: record, TargetTip: base, Budget: budget()} |
| 290 | if err := Verify(w.repo(), rec, against); err != nil { |
| 291 | t.Fatalf("within a budget of 64 MiB: %v", err) |
| 292 | } |
| 293 | against.Budget.Bytes = 1 << 20 |
| 294 | err = Verify(w.repo(), rec, against) |
| 295 | if !errors.Is(err, delta.ErrBudget) || !strings.Contains(err.Error(), `notes[0] names lines of "large.txt"`) { |
| 296 | t.Fatalf("a note on a file of %d bytes within 1 MiB: %v", len(large), err) |
| 297 | } |
| 298 | rec.Notes[0].Path = "missing.txt" |
| 299 | err = Verify(w.repo(), rec, against) |
| 300 | if errors.Is(err, delta.ErrBudget) || err == nil || !strings.Contains(err.Error(), "where there is no such file") { |
| 301 | t.Fatalf("a note on a file that is not there: %v", err) |
| 302 | } |
| 303 | } |