| 1 | package main |
| 2 | |
| 3 | // What the change records refuse, read from a real repository: a request |
| 4 | // named for a change lands only while that change is ready at the |
| 5 | // request's head, a request of any name is held back by a record that is |
| 6 | // not ready at its head, and a request whose commits hold a merge is |
| 7 | // refused. The records are written by hand, the way anyone holding a key |
| 8 | // could write one where no hook guards the repository. |
| 9 | |
| 10 | import ( |
| 11 | "os" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "the-valley/change" |
| 16 | ) |
| 17 | |
| 18 | // scratchRepo is a repository with a main, and a way to make commits and |
| 19 | // records in it. |
| 20 | type scratchRepo struct { |
| 21 | t *testing.T |
| 22 | repo string |
| 23 | } |
| 24 | |
| 25 | func newScratchRepo(t *testing.T) *scratchRepo { |
| 26 | s := &scratchRepo{t: t, repo: t.TempDir()} |
| 27 | s.git("init", "--quiet", "--initial-branch=main") |
| 28 | s.git("commit", "--quiet", "--allow-empty", "-m", "base") |
| 29 | return s |
| 30 | } |
| 31 | |
| 32 | func (s *scratchRepo) git(args ...string) string { |
| 33 | s.t.Helper() |
| 34 | out, err := git(s.repo, append([]string{"-c", "user.name=t", "-c", "user.email=t@localhost"}, args...)...) |
| 35 | if err != nil { |
| 36 | s.t.Fatal(err) |
| 37 | } |
| 38 | return strings.TrimSpace(out) |
| 39 | } |
| 40 | |
| 41 | func (s *scratchRepo) input(stdin string, args ...string) string { |
| 42 | s.t.Helper() |
| 43 | out, err := gitInput(s.repo, []byte(stdin), append([]string{"-c", "user.name=t", "-c", "user.email=t@localhost"}, args...)...) |
| 44 | if err != nil { |
| 45 | s.t.Fatal(err) |
| 46 | } |
| 47 | return strings.TrimSpace(string(out)) |
| 48 | } |
| 49 | |
| 50 | // commit is a new commit on parents, each with a tree of its own. |
| 51 | func (s *scratchRepo) commit(subject string, parents ...string) string { |
| 52 | s.t.Helper() |
| 53 | blob := s.input(subject+"\n", "hash-object", "-w", "--stdin") |
| 54 | tree := s.input("100644 blob "+blob+"\t"+subject+".txt\n", "mktree") |
| 55 | args := []string{"commit-tree", tree, "-m", subject} |
| 56 | for _, p := range parents { |
| 57 | args = append(args, "-p", p) |
| 58 | } |
| 59 | return s.git(args...) |
| 60 | } |
| 61 | |
| 62 | // record writes a record of the change at the heads given, in the state |
| 63 | // given, as the ref for id. |
| 64 | func (s *scratchRepo) record(id, state string, heads ...string) { |
| 65 | s.t.Helper() |
| 66 | r := &change.Record{ |
| 67 | Schema: change.Schema, ChangeID: id, Target: "main", Owner: "owner", State: state, |
| 68 | Seq: len(heads) + 1, Active: len(heads), |
| 69 | } |
| 70 | for i, h := range heads { |
| 71 | r.Versions = append(r.Versions, change.Version{ |
| 72 | N: i + 1, Head: change.HeadPrefix + h, Title: "a version", Branch: "topic", Uploader: "owner", |
| 73 | }) |
| 74 | } |
| 75 | data, err := change.Encode(r) |
| 76 | if err != nil { |
| 77 | s.t.Fatal(err) |
| 78 | } |
| 79 | s.recordText(change.Ref("main", id), string(data), heads...) |
| 80 | } |
| 81 | |
| 82 | // recordText writes a record commit holding text as its record.json, at ref. |
| 83 | func (s *scratchRepo) recordText(ref, text string, parents ...string) { |
| 84 | s.t.Helper() |
| 85 | blob := s.input(text, "hash-object", "-w", "--stdin") |
| 86 | tree := s.input("100644 blob "+blob+"\trecord.json\n", "mktree") |
| 87 | args := []string{"commit-tree", tree, "-m", "a change record"} |
| 88 | for _, p := range parents { |
| 89 | args = append(args, "-p", p) |
| 90 | } |
| 91 | s.git("update-ref", ref, s.git(args...)) |
| 92 | } |
| 93 | |
| 94 | const ( |
| 95 | readyID = "kkkkllllmmmmnnnnooooppppqqqqrrrr" |
| 96 | draftID = "zzzzyyyyxxxxwwwwvvvvuuuuttttssss" |
| 97 | goneID = "ppppppppppppppppqqqqqqqqqqqqqqqq" |
| 98 | ) |
| 99 | |
| 100 | func TestTheRecordsRefuseARequestForAChangeThatIsNotReady(t *testing.T) { |
| 101 | s := newScratchRepo(t) |
| 102 | base := s.git("rev-parse", "HEAD") |
| 103 | v1 := s.commit("v1", base) |
| 104 | v2 := s.commit("v2", base) |
| 105 | drafted := s.commit("drafted", base) |
| 106 | child := s.commit("child of drafted", drafted) |
| 107 | loose := s.commit("loose", base) |
| 108 | s.record(readyID, change.Ready, v1, v2) |
| 109 | s.record(draftID, change.Draft, drafted) |
| 110 | // A record that does not read, and one whose ref names another change. |
| 111 | s.recordText(change.Ref("main", goneID), "{}\n", loose) |
| 112 | s.recordText(change.Ref("main", "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"), |
| 113 | strings.Replace(recordJSON(t, readyID, change.Draft, loose), readyID, draftID, 1), loose) |
| 114 | |
| 115 | in := &integrator{repo: s.repo} |
| 116 | idx, err := in.readRecords() |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if len(idx.unreadable) != 2 { |
| 121 | t.Fatalf("unreadable = %q, want the two records that do not read", idx.unreadable) |
| 122 | } |
| 123 | if len(idx.byHead[v1]) != 1 || len(idx.byHead[v2]) != 1 || len(idx.byHead[drafted]) != 1 || len(idx.byHead[loose]) != 0 { |
| 124 | t.Fatalf("the index by head is %v", idx.byHead) |
| 125 | } |
| 126 | |
| 127 | for _, c := range []struct { |
| 128 | name, change, head string |
| 129 | refused bool |
| 130 | says string |
| 131 | }{ |
| 132 | {"its change's request at a version head", change.RequestName(readyID), v2, false, ""}, |
| 133 | {"its change's request at an older version head", change.RequestName(readyID), v1, false, ""}, |
| 134 | {"its change's request at a head no version records", change.RequestName(readyID), loose, true, "is no version of change " + readyID}, |
| 135 | {"a draft's request", change.RequestName(draftID), drafted, true, "change " + draftID + " is draft, not ready"}, |
| 136 | {"a request for a change with no record", change.RequestName("llllllllllllllllllllllllllllllll"), loose, true, "holds no record of it"}, |
| 137 | {"a request for a change whose record does not read", change.RequestName(goneID), loose, true, "does not read"}, |
| 138 | {"a branch's request at a draft's head", "topic", drafted, true, "is v1 of change " + draftID + ", which is draft, not ready"}, |
| 139 | {"a request for a ready change at a draft's head", change.RequestName(readyID), drafted, true, ""}, |
| 140 | {"a branch's request at a ready change's head", "topic", v1, false, ""}, |
| 141 | {"a branch's request no record knows", "loose", loose, false, ""}, |
| 142 | {"a branch named like a request but not one", "c-" + readyID[:31], drafted, true, "which is draft"}, |
| 143 | // A line of commits above a draft's head brings the draft with it. |
| 144 | {"another name's request for a child of a draft's head", "other", child, true, |
| 145 | "the commit " + drafted[:12] + " it brings is v1 of change " + draftID + ", which is draft, not ready"}, |
| 146 | } { |
| 147 | r := request{ref: requestPrefix + "/main/" + c.change, change: c.change, target: "refs/heads/main", head: c.head} |
| 148 | outcome, why, err := in.recordRefusal(r, idx, base) |
| 149 | if err != nil { |
| 150 | t.Fatalf("%s: %v", c.name, err) |
| 151 | } |
| 152 | if refused := outcome != ""; refused != c.refused || (refused && outcome != refusedNotReady) { |
| 153 | t.Errorf("%s: refused %q (%s), want refused %v", c.name, outcome, why, c.refused) |
| 154 | } |
| 155 | if !strings.Contains(why, c.says) { |
| 156 | t.Errorf("%s: refused saying %q, without %q", c.name, why, c.says) |
| 157 | } |
| 158 | } |
| 159 | // Once the draft has landed, its head is on the tip and brings nothing. |
| 160 | r := request{change: "other", target: "refs/heads/main", head: child} |
| 161 | if outcome, why, _ := in.recordRefusal(r, idx, drafted); outcome != "" { |
| 162 | t.Errorf("a draft's head already on the tip still refuses its child: %s", why) |
| 163 | } |
| 164 | |
| 165 | // The final check reads every record again: the draft readied lets its |
| 166 | // head through, the ready change unreadied holds its heads back, and a |
| 167 | // record that newly names a head the pass's index did not know holds |
| 168 | // that head back too. |
| 169 | s.record(draftID, change.Ready, drafted) |
| 170 | s.record(readyID, change.Draft, v1, v2) |
| 171 | newID := "lllllllllllllllllllllllllllllllk" |
| 172 | s.record(newID, change.Draft, loose) |
| 173 | for _, c := range []struct { |
| 174 | head string |
| 175 | refused bool |
| 176 | }{{drafted, false}, {v1, true}, {loose, true}} { |
| 177 | r := request{change: "topic", target: "refs/heads/main", head: c.head} |
| 178 | outcome, why, err := in.finalRefusal(r, base) |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | if (outcome == refusedNotReady) != c.refused { |
| 183 | t.Errorf("the final check of %s: refused %q (%s), want refused %v", c.head[:12], outcome, why, c.refused) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // A record that does not read may name only commits its record commit |
| 189 | // reaches, so a request is flagged when a commit it brings is one of |
| 190 | // those, and not otherwise. |
| 191 | func TestARecordThatDoesNotReadIsReportedWhereItMayMatter(t *testing.T) { |
| 192 | s := newScratchRepo(t) |
| 193 | base := s.git("rev-parse", "HEAD") |
| 194 | named := s.commit("named", base) |
| 195 | above := s.commit("above", named) |
| 196 | elsewhere := s.commit("elsewhere", base) |
| 197 | s.recordText(change.Ref("main", draftID), "{}\n", named) |
| 198 | in := &integrator{repo: s.repo} |
| 199 | idx, err := in.readRecords() |
| 200 | if err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | for _, c := range []struct { |
| 204 | name, head, tip string |
| 205 | flagged bool |
| 206 | }{ |
| 207 | {"its own head", named, base, true}, |
| 208 | {"a child of its head", above, base, true}, |
| 209 | {"a head it cannot reach", elsewhere, base, false}, |
| 210 | {"a child of its head, once its head is on the tip", above, named, false}, |
| 211 | } { |
| 212 | r := request{change: "topic", target: "refs/heads/main", head: c.head} |
| 213 | if got := len(in.uncheckable(r, idx, c.tip)) == 1; got != c.flagged { |
| 214 | t.Errorf("%s: flagged %v, want %v", c.name, got, c.flagged) |
| 215 | } |
| 216 | if outcome, _, _ := in.recordRefusal(r, idx, c.tip); outcome != "" { |
| 217 | t.Errorf("%s: a record that does not read refused it", c.name) |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // A withdrawn change's requests are deleted, named for it or at one of its |
| 223 | // version heads, each only while the change is still withdrawn and the |
| 224 | // request still where the pass found it. |
| 225 | func TestAWithdrawnChangeVoidsItsRequests(t *testing.T) { |
| 226 | s := newScratchRepo(t) |
| 227 | base := s.git("rev-parse", "HEAD") |
| 228 | h := s.commit("withdrawn", base) |
| 229 | other := s.commit("other", base) |
| 230 | readied := s.commit("readied", base) |
| 231 | s.record(draftID, change.Withdrawn, h) |
| 232 | s.record(readyID, change.Withdrawn, readied) |
| 233 | ref := func(name string) string { return requestPrefix + "/main/" + name } |
| 234 | for name, head := range map[string]string{ |
| 235 | change.RequestName(draftID): h, "topic": h, "unrelated": other, "moved": h, "readied": readied, |
| 236 | } { |
| 237 | s.git("update-ref", ref(name), head) |
| 238 | } |
| 239 | in := &integrator{repo: s.repo, out: devNull(t)} |
| 240 | idx, err := in.readRecords() |
| 241 | if err != nil { |
| 242 | t.Fatal(err) |
| 243 | } |
| 244 | // Between the index and the voiding, one request is approved again at |
| 245 | // another head, and one change is readied again. |
| 246 | s.git("update-ref", ref("moved"), other) |
| 247 | s.record(readyID, change.Ready, readied) |
| 248 | var requests []request |
| 249 | for _, name := range []string{change.RequestName(draftID), "topic", "unrelated", "moved", "readied"} { |
| 250 | head := h |
| 251 | switch name { |
| 252 | case "unrelated": |
| 253 | head = other |
| 254 | case "readied": |
| 255 | head = readied |
| 256 | } |
| 257 | requests = append(requests, request{ref: ref(name), change: name, target: "refs/heads/main", head: head}) |
| 258 | } |
| 259 | kept := in.voidWithdrawn(requests, idx) |
| 260 | var names []string |
| 261 | for _, r := range kept { |
| 262 | names = append(names, r.change) |
| 263 | } |
| 264 | if strings.Join(names, " ") != "unrelated moved readied" { |
| 265 | t.Fatalf("kept %v, want the unrelated request, the one approved again, and the readied change's", names) |
| 266 | } |
| 267 | for name, gone := range map[string]bool{ |
| 268 | change.RequestName(draftID): true, "topic": true, "unrelated": false, "moved": false, "readied": false, |
| 269 | } { |
| 270 | if _, there := gitTry(s.repo, "rev-parse", "--verify", "--quiet", ref(name)); there == gone { |
| 271 | t.Errorf("the request %s: deleted %v, want %v", name, !there, gone) |
| 272 | } |
| 273 | } |
| 274 | memo, _ := gitTry(s.repo, "cat-file", "blob", outcomePrefix+"/main/topic") |
| 275 | if fields := strings.Fields(memo); len(fields) != 4 || fields[3] != string(voidedWithdrawn) { |
| 276 | t.Errorf("the voided request's outcome record is %q", memo) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func recordJSON(t *testing.T, id, state, head string) string { |
| 281 | t.Helper() |
| 282 | data, err := change.Encode(&change.Record{ |
| 283 | Schema: change.Schema, ChangeID: id, Target: "main", Owner: "owner", State: state, Seq: 1, Active: 1, |
| 284 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "a version", Branch: "topic", Uploader: "owner"}}, |
| 285 | }) |
| 286 | if err != nil { |
| 287 | t.Fatal(err) |
| 288 | } |
| 289 | return string(data) |
| 290 | } |
| 291 | |
| 292 | // A record past a bound is not read, so it names no head. |
| 293 | func TestARecordPastItsBoundsIsNotRead(t *testing.T) { |
| 294 | s := newScratchRepo(t) |
| 295 | head := s.commit("head", s.git("rev-parse", "HEAD")) |
| 296 | text := recordJSON(t, draftID, change.Draft, head) |
| 297 | s.recordText(change.Ref("main", draftID), text+strings.Repeat(" ", change.MaxRecordBlob), head) |
| 298 | in := &integrator{repo: s.repo} |
| 299 | idx, err := in.readRecords() |
| 300 | if err != nil { |
| 301 | t.Fatal(err) |
| 302 | } |
| 303 | if len(idx.byHead[head]) != 0 || len(idx.unreadable) != 1 || !strings.Contains(idx.unreadable[0].why, "more than the 65536") { |
| 304 | t.Fatalf("an oversized record read as %v, unreadable %q", idx.byHead, idx.unreadable) |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | func TestARequestWhoseCommitsHoldAMergeIsRefused(t *testing.T) { |
| 309 | s := newScratchRepo(t) |
| 310 | base := s.git("rev-parse", "HEAD") |
| 311 | side := s.commit("side", base) |
| 312 | line := s.commit("line", base) |
| 313 | merged := s.commit("merged", line, side) |
| 314 | above := s.commit("above", merged) |
| 315 | // main itself merged once, below where the change begins. |
| 316 | tip := s.commit("main merged", base, side) |
| 317 | onTip := s.commit("on the tip", tip) |
| 318 | |
| 319 | in := &integrator{repo: s.repo} |
| 320 | for _, c := range []struct { |
| 321 | name, head string |
| 322 | refused bool |
| 323 | }{ |
| 324 | {"a line of commits", line, false}, |
| 325 | {"a merge at the head", merged, true}, |
| 326 | {"a merge below the head", above, true}, |
| 327 | {"a line above a merge main already holds", onTip, false}, |
| 328 | } { |
| 329 | r := request{change: "topic", target: "refs/heads/main", head: c.head} |
| 330 | outcome, why, err := in.mergeRefusal(r, tip) |
| 331 | if err != nil { |
| 332 | t.Fatalf("%s: %v", c.name, err) |
| 333 | } |
| 334 | if refused := outcome == refusedMerge; refused != c.refused { |
| 335 | t.Errorf("%s: refused %v (%s), want %v", c.name, refused, why, c.refused) |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // A refusal is decided again on every pass, so its memo never stands as |
| 341 | // the answer: a draft readied is judged on the next pass though its head, |
| 342 | // the tip and the evidence have not moved. |
| 343 | func TestARefusalIsNeverTheStandingAnswer(t *testing.T) { |
| 344 | in := &integrator{} |
| 345 | memo := "h t e" |
| 346 | for outcome, stands := range map[string]bool{ |
| 347 | "land": true, "stale": true, "reject": true, |
| 348 | string(refusedNotReady): false, string(refusedMerge): false, |
| 349 | } { |
| 350 | if got := in.answered(memo+" "+outcome, memo); got != stands { |
| 351 | t.Errorf("a %s memo stands: %v, want %v", outcome, got, stands) |
| 352 | } |
| 353 | } |
| 354 | if in.answered("other t e land", memo) { |
| 355 | t.Errorf("a memo about another head stands") |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | func devNull(t *testing.T) *os.File { |
| 360 | f, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) |
| 361 | if err != nil { |
| 362 | t.Fatal(err) |
| 363 | } |
| 364 | t.Cleanup(func() { f.Close() }) |
| 365 | return f |
| 366 | } |