| 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 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "the-valley/change" |
| 15 | ) |
| 16 | |
| 17 | // scratchRepo is a repository with a main, and a way to make commits and |
| 18 | // records in it. |
| 19 | type scratchRepo struct { |
| 20 | t *testing.T |
| 21 | repo string |
| 22 | } |
| 23 | |
| 24 | func newScratchRepo(t *testing.T) *scratchRepo { |
| 25 | s := &scratchRepo{t: t, repo: t.TempDir()} |
| 26 | s.git("init", "--quiet", "--initial-branch=main") |
| 27 | s.git("commit", "--quiet", "--allow-empty", "-m", "base") |
| 28 | return s |
| 29 | } |
| 30 | |
| 31 | func (s *scratchRepo) git(args ...string) string { |
| 32 | s.t.Helper() |
| 33 | out, err := git(s.repo, append([]string{"-c", "user.name=t", "-c", "user.email=t@localhost"}, args...)...) |
| 34 | if err != nil { |
| 35 | s.t.Fatal(err) |
| 36 | } |
| 37 | return strings.TrimSpace(out) |
| 38 | } |
| 39 | |
| 40 | func (s *scratchRepo) input(stdin string, args ...string) string { |
| 41 | s.t.Helper() |
| 42 | out, err := gitInput(s.repo, []byte(stdin), append([]string{"-c", "user.name=t", "-c", "user.email=t@localhost"}, args...)...) |
| 43 | if err != nil { |
| 44 | s.t.Fatal(err) |
| 45 | } |
| 46 | return strings.TrimSpace(string(out)) |
| 47 | } |
| 48 | |
| 49 | // commit is a new commit on parents, each with a tree of its own. |
| 50 | func (s *scratchRepo) commit(subject string, parents ...string) string { |
| 51 | s.t.Helper() |
| 52 | blob := s.input(subject+"\n", "hash-object", "-w", "--stdin") |
| 53 | tree := s.input("100644 blob "+blob+"\t"+subject+".txt\n", "mktree") |
| 54 | args := []string{"commit-tree", tree, "-m", subject} |
| 55 | for _, p := range parents { |
| 56 | args = append(args, "-p", p) |
| 57 | } |
| 58 | return s.git(args...) |
| 59 | } |
| 60 | |
| 61 | // record writes a record of the change at the heads given, in the state |
| 62 | // given, as the ref for id. |
| 63 | func (s *scratchRepo) record(id, state string, heads ...string) { |
| 64 | s.t.Helper() |
| 65 | r := &change.Record{ |
| 66 | Schema: change.Schema, ChangeID: id, Target: "main", Owner: "owner", State: state, |
| 67 | Seq: len(heads) + 1, Active: len(heads), |
| 68 | } |
| 69 | for i, h := range heads { |
| 70 | r.Versions = append(r.Versions, change.Version{ |
| 71 | N: i + 1, Head: change.HeadPrefix + h, Title: "a version", Branch: "topic", Uploader: "owner", |
| 72 | }) |
| 73 | } |
| 74 | data, err := change.Encode(r) |
| 75 | if err != nil { |
| 76 | s.t.Fatal(err) |
| 77 | } |
| 78 | s.recordText(change.Ref("main", id), string(data), heads...) |
| 79 | } |
| 80 | |
| 81 | // recordText writes a record commit holding text as its record.json, at ref. |
| 82 | func (s *scratchRepo) recordText(ref, text string, parents ...string) { |
| 83 | s.t.Helper() |
| 84 | blob := s.input(text, "hash-object", "-w", "--stdin") |
| 85 | tree := s.input("100644 blob "+blob+"\trecord.json\n", "mktree") |
| 86 | args := []string{"commit-tree", tree, "-m", "a change record"} |
| 87 | for _, p := range parents { |
| 88 | args = append(args, "-p", p) |
| 89 | } |
| 90 | s.git("update-ref", ref, s.git(args...)) |
| 91 | } |
| 92 | |
| 93 | const ( |
| 94 | readyID = "kkkkllllmmmmnnnnooooppppqqqqrrrr" |
| 95 | draftID = "zzzzyyyyxxxxwwwwvvvvuuuuttttssss" |
| 96 | goneID = "ppppppppppppppppqqqqqqqqqqqqqqqq" |
| 97 | ) |
| 98 | |
| 99 | func TestTheRecordsRefuseARequestForAChangeThatIsNotReady(t *testing.T) { |
| 100 | s := newScratchRepo(t) |
| 101 | base := s.git("rev-parse", "HEAD") |
| 102 | v1 := s.commit("v1", base) |
| 103 | v2 := s.commit("v2", base) |
| 104 | drafted := s.commit("drafted", base) |
| 105 | loose := s.commit("loose", base) |
| 106 | s.record(readyID, change.Ready, v1, v2) |
| 107 | s.record(draftID, change.Draft, drafted) |
| 108 | // A record that does not read, and one whose ref names another change. |
| 109 | s.recordText(change.Ref("main", goneID), "{}\n", loose) |
| 110 | s.recordText(change.Ref("main", "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"), |
| 111 | strings.Replace(recordJSON(t, readyID, change.Draft, loose), readyID, draftID, 1), loose) |
| 112 | |
| 113 | in := &integrator{repo: s.repo} |
| 114 | idx, err := in.readRecords() |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | if len(idx.unreadable) != 2 { |
| 119 | t.Fatalf("unreadable = %q, want the two records that do not read", idx.unreadable) |
| 120 | } |
| 121 | if len(idx.byHead[v1]) != 1 || len(idx.byHead[v2]) != 1 || len(idx.byHead[drafted]) != 1 || len(idx.byHead[loose]) != 0 { |
| 122 | t.Fatalf("the index by head is %v", idx.byHead) |
| 123 | } |
| 124 | |
| 125 | for _, c := range []struct { |
| 126 | name, change, head string |
| 127 | refused bool |
| 128 | says string |
| 129 | }{ |
| 130 | {"its change's request at a version head", change.RequestName(readyID), v2, false, ""}, |
| 131 | {"its change's request at an older version head", change.RequestName(readyID), v1, false, ""}, |
| 132 | {"its change's request at a head no version records", change.RequestName(readyID), loose, true, "is no version of change " + readyID}, |
| 133 | {"a draft's request", change.RequestName(draftID), drafted, true, "change " + draftID + " is draft, not ready"}, |
| 134 | {"a request for a change with no record", change.RequestName("llllllllllllllllllllllllllllllll"), loose, true, "holds no record of it"}, |
| 135 | {"a request for a change whose record does not read", change.RequestName(goneID), loose, true, "does not read"}, |
| 136 | {"a branch's request at a draft's head", "topic", drafted, true, "is v1 of change " + draftID + ", which is draft, not ready"}, |
| 137 | {"a request for a ready change at a draft's head", change.RequestName(readyID), drafted, true, ""}, |
| 138 | {"a branch's request at a ready change's head", "topic", v1, false, ""}, |
| 139 | {"a branch's request no record knows", "loose", loose, false, ""}, |
| 140 | {"a branch named like a request but not one", "c-" + readyID[:31], drafted, true, "which is draft"}, |
| 141 | } { |
| 142 | r := request{ref: requestPrefix + "/main/" + c.change, change: c.change, target: "refs/heads/main", head: c.head} |
| 143 | for _, reread := range []bool{false, true} { |
| 144 | outcome, why, err := in.recordRefusal(r, idx, reread) |
| 145 | if err != nil { |
| 146 | t.Fatalf("%s: %v", c.name, err) |
| 147 | } |
| 148 | if refused := outcome != ""; refused != c.refused || (refused && outcome != refusedNotReady) { |
| 149 | t.Errorf("%s (reread %v): refused %q (%s), want refused %v", c.name, reread, outcome, why, c.refused) |
| 150 | } |
| 151 | if !strings.Contains(why, c.says) { |
| 152 | t.Errorf("%s (reread %v): refused saying %q, without %q", c.name, reread, why, c.says) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Read again, a record that changed since the index was read is read |
| 158 | // as it is now: the draft readied lets its head through, and the ready |
| 159 | // change unreadied holds its heads back. |
| 160 | s.record(draftID, change.Ready, drafted) |
| 161 | s.record(readyID, change.Draft, v1, v2) |
| 162 | r := request{change: "topic", target: "refs/heads/main", head: drafted} |
| 163 | if outcome, why, _ := in.recordRefusal(r, idx, true); outcome != "" { |
| 164 | t.Errorf("a draft readied since the index was read still refuses: %s", why) |
| 165 | } |
| 166 | r = request{change: "topic", target: "refs/heads/main", head: v1} |
| 167 | if outcome, _, _ := in.recordRefusal(r, idx, true); outcome != refusedNotReady { |
| 168 | t.Errorf("a change unreadied since the index was read refuses nothing") |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func recordJSON(t *testing.T, id, state, head string) string { |
| 173 | t.Helper() |
| 174 | data, err := change.Encode(&change.Record{ |
| 175 | Schema: change.Schema, ChangeID: id, Target: "main", Owner: "owner", State: state, Seq: 1, Active: 1, |
| 176 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "a version", Branch: "topic", Uploader: "owner"}}, |
| 177 | }) |
| 178 | if err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | return string(data) |
| 182 | } |
| 183 | |
| 184 | // A record past a bound is not read, so it names no head. |
| 185 | func TestARecordPastItsBoundsIsNotRead(t *testing.T) { |
| 186 | s := newScratchRepo(t) |
| 187 | head := s.commit("head", s.git("rev-parse", "HEAD")) |
| 188 | text := recordJSON(t, draftID, change.Draft, head) |
| 189 | s.recordText(change.Ref("main", draftID), text+strings.Repeat(" ", change.MaxRecordBlob), head) |
| 190 | in := &integrator{repo: s.repo} |
| 191 | idx, err := in.readRecords() |
| 192 | if err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | if len(idx.byHead[head]) != 0 || len(idx.unreadable) != 1 || !strings.Contains(idx.unreadable[0], "more than the 65536") { |
| 196 | t.Fatalf("an oversized record read as %v, unreadable %q", idx.byHead, idx.unreadable) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestARequestWhoseCommitsHoldAMergeIsRefused(t *testing.T) { |
| 201 | s := newScratchRepo(t) |
| 202 | base := s.git("rev-parse", "HEAD") |
| 203 | side := s.commit("side", base) |
| 204 | line := s.commit("line", base) |
| 205 | merged := s.commit("merged", line, side) |
| 206 | above := s.commit("above", merged) |
| 207 | // main itself merged once, below where the change begins. |
| 208 | tip := s.commit("main merged", base, side) |
| 209 | onTip := s.commit("on the tip", tip) |
| 210 | |
| 211 | in := &integrator{repo: s.repo} |
| 212 | for _, c := range []struct { |
| 213 | name, head string |
| 214 | refused bool |
| 215 | }{ |
| 216 | {"a line of commits", line, false}, |
| 217 | {"a merge at the head", merged, true}, |
| 218 | {"a merge below the head", above, true}, |
| 219 | {"a line above a merge main already holds", onTip, false}, |
| 220 | } { |
| 221 | r := request{change: "topic", target: "refs/heads/main", head: c.head} |
| 222 | outcome, why, err := in.mergeRefusal(r, tip) |
| 223 | if err != nil { |
| 224 | t.Fatalf("%s: %v", c.name, err) |
| 225 | } |
| 226 | if refused := outcome == refusedMerge; refused != c.refused { |
| 227 | t.Errorf("%s: refused %v (%s), want %v", c.name, refused, why, c.refused) |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // A refusal is decided again on every pass, so its memo never stands as |
| 233 | // the answer: a draft readied is judged on the next pass though its head, |
| 234 | // the tip and the evidence have not moved. |
| 235 | func TestARefusalIsNeverTheStandingAnswer(t *testing.T) { |
| 236 | in := &integrator{} |
| 237 | memo := "h t e" |
| 238 | for outcome, stands := range map[string]bool{ |
| 239 | "land": true, "stale": true, "reject": true, |
| 240 | string(refusedNotReady): false, string(refusedMerge): false, |
| 241 | } { |
| 242 | if got := in.answered(memo+" "+outcome, memo); got != stands { |
| 243 | t.Errorf("a %s memo stands: %v, want %v", outcome, got, stands) |
| 244 | } |
| 245 | } |
| 246 | if in.answered("other t e land", memo) { |
| 247 | t.Errorf("a memo about another head stands") |
| 248 | } |
| 249 | } |